diff_match_patch_es 2.0.0 → 3.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49966ab58af40ac9cb914af9464c7ef51f9d99e3c5e7d031b00d0d16a8e72646
4
- data.tar.gz: de0d632f1833ff01e32793458f65545e57e892b08f87d13879028331c6063909
3
+ metadata.gz: b2a6896eb175014ca6eb997a1e4b43737ad607aee256c6759d845d82e4944f1c
4
+ data.tar.gz: b9d422de8c9436e125f3ba8af418ef00f64badcfd9b3a2d1d523de59d55e273a
5
5
  SHA512:
6
- metadata.gz: '064728afac4b40eb29a51516f7a341832e6f6ac45e79d7faf4676ac62515b5c17b99b9da96d86b748f4a41b2b5009aac7b57c3f7819d97f2bc4b2b51655dfce6'
7
- data.tar.gz: 0c835902b2f68d6c502b7c6eaaa251fdc699e965da8ddbf34fa7f33831c576cc71b83f72b59984a815e6cd29f1043942c84684574f0fa5634b564934e7d7a641
6
+ metadata.gz: 89136eca4c222a0d7dfe8418d1876667acb5409de5360d2b279ec152a183633a31f576327e27b72e9ba37c3c194efdeabed7d13a5f42549d67c72f3428bd1e60
7
+ data.tar.gz: e1bc4413ba5c02de6efeeb76d1d410c7409f9edf37b0e7df773c8e6c43b29ba91364e43bac8847fc4a41da89e2cbf1be54519b96d3945f3c60376fa98cd5b79a
data/README.md CHANGED
@@ -10,6 +10,10 @@ require 'diff_match_patch_es'
10
10
  diffs = DiffMatchPatchES.diff_main('Apples are a fruit.', 'Bananas are also fruit.')
11
11
  # => [[-1, "Apple"], [1, "Banana"], [0, "s are a"], [1, "lso"], [0, " fruit."]]
12
12
 
13
+ diffs = DiffMatchPatchES.diff_main('mouse', 'sofas')
14
+ DiffMatchPatchES.diff_cleanup_semantic!(diffs)
15
+ diffs # => [[-1, "mouse"], [1, "sofas"]]
16
+
13
17
  patches = DiffMatchPatchES.patch_make('The quick brown fox.', 'The slow green fox.')
14
18
  text = DiffMatchPatchES.patch_to_text(patches)
15
19
  DiffMatchPatchES.patch_apply(DiffMatchPatchES.patch_from_text(text), 'The quick brown fox.')
@@ -19,6 +23,7 @@ DiffMatchPatchES.match_main('abcdefghijk', 'fgh', 5) # => 5
19
23
  ```
20
24
 
21
25
  Every exported function of the JS package has a snake_case equivalent (`diffMain` -> `diff_main`, `patchApply` -> `patch_apply`, …), plus the short aliases `diff`, `match`, and `patch`.
26
+ Functions that mutate an argument in place additionally end in a bang: `diff_cleanup_semantic!`, `diff_cleanup_efficiency!`, `diff_cleanup_merge!`, `diff_cleanup_semantic_lossless!`, `diff_chars_to_lines!`, `patch_add_context!`, `patch_add_padding!`, and `patch_split_max!`.
22
27
  Diffs are `[op, text]` arrays with the ops `DIFF_DELETE` (-1), `DIFF_EQUAL` (0), and `DIFF_INSERT` (1); patches are `DiffMatchPatchES::Patch` objects whose `#to_s` emits the GNU-diff-style format.
23
28
 
24
29
  Options are passed as a trailing hash (or a resolved `DiffMatchPatchES::Options`), mirroring the JS option names:
@@ -22,6 +22,7 @@ module DiffMatchPatchES
22
22
  def now_ms
23
23
  Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_millisecond)
24
24
  end
25
+ private_class_method :now_ms
25
26
 
26
27
  # Find the differences between two texts.
27
28
  def diff_main(text1, text2, options = nil, checklines = true, deadline = nil)
@@ -62,7 +63,7 @@ module DiffMatchPatchES
62
63
  diffs.unshift(create_diff(DIFF_EQUAL, commonprefix)) unless commonprefix.empty?
63
64
  diffs.push(create_diff(DIFF_EQUAL, commonsuffix)) unless commonsuffix.empty?
64
65
 
65
- diff_cleanup_merge(diffs)
66
+ diff_cleanup_merge!(diffs)
66
67
  diffs
67
68
  end
68
69
 
@@ -126,9 +127,9 @@ module DiffMatchPatchES
126
127
  diffs = diff_main(text1, text2, options, false, deadline)
127
128
 
128
129
  # Convert the diff back to original text.
129
- diff_chars_to_lines(diffs, linearray)
130
+ diff_chars_to_lines!(diffs, linearray)
130
131
  # Eliminate freak matches (e.g. blank lines)
131
- diff_cleanup_semantic(diffs)
132
+ diff_cleanup_semantic!(diffs)
132
133
 
133
134
  # Re-diff any replacement blocks, this time character-by-character.
134
135
  # Add a dummy entry at the end.
@@ -177,6 +178,8 @@ module DiffMatchPatchES
177
178
  # Cache the text lengths to prevent multiple calls.
178
179
  text1_length = text1.length
179
180
  text2_length = text2.length
181
+ # Avoid allocations by comparing codepoints below instead of strings
182
+ units1, units2 = diff_bisect_units(text1, text2)
180
183
  max_d = (text1_length + text2_length + 1) / 2
181
184
  v_offset = max_d
182
185
  v_length = 2 * max_d
@@ -209,9 +212,21 @@ module DiffMatchPatchES
209
212
  v1[k1_offset - 1] + 1
210
213
  end
211
214
  y1 = x1 - k1
212
- while x1 < text1_length && y1 < text2_length && text1[x1] == text2[y1]
215
+ # A unit that differs right away is the common case, so the run probe sits behind the first match.
216
+ if x1 < text1_length && y1 < text2_length && units1[x1] == units2[y1]
213
217
  x1 += 1
214
218
  y1 += 1
219
+ probe = x1 + 32
220
+ while x1 < text1_length && y1 < text2_length && units1[x1] == units2[y1]
221
+ x1 += 1
222
+ y1 += 1
223
+ next unless x1 == probe
224
+
225
+ # A long run: skip ahead in blocks, then finish one unit at a time.
226
+ run = diff_bisect_run(units1, units2, x1, y1, false)
227
+ x1 += run
228
+ y1 += run
229
+ end
215
230
  end
216
231
  v1[k1_offset] = x1
217
232
  if x1 > text1_length
@@ -244,10 +259,21 @@ module DiffMatchPatchES
244
259
  v2[k2_offset - 1] + 1
245
260
  end
246
261
  y2 = x2 - k2
