Skip to content

Commit 17edc84

Browse files
committed
fix number parse
1 parent 8c073dd commit 17edc84

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/json_mend/parser.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def parse_object
112112
# --- End Key Parsing ---
113113

114114
# Handle duplicate keys by rolling back and injecting a new object opening
115-
if @context.include?(:array) && object.key?(key)
115+
if context_contain?(:array) && object.key?(key)
116116
# Convert the character-based rollback_index to a byte-based index for string manipulation.
117117
# We do this by taking the substring up to the character position and getting its byte length.
118118
byte_rollback_index = @scanner.string.chars[0...rollback_index].join.bytesize
@@ -383,7 +383,7 @@ def parse_string
383383
end
384384
end
385385

386-
if char == ']' && @context.include?(:array) && string_acc[-1] != rstring_delimiter
386+
if char == ']' && context_contain?(:array) && string_acc[-1] != rstring_delimiter
387387
i = skip_to_character(rstring_delimiter)
388388
# No delimiter found
389389
break unless peek_char(i)
@@ -484,9 +484,9 @@ def parse_string
484484
# This is because the routine after will make sure to correct any bad guess and this solves a corner case
485485
check_comma_in_object_value = false if check_comma_in_object_value && next_c.match?(/\p{L}/)
486486
# If we are in an object context, let's check for the right delimiters
487-
if (@context.include?(:object_key) && [':', '}'].include?(next_c)) ||
488-
(@context.include?(:object_value) && next_c == '}') ||
489-
(@context.include?(:array) && [']', ','].include?(next_c)) ||
487+
if (context_contain?(:object_key) && [':', '}'].include?(next_c)) ||
488+
(context_contain?(:object_value) && next_c == '}') ||
489+
(context_contain?(:array) && [']', ','].include?(next_c)) ||
490490
(
491491
check_comma_in_object_value &&
492492
current_context == :object_value &&
@@ -685,9 +685,9 @@ def parse_comment
685685
elsif @scanner.scan(%r{//|#})
686686
# Determine valid line comment termination characters based on context.
687687
termination_chars = ["\n", "\r"]
688-
termination_chars << ']' if @context.include?(:array)
689-
termination_chars << '}' if @context.include?(:object_value)
690-
termination_chars << ':' if @context.include?(:object_key)
688+
termination_chars << ']' if context_contain?(:array)
689+
termination_chars << '}' if context_contain?(:object_value)
690+
termination_chars << ':' if context_contain?(:object_key)
691691

692692
# Create a regex that will scan until it hits one of the terminators.
693693
# The terminators are positive lookaheads, so they aren't consumed by the scan.
@@ -793,12 +793,15 @@ def skip_whitespaces
793793
def peek_char(offset = 0)
794794
# Peeks the next character without advancing the scanner
795795
rest_of_string = @scanner.rest
796-
characters = rest_of_string.chars
797-
characters[offset]
796+
rest_of_string.chars[offset]
798797
end
799798

800799
def current_context
801800
@context&.last
802801
end
802+
803+
def context_contain?(value)
804+
@context.include?(value)
805+
end
803806
end
804807
end

0 commit comments

Comments
 (0)