c0
C0DATA — structured data using ASCII C0 control codes (C)
Loading...
Searching...
No Matches
c0_diff.h
Go to the documentation of this file.
1/* c0_diff.h — C0DIFF: atomic multi-file edits with anchored patterns.
2 *
3 * A diff is a C0DATA document whose heading declares `shape` `diff`
4 * (DIFF.md): FS begins a file (its path), RS a record that is one anchored
5 * pattern of US-separated units, each an anchor or a substitution written as
6 * a sub-unit, `old␚␟new` (the unit is what the file holds, its sub-unit what
7 * it will hold). A record's search text (its anchors and olds) must match
8 * exactly once from the previous record's match to the end of the file; the
9 * replacement puts each new in place of its old. Anchors, olds and news are
10 * values under the ordinary rules (plain or text); paths are names. Notes
11 * are skipped; everything the text diff does not use (keyed units, levels,
12 * references, header rows, GS, two sub-units on one unit, a sub-unit of a
13 * sub-unit, dead records and dead file lines, a record before the first FS,
14 * an empty path, the same path twice, an empty search text, a bare
15 * insertion, `␟␚␟`) is refused; SUB before RS, FS or SOH is a syntax error.
16 * There is no replace-all.
17 */
18#ifndef C0_DIFF_H
19#define C0_DIFF_H
20
21#include "c0.h"
22#include "c0_strbuf.h"
23
24#ifdef __cplusplus
25extern "C" {
26#endif
27
40
41/* --- Parsed model (all buffers malloc'd; free the root with c0_diff_free) --- */
42
45typedef struct {
46 uint8_t *old;
47 size_t old_len;
48 uint8_t *new_;
49 size_t new_len;
50 int is_sub;
52
54typedef struct {
56 size_t nunits;
58
60typedef struct {
61 uint8_t *path;
62 size_t path_len;
64 size_t nrecords;
66
75
78c0_diff c0_diff_parse(const uint8_t *buf, size_t len);
80
84uint8_t *c0_diff_record_search(const c0_diff_record *r, size_t *out_len);
85uint8_t *c0_diff_record_replacement(const c0_diff_record *r, size_t *out_len);
86
87/* --- Apply --- */
88
89typedef struct {
91 char path[256];
92 size_t record;
93 size_t pos;
94 const char *msg;
96
102
104typedef struct {
105 uint8_t *path;
106 size_t path_len;
107 uint8_t *content;
110
116int c0_diff_apply(const uint8_t *diff_buf, size_t diff_len,
117 const c0_diff_file_input *files, size_t nfiles,
118 c0_diff_file_output **out, size_t *out_count,
119 c0_diff_error *err);
120
122
123/* --- Builder --- */
124
125typedef struct {
129
133
135void c0_diff_file_begin(c0_diff_builder *b, const uint8_t *path, size_t path_len);
139void c0_diff_anchor(c0_diff_builder *b, const uint8_t *text, size_t len);
140void c0_diff_sub(c0_diff_builder *b, const uint8_t *old, size_t old_len, const uint8_t *new_, size_t new_len);
143 const uint8_t *before, size_t before_len,
144 const uint8_t *old, size_t old_len,
145 const uint8_t *new_, size_t new_len,
146 const uint8_t *after, size_t after_len);
148void c0_diff_insert_after(c0_diff_builder *b, const uint8_t *anchor, size_t anchor_len, const uint8_t *text, size_t len);
150void c0_diff_delete(c0_diff_builder *b, const uint8_t *old, size_t old_len,
151 const uint8_t *before, size_t before_len, const uint8_t *after, size_t after_len);
153void c0_diff_assert(c0_diff_builder *b, const uint8_t *text, size_t len);
155void c0_diff_note(c0_diff_builder *b, const uint8_t *text, size_t len);
158
161
162/* NUL-terminated convenience wrappers. */
163void c0_diff_file_begin_str(c0_diff_builder *b, const char *path);
164void c0_diff_anchor_str(c0_diff_builder *b, const char *text);
165void c0_diff_sub_str(c0_diff_builder *b, const char *old, const char *new_);
166void c0_diff_replace_str(c0_diff_builder *b, const char *before, const char *old, const char *new_, const char *after);
167void c0_diff_insert_after_str(c0_diff_builder *b, const char *anchor, const char *text);
168void c0_diff_delete_str(c0_diff_builder *b, const char *old, const char *before, const char *after);
169void c0_diff_assert_str(c0_diff_builder *b, const char *text);
170void c0_diff_note_str(c0_diff_builder *b, const char *text);
171
172#ifdef __cplusplus
173}
174#endif
175
176#endif /* C0_DIFF_H */
void c0_diff_replace(c0_diff_builder *b, const uint8_t *before, size_t before_len, const uint8_t *old, size_t old_len, const uint8_t *new_, size_t new_len, const uint8_t *after, size_t after_len)
One record: anchor, old␚␟new, anchor (empty anchors are omitted).
void c0_diff_note_str(c0_diff_builder *b, const char *text)
void c0_diff_delete_str(c0_diff_builder *b, const char *old, const char *before, const char *after)
void c0_diff_assert(c0_diff_builder *b, const uint8_t *text, size_t len)
One record: assert that text is present once; nothing changes.
void c0_diff_sub(c0_diff_builder *b, const uint8_t *old, size_t old_len, const uint8_t *new_, size_t new_len)
void c0_diff_anchor_str(c0_diff_builder *b, const char *text)
void c0_diff_sub_str(c0_diff_builder *b, const char *old, const char *new_)
void c0_diff_file_begin(c0_diff_builder *b, const uint8_t *path, size_t path_len)
FS and the path (a name).
void c0_diff_anchor(c0_diff_builder *b, const uint8_t *text, size_t len)
An anchor / a substitution old␚␟new as the next unit of the current record.
void c0_diff_assert_str(c0_diff_builder *b, const char *text)
int c0_diff_apply(const uint8_t *diff_buf, size_t diff_len, const c0_diff_file_input *files, size_t nfiles, c0_diff_file_output **out, size_t *out_count, c0_diff_error *err)
Apply a diff to in-memory files.
void c0_diff_insert_after_str(c0_diff_builder *b, const char *anchor, const char *text)
void c0_diff_free(c0_diff *d)
void c0_diff_free_outputs(c0_diff_file_output *out, size_t count)
void c0_diff_insert_after(c0_diff_builder *b, const uint8_t *anchor, size_t anchor_len, const uint8_t *text, size_t len)
One record: insert text after anchor (␞anchor␟␚␟text).
void c0_diff_note(c0_diff_builder *b, const uint8_t *text, size_t len)
BEL and a note.
c0_diff c0_diff_parse(const uint8_t *buf, size_t len)
Parse a diff document (with or without its SYN heading), refusing everything DIFF....
uint8_t * c0_diff_record_search(const c0_diff_record *r, size_t *out_len)
A record's search text (its anchors and olds) or replacement (news in place of olds),...
c0_bytes c0_diff_builder_bytes(const c0_diff_builder *b)
Borrow the builder's bytes (valid until it is freed or extended).
void c0_diff_eot(c0_diff_builder *b)
EOT.
void c0_diff_delete(c0_diff_builder *b, const uint8_t *old, size_t old_len, const uint8_t *before, size_t before_len, const uint8_t *after, size_t after_len)
One record: remove old, found once after the cursor, between optional anchors.
c0_diff_status
Definition c0_diff.h:28
@ C0_DIFF_OK
Definition c0_diff.h:29
@ C0_DIFF_FILE_NOT_FOUND
a path with no input file
Definition c0_diff.h:34
@ C0_DIFF_CONSTRAINT
the diff holds something a diff may not (see msg)
Definition c0_diff.h:32
@ C0_DIFF_EMPTY_SEARCH
a record whose search text is empty
Definition c0_diff.h:35
@ C0_DIFF_DUPLICATE_PATH
the same path twice
Definition c0_diff.h:33
@ C0_DIFF_OOM
Definition c0_diff.h:38
@ C0_DIFF_WRONG_SHAPE
the heading's shape is present and not diff
Definition c0_diff.h:31
@ C0_DIFF_PATTERN_NOT_FOUND
a record's search text does not occur
Definition c0_diff.h:36
@ C0_DIFF_SYNTAX
the diff is not well-formed C0DATA (see msg/pos)
Definition c0_diff.h:30
@ C0_DIFF_PATTERN_AMBIGUOUS
it occurs more than once after the cursor
Definition c0_diff.h:37
uint8_t * c0_diff_record_replacement(const c0_diff_record *r, size_t *out_len)
void c0_diff_builder_init(c0_diff_builder *b, int heading)
Start a diff; with heading the SYN heading shape diff is written first.
void c0_diff_record_begin(c0_diff_builder *b)
RS: a new record (anchored pattern).
void c0_diff_builder_free(c0_diff_builder *b)
void c0_diff_file_begin_str(c0_diff_builder *b, const char *path)
void c0_diff_replace_str(c0_diff_builder *b, const char *before, const char *old, const char *new_, const char *after)
A zero-copy view into a buffer: not NUL-terminated.
Definition c0.h:66
Definition c0_diff.h:125
c0_strbuf buf
Definition c0_diff.h:126
int unit_first
at the start of a record's pattern
Definition c0_diff.h:127
Definition c0_diff.h:89
size_t record
offending record index within the file
Definition c0_diff.h:92
char path[256]
offending file (truncated, NUL-terminated)
Definition c0_diff.h:91
c0_diff_status status
Definition c0_diff.h:90
const char * msg
static message
Definition c0_diff.h:94
size_t pos
byte offset in the diff, for SYNTAX
Definition c0_diff.h:93
An input file (borrowed).
Definition c0_diff.h:98
c0_bytes content
Definition c0_diff.h:100
c0_bytes path
Definition c0_diff.h:99
An output file (malloc'd path and content).
Definition c0_diff.h:104
size_t path_len
Definition c0_diff.h:106
uint8_t * content
Definition c0_diff.h:107
size_t content_len
Definition c0_diff.h:108
uint8_t * path
Definition c0_diff.h:105
A file's records, in order.
Definition c0_diff.h:60
uint8_t * path
Definition c0_diff.h:61
size_t nrecords
Definition c0_diff.h:64
size_t path_len
Definition c0_diff.h:62
c0_diff_record * records
Definition c0_diff.h:63
A record: one anchored pattern.
Definition c0_diff.h:54
size_t nunits
Definition c0_diff.h:56
c0_diff_unit * units
Definition c0_diff.h:55
One unit of a pattern: an anchor (is_sub 0, new_ NULL) or a substitution old␚␟new.
Definition c0_diff.h:45
uint8_t * new_
Definition c0_diff.h:48
uint8_t * old
Definition c0_diff.h:46
size_t old_len
Definition c0_diff.h:47
int is_sub
Definition c0_diff.h:50
size_t new_len
Definition c0_diff.h:49
A parsed diff.
Definition c0_diff.h:69
size_t nfiles
Definition c0_diff.h:71
c0_diff_file * files
Definition c0_diff.h:70
c0_error err
Definition c0_diff.h:73
c0_diff_status status
Definition c0_diff.h:72
Error detail: the code, the byte offset it was found at, and a static message.
Definition c0.h:91
Definition c0_strbuf.h:10