247
- while x2 < text1_length && y2 < text2_length &&
248
- text1[text1_length - x2 - 1] == text2[text2_length - y2 - 1]
262
+ if x2 < text1_length && y2 < text2_length &&
263
+ units1[text1_length - x2 - 1] == units2[text2_length - y2 - 1]
249
264
  x2 += 1
250
265
  y2 += 1
266
+ probe = x2 + 32
267
+ while x2 < text1_length && y2 < text2_length &&
268
+ units1[text1_length - x2 - 1] == units2[text2_length - y2 - 1]
269
+ x2 += 1
270
+ y2 += 1
271
+ next unless x2 == probe
272
+
273
+ run = diff_bisect_run(units1, units2, text1_length - x2, text2_length - y2, true)
274
+ x2 += run
275
+ y2 += run
276
+ end
251
277
  end
252
278
  v2[k2_offset] = x2
253
279
  if x2 > text1_length
@@ -293,15 +319,51 @@ module DiffMatchPatchES
293
319
  diffs.concat(diffsb)
294
320
  end
295
321
 
296
- def line_char_for(index)
297
- codepoint = index < 0xD800 ? index : index + LINE_INDEX_SURROGATE_SHIFT
298
- codepoint.chr(Encoding::UTF_8)
322
+ def diff_bisect_units(text1, text2)
323
+ if text1.valid_encoding? && text2.valid_encoding?
324
+ [text1.codepoints, text2.codepoints]
325
+ else
326
+ [text1.chars, text2.chars]
327
+ end
328
+ end
329
+ private_class_method :diff_bisect_units
330
+
331
+ # Counts units known to match, forwards from at1 / at2 or backwards ending just before them, up to 62 short of the
332
+ # first mismatch. Compares array slices, which share memory, in blocks that double until one differs, then halve.
333
+ def diff_bisect_run(units1, units2, at1, at2, back)
334
+ left1 = back ? at1 : units1.length - at1
335
+ left2 = back ? at2 : units2.length - at2
336
+ max = left1 < left2 ? left1 : left2
337
+ count = 0
338
+ # Literal widths: YJIT in Ruby 3.4+ stops running compiled code at a constant first read in a rarely run branch.
339
+ width = 64
340
+ grow = true
341
+ # While growing, width is the next block. Once a block differs, it is the span that holds the first mismatch.
342
+ while count < max && (grow || width >= 64)
343
+ width = max - count if width > max - count
344
+ block = grow ? width : width / 2
345
+ skip = back ? -count - block : count
346
+ if units1[at1 + skip, block] == units2[at2 + skip, block]
347
+ count += block
348
+ width = grow ? width * 2 : width - block
349
+ else
350
+ width = block
351
+ grow = false
352
+ end
353
+ end
354
+ count
355
+ end
356
+ private_class_method :diff_bisect_run
357
+
358
+ def line_codepoint_for(index)
359
+ index < 0xD800 ? index : index + LINE_INDEX_SURROGATE_SHIFT
299
360
  end
361
+ private_class_method :line_codepoint_for
300
362
 
301
- def line_index_for(char)
302
- codepoint = char.ord
363
+ def line_index_for(codepoint)
303
364
  codepoint < 0xD800 ? codepoint : codepoint - LINE_INDEX_SURROGATE_SHIFT
304
365
  end
366
+ private_class_method :line_index_for
305
367
 
306
368
  # Split two texts into an array of strings. Reduce the texts to a string of
307
369
  # placeholder characters where each character represents one line.
@@ -319,25 +381,32 @@ module DiffMatchPatchES
319
381
  munge = lambda do |text|
320
382
  chars = +''
321
383
  # Walk the text, pulling out a substring for each line.
384
+ text_size = text.bytesize
385
+ # byteindex refuses an offset it considers mid-character, which just
386
+ # after a "\n" can only happen when the next byte is a stray UTF-8
387
+ # continuation byte in text with invalid sequences; search a binary
388
+ # view of such text instead (byteslice has no such check).
389
+ haystack = !text.valid_encoding? && text.encoding.ascii_compatible? ? text.b : text
322
390
  line_start = 0
323
391
  line_end = -1
324
392
  line_array_length = line_array.length
325
- while line_end < text.length - 1
326
- line_end = text.index("\n", line_start) || -1
327
- line_end = text.length - 1 if line_end == -1
393
+ while line_end < text_size - 1
394
+ line_end = haystack.byteindex("\n", line_start) || -1
395
+ line_end = text_size - 1 if line_end == -1
328
396
 
329
- line = JsString.substring(text, line_start, line_end + 1)
397
+ line = text.byteslice(line_start, line_end + 1 - line_start)
330
398
 
331
399
  if line_hash.key?(line)
332
- chars << line_char_for(line_hash[line])
400
+ # Appending an Integer encodes the code point without allocating a String.
401
+ chars << line_codepoint_for(line_hash[line])
333
402
  else
334
403
  if line_array_length == max_lines
335
404
  # Bail out at 65535 because
336
405
  # String.fromCharCode(65536) == String.fromCharCode(0)
337
- line = JsString.substring(text, line_start)
338
- line_end = text.length
406
+ line = text.byteslice(line_start, text_size - line_start)
407
+ line_end = text_size
339
408
  end
340
- chars << line_char_for(line_array_length)
409
+ chars << line_codepoint_for(line_array_length)
341
410
  line_hash[line] = line_array_length
342
411
  line_array[line_array_length] = line
343
412
  line_array_length += 1
@@ -355,11 +424,16 @@ module DiffMatchPatchES
355
424
 
356
425
  # Rehydrate the text in a diff from a string of line placeholders to real
357
426
  # lines of text.
358
- def diff_chars_to_lines(diffs, line_array)
427
+ def diff_chars_to_lines!(diffs, line_array)
359
428
  diffs.each do |diff|
360
429
  chars = diff[1]
361
430
  text = +''
362
- chars.each_char { |ch| text << line_array[line_index_for(ch)] }
431
+ if chars.valid_encoding?
432
+ # each_codepoint yields Integers. each_char allocates a String per placeholder.
433
+ chars.each_codepoint { |codepoint| text << line_array[line_index_for(codepoint)] }
434
+ else
435
+ chars.each_char { |ch| text << line_array[line_index_for(ch.ord)] }
436
+ end
363
437
  diff[1] = text
364
438
  end
365
439
  nil
@@ -367,6 +441,93 @@ module DiffMatchPatchES
367
441
 
368
442
  # Determine the common prefix of two strings.
369
443
  def diff_common_prefix(text1, text2)
