json_mend 0.2.0 → 0.2.2

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: d35f7c9f36e19aad3676320c0d23913ee95f9e92d391587732baa6ab849aeb03
4
- data.tar.gz: fbf7a08168984cd2d8c5cd3efcdd93559df98c749d3c8b0dff5393d7b16b6817
3
+ metadata.gz: 132fa0fa5489f3d26c01bbb256ed9abbc2af84bf5e39da35d3ef2aa62d610154
4
+ data.tar.gz: 3dbf1c1d09ebfdd61a07e51465e3239d7f069de57853c26a540349638fdce4b6
5
5
  SHA512:
6
- metadata.gz: dd89121f4e73649f762d01e360838364f91f88cb0fe8f56ef158840d41f2c863e916c572625a913a90334332adeb2e2f8e4213f0962a26e34704616d4756b071
7
- data.tar.gz: fe6fd2bc78b8d25b7ee25f00148e8f65b75cdeeef5faadf45efd514d1463f793cf80390d2be00d5af1d4e5b2f5a211079e91f85682dd5dd050a5679d4542a274
6
+ metadata.gz: ce3494e2d0a6c35de7828051050a35a022ecba2659d93d814b455cab38f6bc7c3128ba2751789663e66bd19ac7a8598f63681489705578aa101d9ef543d3fa17
7
+ data.tar.gz: d469f040577069840fb06cfad141793c4a17d6e4113705205e3045ebe3d75669e0b5d190a7221fcc3a8909a5a4217cbb3ad37f9fd0a53d8a4fe855e744fcf528
@@ -787,7 +787,9 @@ module JsonMend
787
787
  bk = 1
788
788
  slashes = 0
789
789
  # Look back in the string buffer directly for speed
790
- while (char_code = @scanner.string.getbyte(@scanner.pos - 1 - bk)) && char_code == 92 # 92 is backslash
790
+ while (@scanner.pos - 1 - bk >= 0) &&
791
+ (char_code = @scanner.string.getbyte(@scanner.pos - 1 - bk)) &&
792
+ char_code == 92 # 92 is backslash
791
793
  slashes += 1
792
794
  bk += 1
793
795
  end
@@ -1063,6 +1065,9 @@ module JsonMend
1063
1065
  scanned_str = @scanner.scan(regex)
1064
1066
  return nil unless scanned_str
1065
1067
 
1068
+ # Save the original length so we can safely roll back if it's completely invalid
1069
+ original_length = scanned_str.bytesize
1070
+
1066
1071
  # Handle cases where the number ends with an invalid character.
1067
1072
  if !scanned_str.empty? && INVALID_NUMBER_TRAILERS.include?(scanned_str[-1])
1068
1073
  # Do not rewind scanner, simply discard the invalid trailing char (garbage)
@@ -1071,10 +1076,16 @@ module JsonMend
1071
1076
  # e.g. "123-abc"
1072
1077
  elsif peek_char&.match?(/\p{L}/)
1073
1078
  # Roll back the entire scan and re-parse as a string.
1074
- @scanner.pos -= scanned_str.bytesize
1079
+ @scanner.pos -= original_length
1075
1080
  return parse_string
1076
1081
  end
1077
1082
 
1083
+ # Reject non-numbers (e.g., stray periods "." or dashes "-" from LLM conversational text)
1084
+ unless scanned_str.match?(/\d/)
1085
+ @scanner.pos -= original_length
1086
+ return ''
1087
+ end
1088
+
1078
1089
  # Sometimes numbers are followed by a quote, which is garbage
1079
1090
  @scanner.getch if peek_char == '"'
1080
1091
 
@@ -1144,20 +1155,24 @@ module JsonMend
1144
1155
 
1145
1156
  if context_contain?(:object_key)
1146
1157
  # If parsing a key, we must stop at ':' and structural closers
1147
- @scanner.scan_until(/(?=[\n\r:}\]])/)
1158
+ @scanner.scan_until(/(?=[\n\r:}\]]|\\n|\\r)/) || @scanner.terminate
1148
1159
  elsif in_array && in_object
1149
1160
  # Nested ambiguity, stop at any closer
1150
- @scanner.scan_until(/(?=[\n\r}\]])/)
1161
+ @scanner.scan_until(/(?=[\n\r}\]]|\\n|\\r)/) || @scanner.terminate
1151
1162
  elsif in_array
1152
1163
  # Inside array, stop at ']'
1153
- @scanner.scan_until(/(?=[\n\r\]])/)
1164
+ @scanner.scan_until(/(?=[\n\r\]]|\\n|\\r)/) || @scanner.terminate
1154
1165
  elsif in_object
1155
1166
  # Inside object value, stop at '}'
1156
- @scanner.scan_until(/(?=[\n\r}])/)
1167
+ @scanner.scan_until(/(?=[\n\r}]|\\n|\\r)/) || @scanner.terminate
1157
1168
  else
1158
1169
  # Top level or neutral, stop at newline
1159
- @scanner.scan_until(/(?=[\n\r])/)
1170
+ @scanner.scan_until(/(?=[\n\r]|\\n|\\r)/) || @scanner.terminate
1160
1171
  end
1172
+
1173
+ # Consume literal escaped newlines so they don't break subsequent parsing.
1174
+ # (Real newlines will be left alone here and consumed normally by skip_whitespaces).
1175
+ @scanner.skip(/\\n|\\r/)
1161
1176
  else
1162
1177
  # The character at the current position (likely '/') is not the start of a
1163
1178
  # valid comment. To prevent an infinite loop in the calling parser, we must
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JsonMend
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_mend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleksii Vasyliev