c0data 0.3.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +122 -24
- data/c0-c/c0.h +2309 -1509
- data/c0-c/c0_diff.c +551 -0
- data/c0-c/c0_diff.h +176 -0
- data/c0-c/c0_strbuf.h +49 -0
- data/ext/c0/c0_diff_impl.c +5 -0
- data/ext/c0/c0_ext.c +354 -69
- data/ext/c0/extconf.rb +2 -1
- data/lib/c0/body.rb +158 -38
- data/lib/c0/builder.rb +18 -13
- data/lib/c0/diff.rb +193 -0
- data/lib/c0/document.rb +49 -25
- data/lib/c0/stream.rb +71 -13
- data/lib/c0.rb +18 -11
- metadata +6 -1
data/c0-c/c0_diff.h
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
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
|
|
25
|
+
extern "C" {
|
|
26
|
+
#endif
|
|
27
|
+
|
|
28
|
+
typedef enum {
|
|
29
|
+
C0_DIFF_OK = 0,
|
|
30
|
+
C0_DIFF_SYNTAX, /**< the diff is not well-formed C0DATA (see msg/pos) */
|
|
31
|
+
C0_DIFF_WRONG_SHAPE, /**< the heading's shape is present and not diff */
|
|
32
|
+
C0_DIFF_CONSTRAINT, /**< the diff holds something a diff may not (see msg) */
|
|
33
|
+
C0_DIFF_DUPLICATE_PATH, /**< the same path twice */
|
|
34
|
+
C0_DIFF_FILE_NOT_FOUND, /**< a path with no input file */
|
|
35
|
+
C0_DIFF_EMPTY_SEARCH, /**< a record whose search text is empty */
|
|
36
|
+
C0_DIFF_PATTERN_NOT_FOUND, /**< a record's search text does not occur */
|
|
37
|
+
C0_DIFF_PATTERN_AMBIGUOUS, /**< it occurs more than once after the cursor */
|
|
38
|
+
C0_DIFF_OOM
|
|
39
|
+
} c0_diff_status;
|
|
40
|
+
|
|
41
|
+
/* --- Parsed model (all buffers malloc'd; free the root with c0_diff_free) --- */
|
|
42
|
+
|
|
43
|
+
/** One unit of a pattern: an anchor (is_sub 0, new_ NULL) or a substitution
|
|
44
|
+
* `old␚␟new`. Values are decoded. */
|
|
45
|
+
typedef struct {
|
|
46
|
+
uint8_t *old;
|
|
47
|
+
size_t old_len;
|
|
48
|
+
uint8_t *new_;
|
|
49
|
+
size_t new_len;
|
|
50
|
+
int is_sub;
|
|
51
|
+
} c0_diff_unit;
|
|
52
|
+
|
|
53
|
+
/** A record: one anchored pattern. With no substitution it is an assertion. */
|
|
54
|
+
typedef struct {
|
|
55
|
+
c0_diff_unit *units;
|
|
56
|
+
size_t nunits;
|
|
57
|
+
} c0_diff_record;
|
|
58
|
+
|
|
59
|
+
/** A file's records, in order. */
|
|
60
|
+
typedef struct {
|
|
61
|
+
uint8_t *path;
|
|
62
|
+
size_t path_len;
|
|
63
|
+
c0_diff_record *records;
|
|
64
|
+
size_t nrecords;
|
|
65
|
+
} c0_diff_file;
|
|
66
|
+
|
|
67
|
+
/** A parsed diff. `status` is C0_DIFF_OK, or why parsing refused (SYNTAX,
|
|
68
|
+
* WRONG_SHAPE, CONSTRAINT, DUPLICATE_PATH, OOM) with detail in `err`. */
|
|
69
|
+
typedef struct {
|
|
70
|
+
c0_diff_file *files;
|
|
71
|
+
size_t nfiles;
|
|
72
|
+
c0_diff_status status;
|
|
73
|
+
c0_error err;
|
|
74
|
+
} c0_diff;
|
|
75
|
+
|
|
76
|
+
/** Parse a diff document (with or without its SYN heading), refusing
|
|
77
|
+
* everything DIFF.md lists under "What a diff may not hold". */
|
|
78
|
+
c0_diff c0_diff_parse(const uint8_t *buf, size_t len);
|
|
79
|
+
void c0_diff_free(c0_diff *d);
|
|
80
|
+
|
|
81
|
+
/** A record's search text (its anchors and olds) or replacement (news in
|
|
82
|
+
* place of olds), concatenated into a malloc'd buffer the caller frees.
|
|
83
|
+
* NULL only on allocation failure; *out_len gets the length. */
|
|
84
|
+
uint8_t *c0_diff_record_search(const c0_diff_record *r, size_t *out_len);
|
|
85
|
+
uint8_t *c0_diff_record_replacement(const c0_diff_record *r, size_t *out_len);
|
|
86
|
+
|
|
87
|
+
/* --- Apply --- */
|
|
88
|
+
|
|
89
|
+
typedef struct {
|
|
90
|
+
c0_diff_status status;
|
|
91
|
+
char path[256]; /**< offending file (truncated, NUL-terminated) */
|
|
92
|
+
size_t record; /**< offending record index within the file */
|
|
93
|
+
size_t pos; /**< byte offset in the diff, for SYNTAX */
|
|
94
|
+
const char *msg; /**< static message */
|
|
95
|
+
} c0_diff_error;
|
|
96
|
+
|
|
97
|
+
/** An input file (borrowed). */
|
|
98
|
+
typedef struct {
|
|
99
|
+
c0_bytes path;
|
|
100
|
+
c0_bytes content;
|
|
101
|
+
} c0_diff_file_input;
|
|
102
|
+
|
|
103
|
+
/** An output file (malloc'd path and content). */
|
|
104
|
+
typedef struct {
|
|
105
|
+
uint8_t *path;
|
|
106
|
+
size_t path_len;
|
|
107
|
+
uint8_t *content;
|
|
108
|
+
size_t content_len;
|
|
109
|
+
} c0_diff_file_output;
|
|
110
|
+
|
|
111
|
+
/** Apply a diff to in-memory files. Atomic: everything is validated before
|
|
112
|
+
* anything is produced. On success returns 1, allocates *out (one entry per
|
|
113
|
+
* input file, in input order, edited or copied unchanged) and sets
|
|
114
|
+
* *out_count; free with c0_diff_free_outputs. On failure returns 0, fills
|
|
115
|
+
* *err (when given) and allocates nothing. */
|
|
116
|
+
int 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
|
+
|
|
121
|
+
void c0_diff_free_outputs(c0_diff_file_output *out, size_t count);
|
|
122
|
+
|
|
123
|
+
/* --- Builder --- */
|
|
124
|
+
|
|
125
|
+
typedef struct {
|
|
126
|
+
c0_strbuf buf;
|
|
127
|
+
int unit_first; /**< at the start of a record's pattern */
|
|
128
|
+
} c0_diff_builder;
|
|
129
|
+
|
|
130
|
+
/** Start a diff; with `heading` the SYN heading `shape diff` is written first. */
|
|
131
|
+
void c0_diff_builder_init(c0_diff_builder *b, int heading);
|
|
132
|
+
void c0_diff_builder_free(c0_diff_builder *b);
|
|
133
|
+
|
|
134
|
+
/** FS and the path (a name). */
|
|
135
|
+
void c0_diff_file_begin(c0_diff_builder *b, const uint8_t *path, size_t path_len);
|
|
136
|
+
/** RS: a new record (anchored pattern). */
|
|
137
|
+
void c0_diff_record_begin(c0_diff_builder *b);
|
|
138
|
+
/** An anchor / a substitution `old␚␟new` as the next unit of the current record. */
|
|
139
|
+
void c0_diff_anchor(c0_diff_builder *b, const uint8_t *text, size_t len);
|
|
140
|
+
void c0_diff_sub(c0_diff_builder *b, const uint8_t *old, size_t old_len, const uint8_t *new_, size_t new_len);
|
|
141
|
+
/** One record: anchor, old␚␟new, anchor (empty anchors are omitted). */
|
|
142
|
+
void c0_diff_replace(c0_diff_builder *b,
|
|
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);
|
|
147
|
+
/** One record: insert `text` after `anchor` (`␞anchor␟␚␟text`). */
|
|
148
|
+
void c0_diff_insert_after(c0_diff_builder *b, const uint8_t *anchor, size_t anchor_len, const uint8_t *text, size_t len);
|
|
149
|
+
/** One record: remove `old`, found once after the cursor, between optional anchors. */
|
|
150
|
+
void 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);
|
|
152
|
+
/** One record: assert that `text` is present once; nothing changes. */
|
|
153
|
+
void c0_diff_assert(c0_diff_builder *b, const uint8_t *text, size_t len);
|
|
154
|
+
/** BEL and a note. */
|
|
155
|
+
void c0_diff_note(c0_diff_builder *b, const uint8_t *text, size_t len);
|
|
156
|
+
/** EOT. */
|
|
157
|
+
void c0_diff_eot(c0_diff_builder *b);
|
|
158
|
+
|
|
159
|
+
/** Borrow the builder's bytes (valid until it is freed or extended). */
|
|
160
|
+
c0_bytes c0_diff_builder_bytes(const c0_diff_builder *b);
|
|
161
|
+
|
|
162
|
+
/* NUL-terminated convenience wrappers. */
|
|
163
|
+
void c0_diff_file_begin_str(c0_diff_builder *b, const char *path);
|
|
164
|
+
void c0_diff_anchor_str(c0_diff_builder *b, const char *text);
|
|
165
|
+
void c0_diff_sub_str(c0_diff_builder *b, const char *old, const char *new_);
|
|
166
|
+
void c0_diff_replace_str(c0_diff_builder *b, const char *before, const char *old, const char *new_, const char *after);
|
|
167
|
+
void c0_diff_insert_after_str(c0_diff_builder *b, const char *anchor, const char *text);
|
|
168
|
+
void c0_diff_delete_str(c0_diff_builder *b, const char *old, const char *before, const char *after);
|
|
169
|
+
void c0_diff_assert_str(c0_diff_builder *b, const char *text);
|
|
170
|
+
void c0_diff_note_str(c0_diff_builder *b, const char *text);
|
|
171
|
+
|
|
172
|
+
#ifdef __cplusplus
|
|
173
|
+
}
|
|
174
|
+
#endif
|
|
175
|
+
|
|
176
|
+
#endif /* C0_DIFF_H */
|
data/c0-c/c0_strbuf.h
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/* c0_strbuf.h — a tiny growable byte buffer shared by the converter sources.
|
|
2
|
+
* static inline so it can be included into multiple translation units. */
|
|
3
|
+
#ifndef C0_STRBUF_H
|
|
4
|
+
#define C0_STRBUF_H
|
|
5
|
+
|
|
6
|
+
#include <stdint.h>
|
|
7
|
+
#include <stdlib.h>
|
|
8
|
+
#include <string.h>
|
|
9
|
+
|
|
10
|
+
typedef struct {
|
|
11
|
+
uint8_t *d;
|
|
12
|
+
size_t n;
|
|
13
|
+
size_t cap;
|
|
14
|
+
int oom;
|
|
15
|
+
} c0_strbuf;
|
|
16
|
+
|
|
17
|
+
static inline int c0_sb_reserve(c0_strbuf *s, size_t extra) {
|
|
18
|
+
if (s->oom) return 0;
|
|
19
|
+
if (s->n + extra > s->cap) {
|
|
20
|
+
size_t ncap = s->cap ? s->cap : 64;
|
|
21
|
+
uint8_t *nd;
|
|
22
|
+
while (ncap < s->n + extra) ncap *= 2;
|
|
23
|
+
nd = (uint8_t *)realloc(s->d, ncap);
|
|
24
|
+
if (!nd) {
|
|
25
|
+
s->oom = 1;
|
|
26
|
+
return 0;
|
|
27
|
+
}
|
|
28
|
+
s->d = nd;
|
|
29
|
+
s->cap = ncap;
|
|
30
|
+
}
|
|
31
|
+
return 1;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
static inline void c0_sb_byte(c0_strbuf *s, uint8_t b) {
|
|
35
|
+
if (c0_sb_reserve(s, 1)) s->d[s->n++] = b;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static inline void c0_sb_raw(c0_strbuf *s, const uint8_t *p, size_t n) {
|
|
39
|
+
if (n && c0_sb_reserve(s, n)) {
|
|
40
|
+
memcpy(s->d + s->n, p, n);
|
|
41
|
+
s->n += n;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static inline void c0_sb_cstr(c0_strbuf *s, const char *p) {
|
|
46
|
+
c0_sb_raw(s, (const uint8_t *)p, strlen(p));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#endif /* C0_STRBUF_H */
|