444
+ # Quick check for common null cases.
445
+ return 0 if text1.empty? || text2.empty? || text1.getbyte(0) != text2.getbyte(0)
446
+ return diff_common_prefix_by_char(text1, text2) unless byte_comparable?(text1) && byte_comparable?(text2)
447
+
448
+ # Galloping search over bytes. The reference binary search first compares
449
+ # the whole shorter text, which copies it here (JS substrings share
450
+ # memory) and scans multi-byte text to find each character index.
451
+ # Doubling the probe width keeps the work proportional to the prefix
452
+ # actually shared.
453
+ size = text1.bytesize < text2.bytesize ? text1.bytesize : text2.bytesize
454
+ lo = 1 # Bytes [0, lo) are known to match.
455
+ hi = 1
456
+ step = 1
457
+ while lo < size
458
+ hi = lo + step
459
+ hi = size if hi > size
460
+ break unless text1.byteslice(lo, hi - lo) == text2.byteslice(lo, hi - lo)
461
+
462
+ lo = hi
463
+ step *= 2
464
+ end
465
+ if lo < size
466
+ # The first mismatch lies in [lo, hi); bisect to find it.
467
+ while hi - lo > 1
468
+ mid = (lo + hi) / 2
469
+ if text1.byteslice(lo, mid - lo) == text2.byteslice(lo, mid - lo)
470
+ lo = mid
471
+ else
472
+ hi = mid
473
+ end
474
+ end
475
+ # Back up to a character boundary (0b10xxxxxx continues a character).
476
+ lo -= 1 while (text1.getbyte(lo) & 0xC0) == 0x80
477
+ end
478
+ return lo if text1.ascii_only? || text2.ascii_only?
479
+
480
+ text1.byteslice(0, lo).length
481
+ end
482
+
483
+ # Determine the common suffix of two strings.
484
+ def diff_common_suffix(text1, text2)
485
+ # Quick check for common null cases.
486
+ return 0 if text1.empty? || text2.empty? || text1.getbyte(-1) != text2.getbyte(-1)
487
+ return diff_common_suffix_by_char(text1, text2) unless byte_comparable?(text1) && byte_comparable?(text2)
488
+
489
+ # Galloping search over bytes, as in diff_common_prefix.
490
+ size1 = text1.bytesize
491
+ size2 = text2.bytesize
492
+ size = size1 < size2 ? size1 : size2
493
+ lo = 1 # The last lo bytes are known to match.
494
+ hi = 1
495
+ step = 1
496
+ while lo < size
497
+ hi = lo + step
498
+ hi = size if hi > size
499
+ break unless text1.byteslice(size1 - hi, hi - lo) == text2.byteslice(size2 - hi, hi - lo)
500
+
501
+ lo = hi
502
+ step *= 2
503
+ end
504
+ if lo < size
505
+ while hi - lo > 1
506
+ mid = (lo + hi) / 2
507
+ if text1.byteslice(size1 - mid, mid - lo) == text2.byteslice(size2 - mid, mid - lo)
508
+ lo = mid
509
+ else
510
+ hi = mid
511
+ end
512
+ end
513
+ # Move forward to a character boundary.
514
+ lo -= 1 while lo.positive? && (text1.getbyte(size1 - lo) & 0xC0) == 0x80
515
+ end
516
+ return lo if text1.ascii_only? || text2.ascii_only?
517
+
518
+ text1.byteslice(size1 - lo, lo).length
519
+ end
520
+
521
+ # Whether comparing text byte by byte finds the same shared prefix/suffix
522
+ # as comparing it character by character: ASCII-only text in any encoding,
523
+ # or valid UTF-8, whose byte sequences are self-synchronising.
524
+ def byte_comparable?(text)
525
+ text.ascii_only? || (text.encoding == Encoding::UTF_8 && text.valid_encoding?)
526
+ end
527
+ private_class_method :byte_comparable?
528
+
529
+ # The reference implementation of diff_common_prefix.
530
+ def diff_common_prefix_by_char(text1, text2)
370
531
  # Quick check for common null cases.
371
532
  return 0 if text1.empty? || text2.empty? || text1[0] != text2[0]
372
533
 
@@ -388,9 +549,10 @@ module DiffMatchPatchES
388
549
  end
389
550
  pointermid
390
551
  end
552
+ private_class_method :diff_common_prefix_by_char
391
553
 
392
- # Determine the common suffix of two strings.
393
- def diff_common_suffix(text1, text2)
554
+ # The reference implementation of diff_common_suffix.
555
+ def diff_common_suffix_by_char(text1, text2)
394
556
  # Quick check for common null cases.
395
557
  return 0 if text1.empty? || text2.empty? || text1[-1] != text2[-1]
396
558
 
@@ -412,6 +574,7 @@ module DiffMatchPatchES
412
574
  end
413
575
  pointermid
414
576
  end
577
+ private_class_method :diff_common_suffix_by_char
415
578
 
416
579
  # Determine if the suffix of one string is the prefix of another.
417
580
  def diff_common_overlap(text1, text2)
@@ -513,7 +676,7 @@ module DiffMatchPatchES
513
676
  end
514
677
 
515
678
  # Reduce the number of edits by eliminating semantically trivial equalities.
516
- def diff_cleanup_semantic(diffs)
679
+ def diff_cleanup_semantic!(diffs)
517
680
  changes = false
518
681
  # Stack of indices where equalities are found. The reference
519
682
  # implementation lets the stack pointer go negative and stores into the
@@ -573,9 +736,9 @@ module DiffMatchPatchES
573
736
  end
574
737
 
575
738
  # Normalize the diff.
576
- diff_cleanup_merge(diffs) if changes
739
+ diff_cleanup_merge!(diffs) if changes
577
740
 
578
- diff_cleanup_semantic_lossless(diffs)
741
+ diff_cleanup_semantic_lossless!(diffs)
579
742
 
580
743
  # Find any overlaps between deletions and insertions.
581
744
  # e.g: <del>abcxxx</del><ins>xxxdef</ins>
@@ -615,7 +778,8 @@ module DiffMatchPatchES
615
778
  nil
616
779
  end
617
780
 
618
- NON_ALPHA_NUMERIC_REGEX = /[^a-z0-9]/i
781
+ # The reference is /[^a-z0-9]/i. Ruby's /i also folds U+017F and U+212A onto s and k, which JS does not.
782
+ NON_ALPHA_NUMERIC_REGEX = /[^a-zA-Z0-9]/
619
783
  # Matches the JS \s class (which the reference implementation uses).
620
784
  WHITESPACE_REGEX = /[\t\n\v\f\r \u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]/
621
785
  LINEBREAK_REGEX = /[\r\n]/
@@ -665,7 +829,7 @@ module DiffMatchPatchES
665
829
  # Look for single edits surrounded on both sides by equalities
666
830
  # which can be shifted sideways to align the edit to a word boundary.
667
831
  # e.g: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.
668
- def diff_cleanup_semantic_lossless(diffs)
832
+ def diff_cleanup_semantic_lossless!(diffs)
669
833
  pointer = 1
