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.
data/ext/c0/c0_ext.c CHANGED
@@ -13,19 +13,28 @@
13
13
  * fs gs rs us sub soh dle can bel enq syn eot etb em) and the
14
14
  * composed methods built on them, block forms included.
15
15
  *
16
- * Errors become exceptions deriving from C0::Error, with the byte position on
17
- * the syntax errors. Malloc'd results from c0.h (pretty form) are copied into
18
- * Ruby strings and freed before returning.
16
+ * C0::Ext.diff_parse / diff_apply C0DIFF (c0_diff.h) over Ruby strings;
17
+ * the Ruby layer in lib/c0/diff.rb gives them their shape.
18
+ *
19
+ * Parse is validate: the readers (documents, bodies, streams) apply the whole
20
+ * grammar and raise on malformed input; the navigators then assume
21
+ * well-formed bytes and report the result model (dead elements dropped; a
22
+ * sub-unit, SUB before US, is not a unit of its record and is reached as the
23
+ * `subs` of the unit before it). Errors become exceptions deriving from
24
+ * C0::Error, with the byte position on the syntax errors. Malloc'd results
25
+ * from the core (pretty form, diff output) are copied into Ruby strings and
26
+ * freed before returning.
19
27
  */
20
28
  #include "ruby.h"
21
29
  #include "ruby/encoding.h"
22
30
 
23
31
  #define C0_IMPLEMENTATION
24
32
  #include "c0.h"
33
+ #include "c0_diff.h"
25
34
 
26
- static VALUE mC0, mExt, mPretty, cBuilder;
35
+ static VALUE mC0, mExt, mPretty, mDiff, cBuilder;
27
36
  static VALUE eError, eSyntaxError, eUnassignedCodeError, eUnexpectedEndError, eKeyError, eBuildError,
28
- eGlyphInDataError;
37
+ eGlyphInDataError, eDiffError;
29
38
 
30
39
  /* ---- buffers and ranges -------------------------------------------------- */
31
40
 
@@ -107,17 +116,20 @@ static const char *status_message(c0_status code) {
107
116
  }
108
117
  }
109
118
 
110
- /* Raise for a c0_error with position detail. */
111
- static void raise_error(const c0_error *err) {
119
+ /* Raise for a c0_error with position detail; `base` is added to the position
120
+ * when the error was found in a slice of the buffer. */
121
+ static void raise_error_at(const c0_error *err, long base) {
112
122
  VALUE exc;
123
+ long pos = base + (long)err->pos;
113
124
  if (err->code == C0_ERR_OOM) rb_memerror();
114
125
  exc = rb_exc_new_str(error_class(err->code),
115
- rb_sprintf("%s at byte %ld", err->msg && *err->msg ? err->msg : status_message(err->code),
116
- (long)err->pos));
117
- rb_ivar_set(exc, rb_intern("@position"), LONG2NUM((long)err->pos));
126
+ rb_sprintf("%s at byte %ld", err->msg && *err->msg ? err->msg : status_message(err->code), pos));
127
+ rb_ivar_set(exc, rb_intern("@position"), LONG2NUM(pos));
118
128
  rb_exc_raise(exc);
119
129
  }
120
130
 
131
+ static void raise_error(const c0_error *err) { raise_error_at(err, 0); }
132
+
121
133
  /* Raise for a bare status (no position). */
