Skip to content

Commit 6626ee4

Browse files
committed
refactor
1 parent e34d3b8 commit 6626ee4

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

lib/json_mend/parser.rb

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -597,33 +597,49 @@ def parse_literal
597597
# After skipping the comment, it either continues parsing if at the top level
598598
# or returns an empty string to be ignored by the caller.
599599
def parse_comment
600+
char = @scanner.peek(1)
601+
600602
# Determine valid line comment termination characters based on the current context.
601603
termination_chars = ["\n", "\r"]
602604
termination_chars << ']' if @context.include?(:array)
603605
termination_chars << '}' if @context.include?(:object_value)
604606
termination_chars << ':' if @context.include?(:object_key)
605-
line_comment_matcher = Regexp.new("[#{Regexp.escape(termination_chars.join)}]")
606607

607608
# Line comment starting with #
608-
if @scanner.skip(/#/)
609-
# Skip until the next terminator or the end of the string.
610-
found_terminator = @scanner.skip_until(line_comment_matcher)
611-
@scanner.terminate unless found_terminator
609+
if char == "#"
610+
comment = +""
611+
while !@scanner.eos? && !termination_chars.include?(char)
612+
comment << char
613+
@scanner.pos += 1
614+
char = @scanner.peek(1)
615+
end
612616
# Comments starting with /
613-
elsif @scanner.check(%r{/})
617+
elsif char == "/"
618+
next_char = @scanner.string[@scanner.pos + 1]
614619
# Handle line comment starting with //
615-
if @scanner.skip(%r{//})
616-
found_terminator = @scanner.skip_until(line_comment_matcher)
617-
@scanner.terminate unless found_terminator
618-
620+
if next_char == "/"
621+
comment = +"//"
622+
@scanner.pos += 2
623+
char = @scanner.peek(1)
624+
while !@scanner.eos? && !termination_chars.include?(char)
625+
comment << char
626+
@scanner.pos += 1
627+
char = @scanner.peek(1)
628+
end
619629
# Handle block comment starting with /*
620-
elsif @scanner.skip(%r{/\*})
621-
found_terminator = @scanner.skip_until(%r{\*/})
622-
@scanner.terminate unless found_terminator
630+
elsif next_char == "*"
631+
comment = "/*"
632+
@scanner.pos += 2
633+
loop do
634+
break if @scanner.eos?
623635

624-
# Handle standalone '/' characters that are not part of a comment.
636+
char = @scanner.peek(1)
637+
comment += char
638+
@scanner.pos += 1
639+
break if comment.end_with?("*/")
640+
end
625641
else
626-
@scanner.pos += 1 # Skip it to avoid an infinite loop.
642+
@scanner.pos += 1
627643
end
628644
end
629645

spec/json_mend_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
},
4747
{
4848
input: '{"key": "value\\nvalue"}',
49-
expected_output: JSON.dump({ key: 'value\\nvalue' })
49+
expected_output: JSON.dump({ key: "value\nvalue" })
5050
}
5151
].each do |test_case|
5252
it "repair #{test_case[:input]} to #{test_case[:expected_output]}" do
@@ -95,7 +95,7 @@
9595
},
9696
{
9797
input: '{"key": "value\\nvalue"}',
98-
expected_output: JSON.dump({ key: 'value\\nvalue' })
98+
expected_output: JSON.dump({ key: "value\nvalue" })
9999
}
100100
].each do |test_case|
101101
it "repair #{test_case[:input]} to #{test_case[:expected_output]}" do

0 commit comments

Comments
 (0)