670
834
  # Intentionally ignore the first and last element (don't need checking).
671
835
  while pointer < diffs.length - 1
@@ -728,12 +892,12 @@ module DiffMatchPatchES
728
892
  end
729
893
 
730
894
  # Reduce the number of edits by eliminating operationally trivial equalities.
731
- def diff_cleanup_efficiency(diffs, options = nil)
895
+ def diff_cleanup_efficiency!(diffs, options = nil)
732
896
  diff_edit_cost = resolve_options(options).diff_edit_cost
733
897
 
734
898
  changes = false
735
899
  # Stack of indices where equalities are found (Hash for the same
736
- # negative-index reason as in diff_cleanup_semantic).
900
+ # negative-index reason as in diff_cleanup_semantic!).
737
901
  equalities = {}
738
902
  equalities_length = 0
739
903
  last_equality = nil
@@ -800,13 +964,13 @@ module DiffMatchPatchES
800
964
  pointer += 1
801
965
  end
802
966
 
803
- diff_cleanup_merge(diffs) if changes
967
+ diff_cleanup_merge!(diffs) if changes
804
968
  nil
805
969
  end
806
970
 
807
971
  # Reorder and merge like edit sections. Merge equalities.
808
972
  # Any edit section can move as long as it doesn't cross an equality.
809
- def diff_cleanup_merge(diffs)
973
+ def diff_cleanup_merge!(diffs)
810
974
  # Add a dummy entry at the end.
811
975
  diffs.push(create_diff(DIFF_EQUAL, ''))
812
976
  pointer = 0
@@ -906,7 +1070,7 @@ module DiffMatchPatchES
906
1070
  pointer += 1
907
1071
  end
908
1072
  # If shifts were made, the diff needs reordering and another shift sweep.
909
- diff_cleanup_merge(diffs) if changes
1073
+ diff_cleanup_merge!(diffs) if changes
910
1074
  nil
911
1075
  end
912
1076
 
@@ -7,6 +7,15 @@ module DiffMatchPatchES
7
7
 
8
8
  # JS String.prototype.substring: negative/overflowing indices clamp to [0, length] and the arguments swap when start > end.
9
9
  def substring(str, start_index, end_index = nil)
10
+ # Common case first: String#[] clamps the end itself, so String#length
11
+ # (a scan of multi-byte text) is only needed for the odd cases below.
12
+ size = str.bytesize
13
+ if start_index.between?(0, size)
14
+ count = end_index.nil? || end_index >= size ? size : end_index - start_index
15
+ result = str[start_index, count] if count >= 0
16
+ return result if result
17
+ end
18
+
10
19
  len = str.length
11
20
  a = start_index.clamp(0, len)
12
21
  b = end_index.nil? ? len : end_index.clamp(0, len)
@@ -23,16 +32,64 @@ module DiffMatchPatchES
23
32
 
24
33
  # JS String.prototype.indexOf.
25
34
  def index_of(str, pattern, from = 0)
35
+ # String#index counts the characters of +str+ to validate an offset,
36
+ # so a search from the start passes none.
37
+ return str.index(pattern) || -1 unless from.positive?
38
+
39
+ # It also walks multi-byte text to the offset one character at a time.
40
+ # Slicing off the tail is much faster, and the slice shares the text.
41
+ if !str.ascii_only? && str.encoding == pattern.encoding && from <= str.bytesize &&
42
+ (tail = str[from, str.bytesize])
43
+ found = tail.index(pattern)
44
+ return found ? found + from : -1
45
+ end
46
+
26
47
  from = from.clamp(0, str.length)
27
48
  str.index(pattern, from) || -1
28
49
  end
29
50
 
30
51
  # JS String.prototype.lastIndexOf: the match may start at or before +from+.
31
52
  def last_index_of(str, pattern, from)
53
+ # String#rindex can read past the end of the text
54
+ # (https://bugs.ruby-lang.org/issues/22548), and it walks multi-byte text
55
+ # to its start one character at a time. Search by byte where that is exact.
56
+ # In ASCII-only text a character offset is a byte offset.
57
+ return str.byterindex(pattern, from.clamp(0, str.bytesize)) || -1 if str.ascii_only?
58
+
59
+ if str.encoding == Encoding::UTF_8 && pattern.encoding == Encoding::UTF_8 && str.valid_encoding?
60
+ size = str.bytesize
61
+ from = from.clamp(0, size)
62
+ tail = str[from, size] # nil when +from+ is past the last character.
63
+ limit = tail ? size - tail.bytesize : size
64
+ found = str.byterindex(pattern, limit)
65
+ return -1 unless found
66
+
67
+ # Turn the byte offset into a character index by counting the
68
+ # characters on the shorter side of the match.
69
+ return str.byteslice(0, found).length if found < limit - found
70
+
71
+ return (tail ? from : str.length) - str.byteslice(found, limit - found).length
72
+ end
73
+
32
74
  from = from.clamp(0, str.length)
33
75
  str.rindex(pattern, from) || -1
34
76
  end
35
77
 
78
+ # lastIndexOf, where a match that starts before +first+ counts as none.
79
+ def last_index_between(str, pattern, first, from)
80
+ # Every match that counts lies within one slice of the text, so search
81
+ # only that slice. Kept to the cases last_index_of searches by byte.
82
+ if first.is_a?(Integer) && from.is_a?(Integer) && first.between?(1, from) && !pattern.empty? &&
83
+ str.encoding == pattern.encoding &&
84
+ (str.ascii_only? || (str.encoding == Encoding::UTF_8 && str.valid_encoding?))
85
+ found = last_index_of(substring(str, first, from + pattern.length), pattern, from - first)
86
+ return found == -1 ? -1 : found + first
87
+ end
88
+
89
+ found = last_index_of(str, pattern, from)
90
+ found < first ? -1 : found
91
+ end
92
+
36
93
  # JS Number.parseInt(str, 10): optional leading whitespace and sign,
37
94
  # JS StrWhiteSpace: ASCII whitespace, NBSP, BOM, and Unicode space separators.
38
95
  JS_PARSE_INT = /\A[\t\n\v\f\r \u00A0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]*([+-]?\d+)/
@@ -45,13 +102,44 @@ module DiffMatchPatchES
45
102
 
46
103
  # Characters encodeURI leaves unescaped.