122
134
  static void raise_status(c0_status code) {
123
135
  if (code == C0_ERR_OOM) rb_memerror();
@@ -212,18 +224,35 @@ static VALUE ext_unit_level(VALUE self, VALUE str, VALUE s, VALUE e) {
212
224
  return rb_ary_new3(2, LONG2NUM(base + (long)body.start), LONG2NUM(base + (long)body.stop));
213
225
  }
214
226
 
215
- /* The raw unit at [s, e) as a reference: its segments as [start, end] pairs. */
227
+ /* The raw unit at [s, e) as a reference:
228
+ * [name_range | nil, pattern_body_range | nil, [[step_start, step_end], ...], group_name_range | nil].
229
+ * A plain reference has a name; a pattern (ENQ DLE … CAN) has the range of
230
+ * its level's inside. The steps are the path in walking order (c0_ref_path);
231
+ * the group name is set when the reference names a group as a whole. */
216
232
  static VALUE ext_unit_ref(VALUE self, VALUE str, VALUE s, VALUE e) {
217
233
  buf_t b = get_buf(str);
218
234
  c0_ref ref;
219
235
  c0_status st = c0_unit_ref(slice_of(b, s, e), &ref);
220
- VALUE out;
221
- size_t i;
236
+ VALUE name = Qnil, pattern = Qnil, steps, group = Qnil, tmp;
237
+ c0_bytes gname, *arr;
238
+ size_t n, i;
222
239
  (void)self;
223
240
  if (st != C0_OK) raise_status(st);
224
- out = rb_ary_new_capa((long)ref.count);
225
- for (i = 0; i < ref.count; i++) rb_ary_push(out, span(b, ref.seg[i]));
226
- return out;
241
+ if (ref.is_pattern) {
242
+ long base = (long)(ref.pattern.buf - b.p);
243
+ pattern = rb_ary_new3(2, LONG2NUM(base + (long)ref.pattern.start), LONG2NUM(base + (long)ref.pattern.stop));
244
+ } else {
245
+ name = span(b, ref.name);
246
+ }
247
+ n = c0_ref_path(&ref, NULL, 0);
248
+ tmp = rb_str_tmp_new((long)((n > 0 ? n : 1) * sizeof(c0_bytes)));
249
+ arr = (c0_bytes *)RSTRING_PTR(tmp);
250
+ c0_ref_path(&ref, arr, n);
251
+ steps = rb_ary_new_capa((long)n);
252
+ for (i = 0; i < n; i++) rb_ary_push(steps, span(b, arr[i]));
253
+ RB_GC_GUARD(tmp);
254
+ if (c0_ref_group(&ref, &gname)) group = span(b, gname);
255
+ return rb_ary_new3(4, name, pattern, steps, group);
227
256
  }
228
257
 
229
258
  static VALUE collect_units(buf_t b, c0_unit_iter *it) {
@@ -262,7 +291,8 @@ static VALUE ext_record_keyed(VALUE self, VALUE str, VALUE s, VALUE e) {
262
291
  return c0_record_keyed(record_of(b, s, e)) ? Qtrue : Qfalse;
263
292
  }
264
293
 
265
- /* Positional units of the record at [s, e): raw ranges. */
294
+ /* Positional units of the record at [s, e): raw ranges. A sub-unit is not a
295
+ * unit and is not among them. */
266
296
  static VALUE ext_record_units(VALUE self, VALUE str, VALUE s, VALUE e) {
267
297
  buf_t b = get_buf(str);
268
298
  c0_unit_iter it = c0_record_units(record_of(b, s, e));
@@ -270,6 +300,51 @@ static VALUE ext_record_units(VALUE self, VALUE str, VALUE s, VALUE e) {
270
300
  return collect_units(b, &it);
271
301
  }
272
302
 
303
+ /* A unit with its sub-units as [start, end, [sub, ...]], each sub the same
304
+ * shape (the direct children, written order; a sub-unit's own sub-units
305
+ * nest inside it). */
306
+ static VALUE unit_tree(buf_t b, c0_unit u) {
307
+ c0_sub_iter it = c0_unit_subs(u);
308
+ VALUE subs = rb_ary_new();
309
+ c0_unit sub;
310
+ long rs = (long)(u.raw.ptr - b.p);
311
+ while (c0_next_sub(&it, &sub)) rb_ary_push(subs, unit_tree(b, sub));
312
+ if (it.err.code) raise_error(&it.err);
313
+ return rb_ary_new3(3, LONG2NUM(rs), LONG2NUM(rs + (long)u.raw.len), subs);
314
+ }
315
+
316
+ /* Every positional unit of the record at [s, e) with its sub-units:
317
+ * [start, end, subs] each (see unit_tree). */
318
+ static VALUE ext_record_units_with_subs(VALUE self, VALUE str, VALUE s, VALUE e) {
319
+ buf_t b = get_buf(str);
320
+ c0_unit_iter it = c0_record_units(record_of(b, s, e));
321
+ VALUE out = rb_ary_new();
322
+ c0_unit u;
323
+ (void)self;
324
+ while (c0_next_unit_with_subs(&it, &u)) rb_ary_push(out, unit_tree(b, u));
325
+ if (it.err.code) raise_error(&it.err);
326
+ return out;
327
+ }
328
+
329
+ /* Keyed units of the record at [s, e) with their sub-units:
330
+ * [ks, ke, vs, ve, subs] each. */
331
+ static VALUE ext_record_pairs_with_subs(VALUE self, VALUE str, VALUE s, VALUE e) {
332
+ buf_t b = get_buf(str);
333
+ c0_pair_iter it = c0_record_pairs(record_of(b, s, e));
334
+ VALUE out = rb_ary_new();
335
+ c0_bytes k;
336
+ c0_unit u;
337
+ (void)self;
338
+ while (c0_next_pair_with_subs(&it, &k, &u)) {
339
+ long ks = (long)(k.ptr - b.p);
340
+ VALUE tree = unit_tree(b, u);
341
+ rb_ary_push(out, rb_ary_new3(5, LONG2NUM(ks), LONG2NUM(ks + (long)k.len), RARRAY_AREF(tree, 0),
342
+ RARRAY_AREF(tree, 1), RARRAY_AREF(tree, 2)));
343
+ }
344
+ if (it.err.code) raise_error(&it.err);
345
+ return out;
346
+ }
347
+
273
348
  /* Keyed units of the record (or heading) at [s, e): [ks, ke, vs, ve] each. */
274
349
  static VALUE ext_record_pairs(VALUE self, VALUE str, VALUE s, VALUE e) {
275
350
  buf_t b = get_buf(str);
@@ -298,7 +373,21 @@ static c0_body body_of(buf_t b, VALUE s, VALUE e) {
298
373
  return body;
299
374
  }
300
375
 
301
- /* Files of the body at [s, e): [name_start, name_end, start, stop] each (name trimmed). */
376
+ /* Parse [s, e) as a body (no SYN, heading or EOT), applying the whole grammar
377
+ * to it: nil when well-formed, else a C0::SyntaxError (position in the buffer). */
378
+ static VALUE ext_body_read(VALUE self, VALUE str, VALUE s, VALUE e) {
379
+ buf_t b = get_buf(str);
380
+ long start = NUM2LONG(s), stop = NUM2LONG(e);
381
+ c0_body body;
382
+ c0_error err;
383
+ (void)self;
384
+ check_range(b, start, stop);
385
+ if (c0_body_read(b.p + start, (size_t)(stop - start), &body, &err) != C0_OK) raise_error_at(&err, start);
386
+ return Qnil;
387
+ }
388
+
389
+ /* Files of the body at [s, e): [name_start, name_end, start, stop, leading?]
390
+ * each (name trimmed; the unnamed file before the first FS is leading). */
302
391
  static VALUE ext_files(VALUE self, VALUE str, VALUE s, VALUE e) {
303
392
  buf_t b = get_buf(str);
304
393
  c0_file_iter it = c0_body_files(body_of(b, s, e));
@@ -308,8 +397,8 @@ static VALUE ext_files(VALUE self, VALUE str, VALUE s, VALUE e) {
308
397
  while (c0_next_file(&it, &f)) {
309
398
  c0_bytes n = c0_file_name(f);
310
399
  long ns = (long)(n.ptr - b.p);
311
- rb_ary_push(out, rb_ary_new3(4, LONG2NUM(ns), LONG2NUM(ns + (long)n.len), LONG2NUM((long)f.start),
312
- LONG2NUM((long)f.stop)));
400
+ rb_ary_push(out, rb_ary_new3(5, LONG2NUM(ns), LONG2NUM(ns + (long)n.len), LONG2NUM((long)f.start),
401
+ LONG2NUM((long)f.stop), f.leading ? Qtrue : Qfalse));
313
402
  }
314
403
  if (it.err.code) raise_error(&it.err);
315
404
  return out;
@@ -376,14 +465,15 @@ static VALUE ext_body_bare(VALUE self, VALUE str, VALUE s, VALUE e) {
376
465
  /* ---- documents ---------------------------------------------------------- */
377
466
 
378
467
  static VALUE document_array(const c0_document *d) {
379
- return rb_ary_new3(9, LONG2NUM((long)d->start), LONG2NUM((long)d->stop), d->has_syn ? Qtrue : Qfalse,
468
+ return rb_ary_new3(10, LONG2NUM((long)d->start), LONG2NUM((long)d->stop), d->has_syn ? Qtrue : Qfalse,
380
469
  d->has_eot ? Qtrue : Qfalse, LONG2NUM((long)d->heading_start),
381
470
  LONG2NUM((long)d->heading_end), LONG2NUM((long)d->body.start),
382
- LONG2NUM((long)d->body.stop), UINT2NUM(d->warnings));
471
+ LONG2NUM((long)d->body.stop), UINT2NUM(d->warnings), LONG2NUM((long)d->dead_elements));
383
472
  }
384
473
 
385
- /* The document at `start`: [start, stop, syn?, eot?, heading_start,
386
- * heading_end, body_start, body_stop, warning_bits]. */
474
+ /* The document at `start`, the whole grammar applied: [start, stop, syn?,
475
+ * eot?, heading_start, heading_end, body_start, body_stop, warning_bits,
476
+ * dead_elements]. */
387
477
  static VALUE ext_document(VALUE self, VALUE str, VALUE start) {
388
478
  buf_t b = get_buf(str);
389
479
  c0_document d;
@@ -409,7 +499,7 @@ static VALUE ext_documents(VALUE self, VALUE str) {
409
499
 
410
500
  /* ---- streams ------------------------------------------------------------ */
411
501
 
412
- /* [committed_end, torn?, [[start, end, damaged?], ...]] */
502
+ /* [committed_end, torn?, [[start, end, damaged?], ...], first_damaged | nil, resume_offset | nil] */
413
503
  static VALUE ext_stream(VALUE self, VALUE str) {
414
504
  buf_t b = get_buf(str);
415
505
  c0_stream s = c0_stream_read(b.p, b.len);
@@ -422,10 +512,13 @@ static VALUE ext_stream(VALUE self, VALUE str) {
422
512
  long bs = (long)(blk.ptr - b.p);
423
513
  rb_ary_push(blocks, rb_ary_new3(3, LONG2NUM(bs), LONG2NUM(bs + (long)blk.len), damaged ? Qtrue : Qfalse));
424
514
  }
425
- return rb_ary_new3(3, LONG2NUM((long)s.committed_end), s.torn ? Qtrue : Qfalse, blocks);
515
+ return rb_ary_new3(5, LONG2NUM((long)s.committed_end), s.torn ? Qtrue : Qfalse, blocks,
516
+ s.has_damaged ? LONG2NUM((long)s.first_damaged) : Qnil,
517
+ s.has_damaged ? LONG2NUM((long)s.resume_offset) : Qnil);
426
518
  }
427
519
 
428
- /* The undamaged committed blocks, ETB markers and payloads removed. */
520
+ /* The undamaged committed blocks, ETB markers and payloads removed,
521
+ * continuing past every damaged block. */
429
522
  static VALUE ext_stream_undamaged(VALUE self, VALUE str) {
430
523
  buf_t b = get_buf(str);
431
524
  c0_stream s = c0_stream_read(b.p, b.len);
@@ -436,6 +529,17 @@ static VALUE ext_stream_undamaged(VALUE self, VALUE str) {
436
529
  return rb_enc_associate(out, rb_ascii8bit_encoding());
437
530
  }
438
531
 
532
+ /* The same, stopping at the first damaged block (the default reading). */
533
+ static VALUE ext_stream_before_damage(VALUE self, VALUE str) {
534
+ buf_t b = get_buf(str);
535
+ c0_stream s = c0_stream_read(b.p, b.len);
536
+ VALUE out = rb_str_buf_new((long)s.committed_end + 1);
537
+ size_t n = c0_stream_before_damage(&s, (uint8_t *)RSTRING_PTR(out));
538
+ (void)self;
539
+ rb_str_set_len(out, (long)n);
540
+ return rb_enc_associate(out, rb_ascii8bit_encoding());
541
+ }
542
+
439
543
  static VALUE ext_last_commit_end(VALUE self, VALUE str) {
440
544
  buf_t b = get_buf(str);
441
545
  (void)self;
@@ -581,18 +685,11 @@ static VALUE builder_depth(VALUE self) { return LONG2NUM((long)get_builder(self)
581
685
 
582
686
  /* -- primitives: one method per code -- */
583
687
 
584
- /* A reference path: one to three non-empty names, as c0_bytes backed by *tmp. */
585
- static c0_bytes *path_arg(VALUE path, VALUE *tmp, long *count) {
586
- c0_bytes *arr;
587
- long i;
588
- if (RARRAY_LEN(path) < 1 || RARRAY_LEN(path) > 3) {
589
- rb_raise(eBuildError, "a reference is a group name or a path of two or three segments");
590
- }
591
- arr = bytes_array(path, 1, "reference segment", tmp, count);
592
- for (i = 0; i < *count; i++) {
593
- if (arr[i].len == 0) rb_raise(eBuildError, "reference segment must not be empty");
594
- }
595
- return arr;
688
+ /* A non-empty name (a reference needs one). */
689
+ static c0_bytes ref_name_arg(VALUE v, const char *what) {
690
+ c0_bytes n = name_arg(v, what);
691
+ if (n.len == 0) rb_raise(eBuildError, "%s must not be empty", what);
692
+ return n;
596
693
  }
597
694
 
598
695
  /* fs(name): FS and a file name; ends any open record. */
@@ -604,16 +701,35 @@ static VALUE builder_fs(VALUE self, VALUE name) {
604
701
  return self;
605
702
  }
606
703
 
607
- /* gs(name = nil): GS and, when given, a group name. gs.gs("sub") is depth 2. */
704
+ /* A depth: keyword from an options hash (nil when absent); must be >= min. */
705
+ static long depth_opt(VALUE opts, long dflt, long min) {
706
+ ID kw = rb_intern("depth");
707
+ VALUE v;
708
+ long d;
709
+ if (NIL_P(opts)) return dflt;
710
+ rb_get_kwargs(opts, &kw, 0, 1, &v);
711
+ if (v == Qundef || NIL_P(v)) return dflt;
712
+ d = NUM2LONG(v);
713
+ if (d < min) rb_raise(rb_eArgError, "depth must be at least %ld", min);
714
+ return d;
715
+ }
716
+
717
+ /* gs(name = nil, depth: 1): depth-1 SUB, then GS and, when given, a group
718
+ * name: gs("s", depth: 2) writes ␚␝s, a subsection. A bare gs is an
719
+ * empty-named group (␝␝b is group "" then group b, not a depth step). */
608
720
  static VALUE builder_gs(int argc, VALUE *argv, VALUE self) {
609
721
  c0_builder *b = get_builder(self);
610
- VALUE name;
611
- rb_scan_args(argc, argv, "01", &name);
722
+ VALUE name, opts;
723
+ long d, i;
724
+ rb_scan_args(argc, argv, "01:", &name, &opts);
725
+ d = depth_opt(opts, 1, 1);
612
726
  if (NIL_P(name)) {
727
+ c0__b_end_record(b); /* the prefix is part of the group line: it ends the record too */
728
+ for (i = 1; i < d; i++) c0_builder_sub(b);
613
729
  c0_builder_gs(b);
614
730
  } else {
615
731
  c0_bytes n = name_arg(name, "group name");
616
- c0_builder_gs_name(b, n.ptr, n.len);
732
+ c0_build_section(b, n.ptr, n.len, (size_t)d);
617
733
  }
618
734
  builder_check(b);
619
735
  return self;
@@ -649,19 +765,12 @@ static VALUE builder_us(int argc, VALUE *argv, VALUE self) {
649
765
  return self;
650
766
  }
651
767
 
652
- /* sub(value = nil): SUB and, when given, the new side of a substitution. C0-DIFF
653
- * only: rs("old").sub("new") is a replace-all; in a data body SUB is an error.
654
- * Does not count as a unit. */
655
- static VALUE builder_sub(int argc, VALUE *argv, VALUE self) {
768
+ /* sub: SUB alone. The element after the next separator is subordinate to the
769
+ * one before it, at that code's grain: before GS a subsection (gs("a").sub.gs("b")
770
+ * writes ␝a␚␝b), before US a sub-unit (rs("old").sub.us("new") writes ␞old␚␟new). */
771
+ static VALUE builder_sub(VALUE self) {
656
772
  c0_builder *b = get_builder(self);
657
- VALUE value;
658
- rb_scan_args(argc, argv, "01", &value);
659
- if (NIL_P(value)) {
660
- c0_builder_sub(b);
661
- } else {
662
- c0_bytes v = bytes_arg(value);
663
- c0_builder_sub_value(b, v.ptr, v.len);
664
- }
773
+ c0_builder_sub(b);
665
774
  builder_check(b);
666
775
  return self;
667
776
  }
@@ -714,14 +823,24 @@ static VALUE builder_bel(int argc, VALUE *argv, VALUE self) {
714
823
  return self;
715
824
  }
716
825
 
717
- /* enq(name) or enq(group, record[, field]): ENQ and a reference. */
826
+ static VALUE builder_in_level(VALUE self);
827
+
828
+ /* enq(name): ENQ and a group name, a plain reference. enq { ... }: ENQ DLE, the
829
+ * pattern's lines written in the block, CAN (closed even on a raise). */
718
830
  static VALUE builder_enq(int argc, VALUE *argv, VALUE self) {
719
831
  c0_builder *b = get_builder(self);
720
- VALUE tmp;
721
- long n;
722
- c0_bytes *arr = path_arg(splat_or_array(argc, argv), &tmp, &n);
723
- c0_builder_enq_path(b, arr, (size_t)n);
724
- RB_GC_GUARD(tmp);
832
+ VALUE name;
833
+ rb_scan_args(argc, argv, "01", &name);
834
+ if (NIL_P(name)) {
835
+ if (!rb_block_given_p()) rb_raise(rb_eArgError, "enq needs a group name or a block writing a pattern");
836
+ c0_builder_enq_dle(b);
837
+ return builder_in_level(self);
838
+ }
839
+ if (rb_block_given_p()) rb_raise(rb_eArgError, "enq takes a group name or a block, not both");
840
+ {
841
+ c0_bytes n = ref_name_arg(name, "reference name");
842
+ c0_builder_enq(b, n.ptr, n.len);
843
+ }
725
844
  builder_check(b);
726
845
  return self;
727
846
  }
@@ -801,7 +920,7 @@ static VALUE builder_header_ary(VALUE self, VALUE names) {
801
920
  return self;
802
921
  }
803
922
 
804
- /* group(name, headers = nil, depth = 1) { ... }: GS×depth name, then a header row when given. */
923
+ /* group(name, headers = nil, depth = 1) { ... }: SUB×(depth-1) GS name, then a header row when given. */
805
924
  static VALUE builder_group(int argc, VALUE *argv, VALUE self) {
806
925
  c0_builder *b = get_builder(self);
807
926
  VALUE name, headers, depth;
@@ -955,14 +1074,49 @@ static VALUE builder_list(int argc, VALUE *argv, VALUE self) {
955
1074
  return self;
956
1075
  }
957
1076
 
958
- /* ref(name) or ref(group, record[, field]): a reference as the next unit. */
1077
+ /* ref(group): a group reference (␅group) as the next unit.
1078
+ * ref(group, record[, field]): the pattern ␅␐␝group␞record[␟field]␘.
1079
+ * ref { ... }: a pattern whose lines the block writes (␅␐ … ␘). */
959
1080
  static VALUE builder_ref(int argc, VALUE *argv, VALUE self) {
960
1081
  c0_builder *b = get_builder(self);
961
- VALUE tmp;
962
- long n;
963
- c0_bytes *arr = path_arg(splat_or_array(argc, argv), &tmp, &n);
964
- c0_build_ref_path(b, arr, (size_t)n);
965
- RB_GC_GUARD(tmp);
1082
+ VALUE group, record, field;
1083
+ rb_scan_args(argc, argv, "03", &group, &record, &field);
1084
+ if (NIL_P(group)) {
1085
+ if (!rb_block_given_p()) rb_raise(rb_eArgError, "ref needs a group name or a block writing a pattern");
1086
+ c0_build_ref_open(b);
1087
+ return builder_in_level(self);
1088
+ }
1089
+ if (rb_block_given_p()) rb_raise(rb_eArgError, "ref takes names or a block, not both");
1090
+ if (NIL_P(record)) {
1091
+ c0_bytes g = ref_name_arg(group, "reference name");
1092
+ if (!NIL_P(field)) rb_raise(rb_eArgError, "ref(group, record, field): a field needs a record");
1093
+ c0_build_ref(b, g.ptr, g.len);
1094
+ } else {
1095
+ c0_bytes g = ref_name_arg(group, "reference group");
1096
+ c0_bytes r = ref_name_arg(record, "reference record id");
1097
+ if (NIL_P(field)) {
1098
+ c0_build_ref_record(b, g.ptr, g.len, r.ptr, r.len, NULL, 0);
1099
+ } else {
1100
+ c0_bytes f = ref_name_arg(field, "reference field");
1101
+ c0_build_ref_record(b, g.ptr, g.len, r.ptr, r.len, f.ptr, f.len);
1102
+ }
1103
+ }
1104
+ builder_check(b);
1105
+ return self;
1106
+ }
1107
+
1108
+ /* sub_unit(value = nil, depth: 1): depth SUB, US and the value: a sub-unit
1109
+ * of the unit just written (depth 2: of the sub-unit before). Not a unit of
1110
+ * the record; opens a record when none is open (␞␚␟new). */
1111
+ static VALUE builder_sub_unit(int argc, VALUE *argv, VALUE self) {
1112
+ c0_builder *b = get_builder(self);
1113
+ VALUE value, opts;
1114
+ c0_bytes v;
1115
+ long d;
1116
+ rb_scan_args(argc, argv, "01:", &value, &opts);
1117
+ d = depth_opt(opts, 1, 1);
1118
+ v = NIL_P(value) ? c0_bytes_of(NULL, 0) : bytes_arg(value);
1119
+ c0_build_sub_unit(b, v.ptr, v.len, (size_t)d);
966
1120
  builder_check(b);
967
1121
  return self;
968
1122
  }
@@ -977,6 +1131,128 @@ static VALUE builder_note(VALUE self, VALUE text) {
977
1131
  return self;
978
1132
  }
979
1133
 
1134
+ /* ---- C0DIFF (c0_diff.h) ------------------------------------------------- */
1135
+
1136
+ static const char *diff_status_message(c0_diff_status st) {
1137
+ switch (st) {
1138
+ case C0_DIFF_SYNTAX: return "the diff is not well-formed C0DATA";
1139
+ case C0_DIFF_WRONG_SHAPE: return "heading declares a shape other than diff";
1140
+ case C0_DIFF_CONSTRAINT: return "the diff holds something a diff may not";
1141
+ case C0_DIFF_DUPLICATE_PATH: return "the same path twice";
1142
+ case C0_DIFF_FILE_NOT_FOUND: return "no such file";
1143
+ case C0_DIFF_EMPTY_SEARCH: return "empty search text";
1144
+ case C0_DIFF_PATTERN_NOT_FOUND: return "pattern not found";
1145
+ case C0_DIFF_PATTERN_AMBIGUOUS: return "pattern matched more than once";
1146
+ default: return "diff error";
1147
+ }
1148
+ }
1149
+
1150
+ /* Raise for a parse refusal: a syntax error keeps its class and position. */
1151
+ static void raise_diff_parse(c0_diff_status st, const c0_error *err) {
1152
+ if (st == C0_DIFF_OOM) rb_memerror();
1153
+ if (st == C0_DIFF_SYNTAX) {
1154
+ c0_error e = *err;
1155
+ if (e.code == C0_OK) e.code = C0_ERR_SYNTAX;
1156
+ raise_error(&e);
1157
+ }
1158
+ rb_raise(eDiffError, "%s", err->msg && *err->msg ? err->msg : diff_status_message(st));
1159
+ }
1160
+
1161
+ /* diff_parse(buf): [[path, [[[old, new | nil], ...], ...]], ...] — the files
1162
+ * of the diff, each with its records, each record its units (new nil for an
1163
+ * anchor). Refuses everything DIFF.md lists. */
1164
+ static VALUE ext_diff_parse(VALUE self, VALUE str) {
1165
+ buf_t b = get_buf(str);
1166
+ c0_diff d = c0_diff_parse(b.p, b.len);
1167
+ VALUE files;
1168
+ size_t i, j, k;
1169
+ (void)self;
1170
+ if (d.status != C0_DIFF_OK) {
1171
+ c0_diff_status st = d.status;
1172
+ c0_error err = d.err;
1173
+ c0_diff_free(&d);
1174
+ raise_diff_parse(st, &err);
1175
+ }
1176
+ files = rb_ary_new_capa((long)d.nfiles);
1177
+ for (i = 0; i < d.nfiles; i++) {
1178
+ const c0_diff_file *f = &d.files[i];
1179
+ VALUE records = rb_ary_new_capa((long)f->nrecords);
1180
+ for (j = 0; j < f->nrecords; j++) {
1181
+ const c0_diff_record *r = &f->records[j];
1182
+ VALUE units = rb_ary_new_capa((long)r->nunits);
1183
+ for (k = 0; k < r->nunits; k++) {
1184
+ const c0_diff_unit *u = &r->units[k];
1185
+ rb_ary_push(units, rb_ary_new3(2, bin_str(u->old, u->old_len),
1186
+ u->is_sub ? bin_str(u->new_, u->new_len) : Qnil));
1187
+ }
1188
+ rb_ary_push(records, units);
1189
+ }
1190
+ rb_ary_push(files, rb_ary_new3(2, bin_str(f->path, f->path_len), records));
1191
+ }
1192
+ c0_diff_free(&d);
1193
+ return files;
1194
+ }
1195
+
1196
+ static void raise_diff_apply(const c0_diff_error *err) {
1197
+ const char *what = diff_status_message(err->status);
1198
+ switch (err->status) {
1199
+ case C0_DIFF_OOM: rb_memerror();
1200
+ case C0_DIFF_SYNTAX: {
1201
+ c0_error e;
1202
+ e.code = C0_ERR_SYNTAX;
1203
+ e.pos = err->pos;
1204
+ e.msg = err->msg;
1205
+ raise_error(&e);
1206
+ }
1207
+ case C0_DIFF_WRONG_SHAPE:
1208
+ case C0_DIFF_CONSTRAINT:
1209
+ rb_raise(eDiffError, "%s", err->msg && *err->msg ? err->msg : what);
1210
+ case C0_DIFF_DUPLICATE_PATH:
1211
+ case C0_DIFF_FILE_NOT_FOUND:
1212
+ rb_raise(eDiffError, "%s: %s", what, err->path);
1213
+ default:
1214
+ rb_raise(eDiffError, "%s in %s (record %ld)", what, err->path, (long)err->record);
1215
+ }
1216
+ }
1217
+
1218
+ /* diff_apply(buf, [[path, content], ...]): [[path, content], ...] — every
1219
+ * input file in input order, edited or copied unchanged. Validates everything
1220
+ * first; raises without producing anything on any error. Paths and contents
1221
+ * must be Strings (the Ruby layer coerces them). */
1222
+ static VALUE ext_diff_apply(VALUE self, VALUE str, VALUE files) {
1223
+ buf_t b = get_buf(str);
1224
+ long n, i;
1225
+ VALUE tmp, result;
1226
+ c0_diff_file_input *in;
1227
+ c0_diff_file_output *out = NULL;
1228
+ size_t count = 0;
1229
+ c0_diff_error err;
1230
+ (void)self;
1231
+ files = rb_convert_type(files, T_ARRAY, "Array", "to_ary");
1232
+ n = RARRAY_LEN(files);
1233
+ tmp = rb_str_tmp_new((long)((n > 0 ? n : 1) * sizeof(c0_diff_file_input)));
1234
+ in = (c0_diff_file_input *)RSTRING_PTR(tmp);
1235
+ for (i = 0; i < n; i++) {
1236
+ VALUE pair = rb_convert_type(RARRAY_AREF(files, i), T_ARRAY, "Array", "to_ary");
1237
+ if (RARRAY_LEN(pair) != 2) rb_raise(rb_eArgError, "diff_apply wants [path, content] pairs");
1238
+ in[i].path = bytes_arg(RARRAY_AREF(pair, 0));
1239
+ in[i].content = bytes_arg(RARRAY_AREF(pair, 1));
1240
+ }
1241
+ if (!c0_diff_apply(b.p, b.len, in, (size_t)n, &out, &count, &err)) {
1242
+ RB_GC_GUARD(tmp);
1243
+ RB_GC_GUARD(files);
1244
+ raise_diff_apply(&err);
1245
+ }
1246
+ result = rb_ary_new_capa((long)count);
1247
+ for (i = 0; i < (long)count; i++) {
1248
+ rb_ary_push(result, rb_ary_new3(2, bin_str(out[i].path, out[i].path_len), bin_str(out[i].content, out[i].content_len)));
1249
+ }
1250
+ c0_diff_free_outputs(out, count);
1251
+ RB_GC_GUARD(tmp);
1252
+ RB_GC_GUARD(files);
1253
+ return result;
1254
+ }
1255
+
980
1256
  /* ---- init --------------------------------------------------------------- */
981
1257
 
982
1258
  void Init_c0_ext(void) {
@@ -992,6 +1268,8 @@ void Init_c0_ext(void) {
992
1268
  eKeyError = rb_define_class_under(mC0, "KeyError", eError);
993
1269
  eBuildError = rb_define_class_under(mC0, "BuildError", eError);
994
1270
  eGlyphInDataError = rb_define_class_under(mPretty, "GlyphInDataError", eError);
1271
+ mDiff = rb_define_module_under(mC0, "Diff");
1272
+ eDiffError = rb_define_class_under(mDiff, "Error", eError);
995
1273
 
996
1274
  rb_define_const(mC0, "VERSION", rb_str_freeze(rb_utf8_str_new_cstr(C0_VERSION)));
997
1275
 
@@ -1009,7 +1287,10 @@ void Init_c0_ext(void) {
1009
1287
  rb_define_module_function(mExt, "unit_list", ext_unit_list, 3);
1010
1288
  rb_define_module_function(mExt, "record_keyed", ext_record_keyed, 3);
1011
1289
  rb_define_module_function(mExt, "record_units", ext_record_units, 3);
1290
+ rb_define_module_function(mExt, "record_units_with_subs", ext_record_units_with_subs, 3);
1291
+ rb_define_module_function(mExt, "record_pairs_with_subs", ext_record_pairs_with_subs, 3);
1012
1292
  rb_define_module_function(mExt, "record_pairs", ext_record_pairs, 3);
1293
+ rb_define_module_function(mExt, "body_read", ext_body_read, 3);
1013
1294
  rb_define_module_function(mExt, "files", ext_files, 3);
1014
1295
  rb_define_module_function(mExt, "groups", ext_groups, 3);
1015
1296
  rb_define_module_function(mExt, "group_header", ext_group_header, 3);
@@ -1019,9 +1300,12 @@ void Init_c0_ext(void) {
1019
1300
  rb_define_module_function(mExt, "documents", ext_documents, 1);
1020
1301
  rb_define_module_function(mExt, "stream", ext_stream, 1);
1021
1302
  rb_define_module_function(mExt, "stream_undamaged", ext_stream_undamaged, 1);
1303
+ rb_define_module_function(mExt, "stream_before_damage", ext_stream_before_damage, 1);
1022
1304
  rb_define_module_function(mExt, "last_commit_end", ext_last_commit_end, 1);
1023
1305
  rb_define_module_function(mExt, "pretty_format", ext_pretty_format, 3);
1024
1306
  rb_define_module_function(mExt, "pretty_parse", ext_pretty_parse, 1);
1307
+ rb_define_module_function(mExt, "diff_parse", ext_diff_parse, 1);
1308
+ rb_define_module_function(mExt, "diff_apply", ext_diff_apply, 2);
1025
1309
 
1026
1310
  cBuilder = rb_define_class_under(mC0, "Builder", rb_cObject);
1027
1311
  rb_define_alloc_func(cBuilder, builder_alloc);
@@ -1033,7 +1317,7 @@ void Init_c0_ext(void) {
1033
1317
  rb_define_method(cBuilder, "gs", builder_gs, -1);
1034
1318
  rb_define_method(cBuilder, "rs", builder_rs, -1);
1035
1319
  rb_define_method(cBuilder, "us", builder_us, -1);
1036
- rb_define_method(cBuilder, "sub", builder_sub, -1);
1320
+ rb_define_method(cBuilder, "sub", builder_sub, 0);
1037
1321
  rb_define_method(cBuilder, "soh", builder_soh, -1);
1038
1322
  rb_define_method(cBuilder, "dle", builder_dle, 0);
1039
1323
  rb_define_method(cBuilder, "can", builder_can, 0);
@@ -1057,5 +1341,6 @@ void Init_c0_ext(void) {
1057
1341
  rb_define_method(cBuilder, "level", builder_level, 0);
1058
1342
  rb_define_method(cBuilder, "list", builder_list, -1);
1059
1343
  rb_define_method(cBuilder, "ref", builder_ref, -1);
1344
+ rb_define_method(cBuilder, "sub_unit", builder_sub_unit, -1);
1060
1345
  rb_define_method(cBuilder, "note", builder_note, 1);
1061
1346
  }
data/ext/c0/extconf.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "mkmf"
2
2
 
3
- # c0.h lives in the c0-c submodule at the repository root.
3
+ # c0.h, c0_diff.h / c0_diff.c and c0_strbuf.h live in the c0-c submodule at
4
+ # the repository root; c0_diff_impl.c here includes c0_diff.c.
4
5
  $INCFLAGS << " -I#{File.expand_path('../../c0-c', __dir__)}"
5
6
  $CFLAGS << " -Wall -Wextra"
6
7