Skip to content

Commit 4ad731b

Browse files
committed
fix
1 parent ed75c78 commit 4ad731b

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

lib/json_mend/parser.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ class Parser
1010
COMMENT_DELIMETERS = ['#', '/'].freeze
1111
NUMBER_CHARS = Set.new('0123456789-.eE/,'.chars).freeze
1212
STRING_DELIMITERS = ['"', "'", '“', '”'].freeze
13+
ESCAPE_MAPPING = {
14+
't' => "\t",
15+
'n' => "\n",
16+
'r' => "\r",
17+
'b' => "\b"
18+
}.freeze
1319

1420
# Pre-compile regexes for performance
1521
NUMBER_REGEX = /[#{Regexp.escape(NUMBER_CHARS.to_a.join)}]+/
@@ -486,8 +492,7 @@ def parse_string
486492
# This is a special case, if people use real strings this might happen
487493
if [rstring_delimiter, 't', 'n', 'r', 'b', '\\'].include?(char)
488494
# OPTIMIZE: Modify string in place instead of creating new string objects
489-
escape_seqs = { 't' => "\t", 'n' => "\n", 'r' => "\r", 'b' => "\b" }
490-
string_acc.chop! << escape_seqs.fetch(char, char)
495+
string_acc.chop! << ESCAPE_MAPPING.fetch(char, char)
491496

492497
@scanner.getch # Consume the character
493498
char = peek_char
@@ -754,11 +759,7 @@ def parse_number
754759
# Attempt to convert the string to the appropriate number type.
755760
# Use rescue to handle conversion errors gracefully, returning the original string.
756761
begin
757-
if scanned_str.include?(')') # typo in original code? Assuming it meant comma?
758-
# The original code had scanned_str.include?(',') but the regex logic suggests we support it
759-
# However, the previous code block did `scanned_str.tr(',', '.')`
760-
Float(scanned_str.tr(',', '.'))
761-
elsif scanned_str.include?(',')
762+
if scanned_str.include?(',')
762763
Float(scanned_str.tr(',', '.'))
763764
elsif scanned_str.match?(/[.eE]/)
764765
Float(scanned_str)

0 commit comments

Comments
 (0)