47
104
  ENCODE_URI_UNESCAPED = /\A[A-Za-z0-9;,\/?:@&=+$\-_.!~*'()#]\z/
105
+ # Runs of characters encodeURI escapes, other than the space.
106
+ ENCODE_URI_ESCAPED = /[^A-Za-z0-9;,\/?:@&=+$\-_.!~*'()# ]+/
107
+ # The %XX escape of each byte value.
108
+ BYTE_ESCAPES = (0..255).map { |byte| format('%%%02X', byte).freeze }.freeze
48
109
  # Characters whose escape sequences decodeURI refuses to decode
49
110
  # (uriReserved plus '#').
50
111
  DECODE_URI_RESERVED = ";/?:@&=+$,#"
112
+ # Runs of escaped bytes outside ASCII, which decode together as UTF-8.
113
+ DECODE_URI_WIDE = /(?:%[89A-Fa-f]\h)+/
114
+ # A '%' that does not begin the escape of an ASCII byte.
115
+ DECODE_URI_MALFORMED = /%(?![0-7]\h)/
116
+ # The escape of an ASCII byte, and what each one decodes to: its character,
117
+ # or the escape itself when the character is reserved.
118
+ DECODE_URI_ASCII = /%[0-7]\h/
119
+ DECODE_URI_ASCII_ESCAPES = [*'0'..'7'].product([*'0'..'9', *'A'..'F', *'a'..'f']).to_h do |digits|
120
+ escape = "%#{digits.join}".freeze
121
+ char = digits.join.hex.chr.freeze
122
+ [escape, DECODE_URI_RESERVED.include?(char) ? escape : char]
123
+ end.freeze
51
124
 
52
125
  # JS encodeURI: percent-encodes the UTF-8 bytes of every character outside
53
126
  # the unescaped set, using uppercase hex.
54
127
  def encode_uri(str)
128
+ # Texts of a few bytes are quicker through the loop below.
129
+ if str.bytesize > 5 && str.valid_encoding? && str.encoding.ascii_compatible?
130
+ # One gsub keeps the scan in C. Spaces, the most common escape, go last
131
+ # through a plain-string gsub, which costs far less per match.
132
+ escaped = str.gsub(ENCODE_URI_ESCAPED) do |run|
133
+ next BYTE_ESCAPES[run.getbyte(0)] if run.bytesize == 1
134
+
135
+ hex = +''
136
+ run.each_byte { |byte| hex << BYTE_ESCAPES[byte] }
137
+ hex
138
+ end
139
+ escaped.gsub!(' ', '%20')
140
+ return escaped.force_encoding(Encoding::UTF_8)
141
+ end
142
+
55
143
  out = +''
56
144
  str.each_char do |ch|
57
145
  if ENCODE_URI_UNESCAPED.match?(ch)
@@ -69,6 +157,22 @@ module DiffMatchPatchES
69
157
  # malformed, overlong, surrogate, and out-of-range sequences) while leaving
70
158
  # escapes of reserved characters (";/?:@&=+$,#") untouched.
71
159
  def decode_uri(str)
160
+ # Texts of a few bytes are quicker through the loop below.
161
+ if str.bytesize > 6 && (str.encoding == Encoding::UTF_8 ? str.valid_encoding? : str.ascii_only?)
162
+ # Multi-byte characters first: decoding them cannot produce a '%' for
163
+ # the next pass to misread, where decoding %25 can. Ruby's UTF-8
164
+ # validity rules are decodeURI's.
165
+ decoded = str.gsub(DECODE_URI_WIDE) do |run|
166
+ chars = [run.delete('%')].pack('H*').force_encoding(Encoding::UTF_8)
167
+ raise UriError, 'URI malformed' unless chars.valid_encoding?
168
+
169
+ chars
170
+ end
171
+ raise UriError, 'URI malformed' if decoded.match?(DECODE_URI_MALFORMED)
172
+
173
+ return decoded.gsub(DECODE_URI_ASCII, DECODE_URI_ASCII_ESCAPES).force_encoding(Encoding::UTF_8)
174
+ end
175
+
72
176
  out = +''
73
177
  i = 0
74
178
  n = str.length
@@ -59,7 +59,10 @@ module DiffMatchPatchES
59
59
  if best_loc != -1
60
60
  score_threshold = [bitap_score.call(0, best_loc), score_threshold].min
61
61
  # What about in the other direction? (speedup)
62
- best_loc = JsString.last_index_of(text, pattern, loc + pattern.length)
62
+ # Only a match closer to loc than the one above can lower the threshold,
63
+ # unless match_distance is negative.
64
+ first = (resolved.match_distance <=> 0) == -1 ? 0 : (2 * loc) - best_loc
65
+ best_loc = JsString.last_index_between(text, pattern, first, loc + pattern.length)
63
66
  score_threshold = [bitap_score.call(0, best_loc), score_threshold].min if best_loc != -1
64
67
  end
65
68
 
@@ -89,11 +92,29 @@ module DiffMatchPatchES
89
92
  start = [1, loc - bin_mid + 1].max
90
93
  finish = [loc + bin_mid, text.length].min + pattern.length
91
94
 
95
+ if d.zero?
96
+ # Later passes scan inside this first window, and a match above loc
97
+ # lowers start by at most pattern.length. Convert the window to code
98
+ # points once: text[j - 1] allocates a String per step and scans
99
+ # multi-byte text from its start.
100
+ units_offset = [1, start - pattern.length].max - 1
101
+ window = JsString.substring(text, units_offset, finish)
102
+ if window.valid_encoding? && pattern.valid_encoding?
103
+ units = window.codepoints
104
+ units_table = s.transform_keys(&:ord)
105
+ else
106
+ # Invalid byte sequences have no code points, so we compare characters.
107
+ units = window.chars
108
+ units_table = s
109
+ end
110
+ end
111
+
92
112
  rd = Array.new(finish + 2, 0)
93
113
  rd[finish + 1] = ((1 << d) - 1) & MASK32
94
114
  j = finish
95
115
  while j >= start
96
- char_match = s[JsString.char_at(text, j - 1)] || 0
116
+ # Past the end of the text nothing matches, as with charAt.
117
+ char_match = units_table[units[j - 1 - units_offset]] || 0
97
118
  rd[j] = if d.zero? # First pass: exact match.
98
119
  (((rd[j + 1] << 1) | 1) & char_match) & MASK32
99
120
  else # Subsequent passes: fuzzy match.
@@ -39,6 +39,15 @@ module DiffMatchPatchES
39
39
  def ==(other)
40
40
  other.is_a?(Options) && to_h == other.to_h
41
41
  end
42
+
43
+ # Stricter than +#==+ in the way +Hash#eql?+ is: 1 and 1.0 are not eql?.
44
+ def eql?(other)
45
+ other.is_a?(Options) && to_h.eql?(other.to_h)
46
+ end
47
+
48
+ def hash
49
+ to_h.hash
50
+ end
42
51
  end
43
52
 
44
53
  DEFAULT_OPTIONS = Options.new.freeze
@@ -63,7 +63,7 @@ module DiffMatchPatchES
63
63
 
64
64
  # Increase the context until it is unique,
65
65
  # but don't let the pattern expand beyond match_max_bits.
66
- def patch_add_context(patch, text, options)
66
+ def patch_add_context!(patch, text, options)
67
67
  return if text.empty?
68
68
 
69
69
  raise Error, 'patch not initialized' if patch.start2.nil?
@@ -77,7 +77,7 @@ module DiffMatchPatchES
77
77
 
78
78
  # Look for the first and last matches of pattern in text. If two
79
79
  # different matches are found, increase the pattern length.
80
- while JsString.index_of(text, pattern) != JsString.last_index_of(text, pattern, text.length) &&
80
+ while patch_context_repeated?(text, pattern) &&
81
81
  pattern.length < match_max_bits - patch_margin - patch_margin
82
82
  padding += patch_margin
83
83
  pattern = JsString.substring(text, patch.start2 - padding, patch.start2 + patch.length1 + padding)
@@ -102,6 +102,20 @@ module DiffMatchPatchES
102
102
  nil
103
103
  end
104
104
 
105
+ # Whether pattern occurs at two different places in text, which JS tests
106
+ # with indexOf != lastIndexOf. Byte offsets tell two matches apart as well
107
+ # as character indices do and skip the scans that compute those. Only for
108
+ # UTF-8: in encodings such as Shift_JIS a byte search can match inside a
109
+ # character.
110
+ def patch_context_repeated?(text, pattern)
111
+ if text.encoding == Encoding::UTF_8
112
+ text.byteindex(pattern) != text.byterindex(pattern)
113
+ else
114
+ JsString.index_of(text, pattern) != JsString.last_index_of(text, pattern, text.length)
115
+ end
116
+ end
117
+ private_class_method :patch_context_repeated?
118
+
105
119
  # Compute a list of patches to turn text1 into text2.
106
120
  # Use diffs if provided, otherwise compute it ourselves.
107
121
  # There are four ways to call this function, depending on what data is
@@ -119,8 +133,8 @@ module DiffMatchPatchES
119
133
  text1 = a
120
134
  diffs = diff_main(text1, opt_b, resolved, true)
121
135
  if diffs.length > 2
122
- diff_cleanup_semantic(diffs)
123
- diff_cleanup_efficiency(diffs, resolved)
136
+ diff_cleanup_semantic!(diffs)
137
+ diff_cleanup_efficiency!(diffs, resolved)
124
138
  end
125
139
  elsif a.is_a?(Array) && opt_b.nil? && opt_c.nil?
126
140
  # Method 2: diffs
@@ -152,6 +166,13 @@ module DiffMatchPatchES
152
166
  # context info.
153
167
  prepatch_text = text1
154
168
  postpatch_text = text1
169
+ # Applying every diff to postpatch_text as it arrives copies the whole
170
+ # text per diff (JS ropes make that cheap). It is only read at a patch
171
+ # boundary, so when the encodings allow it the diffs since the last
172
+ # boundary are applied there in one pass.
173
+ one_pass = patch_uniform_encoding?(text1, diffs)
174
+ pending_from = 0 # First diff not yet applied to postpatch_text.
175
+ pending_offset = 0 # char_count2 at that diff.
155
176
  diffs.each_with_index do |diff, x|
156
177
  diff_type = diff[0]
157
178
  diff_text = diff[1]
@@ -167,14 +188,18 @@ module DiffMatchPatchES
167
188
  patch.diffs[patch_diff_length] = diff
168
189
  patch_diff_length += 1
169
190
  patch.length2 += diff_text.length
170
- postpatch_text = JsString.substring(postpatch_text, 0, char_count2) + diff_text +
171
- JsString.substring(postpatch_text, char_count2)
191
+ unless one_pass
192
+ postpatch_text = JsString.substring(postpatch_text, 0, char_count2) + diff_text +
193
+ JsString.substring(postpatch_text, char_count2)
194
+ end
172
195
  when DIFF_DELETE
173
196
  patch.length1 += diff_text.length
174
197
  patch.diffs[patch_diff_length] = diff
175
198
  patch_diff_length += 1
176
- postpatch_text = JsString.substring(postpatch_text, 0, char_count2) +
177
- JsString.substring(postpatch_text, char_count2 + diff_text.length)
199
+ unless one_pass
200
+ postpatch_text = JsString.substring(postpatch_text, 0, char_count2) +
201
+ JsString.substring(postpatch_text, char_count2 + diff_text.length)
202
+ end
178
203
  when DIFF_EQUAL
179
204
  if diff_text.length <= 2 * resolved.patch_margin &&
180
205
  patch_diff_length.positive? && diffs.length != x + 1
@@ -186,7 +211,7 @@ module DiffMatchPatchES
186
211
  elsif diff_text.length >= 2 * resolved.patch_margin
187
212
  # Time for a new patch.
188
213
  if patch_diff_length.positive?
189
- patch_add_context(patch, prepatch_text, resolved)
214
+ patch_add_context!(patch, prepatch_text, resolved)
190
215
  patches.push(patch)
191
216
  patch = Patch.new
192
217
  patch_diff_length = 0
@@ -194,6 +219,11 @@ module DiffMatchPatchES
194
219
  # https://github.com/google/diff-match-patch/wiki/Unidiff
195
220
  # Update prepatch text & pos to reflect the application of the
196
221
  # just completed patch.
222
+ if one_pass
223
+ postpatch_text = patch_apply_pending(postpatch_text, pending_offset, diffs, pending_from, x)
224
+ pending_from = x
225
+ pending_offset = char_count2
226
+ end
197
227
  prepatch_text = postpatch_text
198
228
  char_count1 = char_count2
199
229
  end
@@ -206,13 +236,45 @@ module DiffMatchPatchES
206
236
  end
207
237
  # Pick up the leftover patch if not empty.
208
238
  if patch_diff_length.positive?
209
- patch_add_context(patch, prepatch_text, resolved)
239
+ patch_add_context!(patch, prepatch_text, resolved)
210
240
  patches.push(patch)
211
241
  end
212
242
 
213
243
  patches
214
244
  end
215
245
 
246
+ # Whether text and every diff text are valid in one shared encoding, so
247
+ # that joining and slicing them by character behaves as JS strings do.
248
+ def patch_uniform_encoding?(text, diffs)
249
+ encoding = text.encoding
250
+ text.valid_encoding? && diffs.all? { |(_, data)| data.encoding == encoding && data.valid_encoding? }
251
+ end
252
+ private_class_method :patch_uniform_encoding?
253
+
254
+ # Apply diffs[from...to] to text, where they start at character offset.
255
+ # Gives the same text as applying them one by one with JS substring,
256
+ # including its clamping of positions past the end of the text.
257
+ def patch_apply_pending(text, offset, diffs, from, to)
258
+ result = text[0, offset]
259
+ # Slicing the remainder from its own start keeps each step proportional
260
+ # to the diff, and a tail slice shares the text's buffer. (byteslice
261
+ # would lose the cached validity of multi-byte text and force a rescan.)
262
+ rest = text[offset..] || ''
263
+ (from...to).each do |index|
264
+ op, data = diffs[index]
265
+ if op == DIFF_INSERT
266
+ result << data
267
+ else
268
+ # Consume the next data.length characters; a deletion drops them.
269
+ length = data.length
270
+ result << rest[0, length] if op != DIFF_DELETE
271
+ rest = rest[length..] || ''
272
+ end
273
+ end
274
+ result << rest
275
+ end
276
+ private_class_method :patch_apply_pending
277
+
216
278
  # Given an array of patches, return another array that is identical.
217
279
  def patch_deep_copy(patches)
218
280
  patches.map do |patch|
@@ -235,10 +297,10 @@ module DiffMatchPatchES
235
297
  patches = patch_deep_copy(patches)
236
298
 
237
299
  resolved = resolve_options(options)
238
- null_padding = patch_add_padding(patches, resolved)
300
+ null_padding = patch_add_padding!(patches, resolved)
239
301
  text = null_padding + text + null_padding
240
302
 
241
- patch_split_max(patches, resolved)
303
+ patch_split_max!(patches, resolved)
242
304
  # delta keeps track of the offset between the expected and actual
243
305
  # location of the previous patch. If there are patches expected at
244
306
  # positions 10 and 20, but the first patch was found at 12, delta is 2
@@ -250,7 +312,7 @@ module DiffMatchPatchES
250
312
  text1 = diff_text1(patch.diffs)
251
313
  end_loc = -1
252
314
  if text1.length > resolved.match_max_bits
253
- # patch_split_max will only provide an oversized pattern in the case
315
+ # patch_split_max! will only provide an oversized pattern in the case
254
316
  # of a monster delete.
255
317
  start_loc = match_main(
256
318
  text,
@@ -302,7 +364,7 @@ module DiffMatchPatchES
302
364
  # The end points match, but the content is unacceptably bad.
303
365
  results[x] = false
304
366
  else
305
- diff_cleanup_semantic_lossless(diffs)
367
+ diff_cleanup_semantic_lossless!(diffs)
306
368
  index1 = 0
307
369
  index2 = 0
308
370
  patch.diffs.each do |mod|
@@ -328,7 +390,7 @@ module DiffMatchPatchES
328
390
  # Add some padding on text start and end so that edges can match something.
329
391
  # Intended to be called only from within patch_apply.
330
392
  # Returns the padding string added to each side.
331
- def patch_add_padding(patches, options = nil)
393
+ def patch_add_padding!(patches, options = nil)
332
394
  padding_length = resolve_options(options).patch_margin
333
395
  null_padding = +''
334
396
  (1..padding_length).each { |x| null_padding << x.chr(Encoding::UTF_8) }
@@ -381,7 +443,7 @@ module DiffMatchPatchES
381
443
  # Look through the patches and break up any which are longer than the
382
444
  # maximum limit of the match algorithm.
383
445
  # Intended to be called only from within patch_apply.
384
- def patch_split_max(patches, options = nil)
446
+ def patch_split_max!(patches, options = nil)
385
447
  resolved = resolve_options(options)
386
448
  x = 0
387
449
  while x < patches.length
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DiffMatchPatchES
4
- VERSION = '2.0.0'
4
+ VERSION = '3.0.1'
5
5
  end
@@ -22,7 +22,7 @@ module DiffMatchPatchES
22
22
 
23
23
  def self?.create_diff: (op operation, String text) -> diff
24
24
 
25
- def self?.now_ms: () -> Float
25
+ private def self.now_ms: () -> Float
26
26
 
27
27
  # Find the differences between two texts.
28
28
  def self?.diff_main: (String text1, String text2, ?options_arg options, ?bool checklines, ?Numeric? deadline) -> Array[diff]
@@ -42,16 +42,24 @@ module DiffMatchPatchES
42
42
  # Given the location of the 'middle snake', split the diff in two parts and recurse.
43
43
  def self?.diff_bisect_split: (String text1, String text2, Options options, Integer x, Integer y, Numeric deadline) -> Array[diff]
44
44
 
45
- def self?.line_char_for: (Integer index) -> String
45
+ # Both texts as Arrays of Integer code points (or of one-character Strings
46
+ # when either text has an invalid byte sequence) for diff_bisect's hot loops.
47
+ private def self.diff_bisect_units: (String text1, String text2) -> [Array[Integer] | Array[String], Array[Integer] | Array[String]]
46
48
 
47
- def self?.line_index_for: (String char) -> Integer
49
+ # A count of units known to match, forwards from at1 / at2 or backwards ending
50
+ # just before them. It can stop up to 62 units short of the first mismatch.
51
+ private def self.diff_bisect_run: (Array[Integer] | Array[String] units1, Array[Integer] | Array[String] units2, Integer at1, Integer at2, bool back) -> Integer
52
+
53
+ private def self.line_codepoint_for: (Integer index) -> Integer
54
+
55
+ private def self.line_index_for: (Integer codepoint) -> Integer
48
56
 
49
57
  # Split two texts into an array of strings.
50
58
  # Reduce the texts to a string of placeholder characters where each character represents one line.
51
59
  def self?.diff_lines_to_chars: (String text1, String text2) -> { chars1: String, chars2: String, line_array: Array[String] }
52
60
 
53
61
  # Rehydrate the text in a diff from a string of line placeholders to real lines of text. Mutates diffs in place.
54
- def self?.diff_chars_to_lines: (Array[diff] diffs, Array[String] line_array) -> nil
62
+ def self?.diff_chars_to_lines!: (Array[diff] diffs, Array[String] line_array) -> nil
55
63
 
56
64
  # Determine the common prefix of two strings.
57
65
  def self?.diff_common_prefix: (String text1, String text2) -> Integer
@@ -59,6 +67,15 @@ module DiffMatchPatchES
59
67
  # Determine the common suffix of two strings.
60
68
  def self?.diff_common_suffix: (String text1, String text2) -> Integer
61
69
 
70
+ # Whether comparing text byte by byte finds the same shared prefix/suffix as
71
+ # comparing it character by character (ASCII-only text or valid UTF-8).
72
+ private def self.byte_comparable?: (String text) -> bool
73
+
74
+ # The reference character-based implementations, used for any other text.
75
+ private def self.diff_common_prefix_by_char: (String text1, String text2) -> Integer
76
+
77
+ private def self.diff_common_suffix_by_char: (String text1, String text2) -> Integer
78
+
62
79
  # Determine if the suffix of one string is the prefix of another.
63
80
  def self?.diff_common_overlap: (String text1, String text2) -> Integer
64
81
 
@@ -69,7 +86,7 @@ module DiffMatchPatchES
69
86
 
70
87
  # Reduce the number of edits by eliminating semantically trivial
71
88
  # equalities. Mutates diffs in place.
72
- def self?.diff_cleanup_semantic: (Array[diff] diffs) -> nil
89
+ def self?.diff_cleanup_semantic!: (Array[diff] diffs) -> nil
73
90
 
74
91
  # Given two strings, compute a score representing whether the internal
75
92
  # boundary falls on logical boundaries. 6 (best) to 0 (worst).
@@ -77,14 +94,14 @@ module DiffMatchPatchES
77
94
 
78
95
  # Look for single edits surrounded on both sides by equalities which can be
79
96
  # shifted sideways to align the edit to a word boundary. Mutates diffs.
80
- def self?.diff_cleanup_semantic_lossless: (Array[diff] diffs) -> nil
97
+ def self?.diff_cleanup_semantic_lossless!: (Array[diff] diffs) -> nil
81
98
 
82
99
  # Reduce the number of edits by eliminating operationally trivial
83
100
  # equalities. Mutates diffs in place.
84
- def self?.diff_cleanup_efficiency: (Array[diff] diffs, ?options_arg options) -> nil
101
+ def self?.diff_cleanup_efficiency!: (Array[diff] diffs, ?options_arg options) -> nil
85
102
 
86
103
  # Reorder and merge like edit sections. Merge equalities. Mutates diffs.
87
- def self?.diff_cleanup_merge: (Array[diff] diffs) -> nil
104
+ def self?.diff_cleanup_merge!: (Array[diff] diffs) -> nil
88
105
 
89
106
  # loc is a location in text1, compute and return the equivalent location
90
107
  # in text2.
@@ -5,8 +5,20 @@ module DiffMatchPatchES
5
5
 
6
6
  ENCODE_URI_UNESCAPED: Regexp
7
7
 
8
+ ENCODE_URI_ESCAPED: Regexp
9
+
10
+ BYTE_ESCAPES: Array[String]
11
+
8
12
  DECODE_URI_RESERVED: String
9
13
 
14
+ DECODE_URI_WIDE: Regexp
15
+
16
+ DECODE_URI_MALFORMED: Regexp
17
+
18
+ DECODE_URI_ASCII: Regexp
19
+
20
+ DECODE_URI_ASCII_ESCAPES: Hash[String, String]
21
+
10
22
  class UriError < StandardError
11
23
  end
12
24
 
@@ -23,6 +35,9 @@ module DiffMatchPatchES
23
35
  # JS String.prototype.lastIndexOf: the match may start at or before +from+.
24
36
  def self?.last_index_of: (String str, String pattern, Integer from) -> Integer
25
37
 
38
+ # lastIndexOf, where a match that starts before +first+ counts as none.
39
+ def self?.last_index_between: (String str, String pattern, Integer first, Integer from) -> Integer
40
+
26
41
  # JS Number.parseInt(str, 10). Returns nil where JS returns NaN.
27
42
  def self?.parse_int: (String str) -> Integer?
28
43
 
@@ -16,19 +16,15 @@ module DiffMatchPatchES
16
16
 
17
17
  attr_accessor match_max_bits: Integer
18
18
 
19
- def initialize: (
20
- ?diff_timeout: Numeric,
21
- ?diff_edit_cost: Integer,
22
- ?match_threshold: Numeric,
23
- ?match_distance: Integer,
24
- ?patch_delete_threshold: Numeric,
25
- ?patch_margin: Integer,
26
- ?match_max_bits: Integer
27
- ) -> void
19
+ def initialize: (?diff_timeout: Numeric, ?diff_edit_cost: Integer, ?match_threshold: Numeric, ?match_distance: Integer, ?patch_delete_threshold: Numeric, ?patch_margin: Integer, ?match_max_bits: Integer) -> void
28
20
 
29
21
  def to_h: () -> Hash[Symbol, Numeric]
30
22
 
31
23
  def ==: (untyped other) -> bool
24
+
25
+ def eql?: (untyped other) -> bool
26
+
27
+ def hash: () -> Integer
32
28
  end
33
29
 
34
30
  DEFAULT_OPTIONS: Options
@@ -24,7 +24,10 @@ module DiffMatchPatchES
24
24
 
25
25
  # Increase the context until it is unique, but don't let the pattern
26
26
  # expand beyond match_max_bits. Mutates patch in place.
27
- def self?.patch_add_context: (Patch patch, String text, options_arg options) -> nil
27
+ def self?.patch_add_context!: (Patch patch, String text, options_arg options) -> nil
28
+
29
+ # Whether pattern occurs at two different places in text.
30
+ private def self.patch_context_repeated?: (String text, String pattern) -> bool
28
31
 
29
32
  # Compute a list of patches to turn text1 into text2.
30
33
  # Method 1: a = text1, b = text2
@@ -36,6 +39,13 @@ module DiffMatchPatchES
36
39
  | (String a, Array[diff] opt_b, ?nil opt_c, ?options_arg options) -> Array[Patch]
37
40
  | (String a, String opt_b, Array[diff] opt_c, ?options_arg options) -> Array[Patch]
38
41
 
42
+ # Whether text and every diff text are valid in one shared encoding.
43
+ private def self.patch_uniform_encoding?: (String text, Array[diff] diffs) -> bool
44
+
45
+ # The text that applying diffs[from...to] to text yields, where those diffs
46
+ # start at character offset.
47
+ private def self.patch_apply_pending: (String text, Integer offset, Array[diff] diffs, Integer from, Integer to) -> String
48
+
39
49
  # Given an array of patches, return another array that is identical.
40
50
  def self?.patch_deep_copy: (Array[Patch] patches) -> Array[Patch]
41
51
 
@@ -45,11 +55,11 @@ module DiffMatchPatchES
45
55
 
46
56
  # Add some padding on text start and end so that edges can match
47
57
  # something. Returns the padding string added to each side.
48
- def self?.patch_add_padding: (Array[Patch] patches, ?options_arg options) -> String
58
+ def self?.patch_add_padding!: (Array[Patch] patches, ?options_arg options) -> String
49
59
 
50
60
  # Look through the patches and break up any which are longer than the
51
61
  # maximum limit of the match algorithm. Mutates patches in place.
52
- def self?.patch_split_max: (Array[Patch] patches, ?options_arg options) -> nil
62
+ def self?.patch_split_max!: (Array[Patch] patches, ?options_arg options) -> nil
53
63
 
54
64
  # Take a list of patches and return a textual representation.
55
65
  def self?.patch_to_text: (Array[Patch] patches) -> String
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diff_match_patch_es
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil Fraser
@@ -51,7 +51,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
51
  - !ruby/object:Gem::Version
52
52
  version: '0'
53
53
  requirements: []
54
- rubygems_version: 4.0.10
54
+ rubygems_version: 3.6.9
55
55
  specification_version: 4
56
56
  summary: Thread-safe Ruby port of diff-match-patch-es
57
57
  test_files: []