From 0c906ce03bbf76abcec99165bbb99e5629db8b7a Mon Sep 17 00:00:00 2001 From: Sheldon Hearn Date: Mon, 28 Jul 2025 10:13:23 +0200 Subject: [PATCH 1/2] Use mutable strings as buffers Fixes dgraham/json-stream#9 --- lib/json/stream/buffer.rb | 2 +- lib/json/stream/parser.rb | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/json/stream/buffer.rb b/lib/json/stream/buffer.rb index cbefd8b..417ce43 100644 --- a/lib/json/stream/buffer.rb +++ b/lib/json/stream/buffer.rb @@ -29,7 +29,7 @@ def initialize def <<(data) # Avoid state machine for complete UTF-8. if @buffer.empty? - data.force_encoding(Encoding::UTF_8) + (+data).force_encoding(Encoding::UTF_8) return data if data.valid_encoding? end diff --git a/lib/json/stream/parser.rb b/lib/json/stream/parser.rb index 33e7925..fbf51f5 100644 --- a/lib/json/stream/parser.rb +++ b/lib/json/stream/parser.rb @@ -102,8 +102,8 @@ def initialize(&block) # Track parse stack. @stack = [] - @unicode = "" - @buf = "" + @unicode = +"" + @buf = +"" @pos = -1 # Register any observers in the block. @@ -178,7 +178,7 @@ def <<(data) @state = :end_key notify(:key, @buf) end - @buf = "" + @buf = +"" when BACKSLASH @state = :start_escape when CONTROL @@ -270,7 +270,7 @@ def <<(data) @buf << ch else end_value(@buf.to_i) - @buf = "" + @buf = +"" @pos -= 1 redo end @@ -291,7 +291,7 @@ def <<(data) @buf << ch else end_value(@buf.to_f) - @buf = "" + @buf = +"" @pos -= 1 redo end @@ -310,7 +310,7 @@ def <<(data) else error('Expected 0-9 digit') unless @buf =~ DIGIT_END end_value(@buf.to_f) - @buf = "" + @buf = +"" @pos -= 1 redo end @@ -326,7 +326,7 @@ def <<(data) @buf << ch else end_value(@buf.to_i) - @buf = "" + @buf = +"" @pos -= 1 redo end @@ -503,7 +503,7 @@ def keyword(word, value, re, ch) if @buf.size == word.size if @buf == word - @buf = "" + @buf = +"" end_value(value) else error("Expected #{word} keyword") From 831698fa5390f1b07409bc08c166d4472c7b5c02 Mon Sep 17 00:00:00 2001 From: Sheldon Hearn Date: Wed, 30 Jul 2025 09:46:12 +0200 Subject: [PATCH 2/2] Properly cope with partial bytes in buffer append --- lib/json/stream/buffer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/json/stream/buffer.rb b/lib/json/stream/buffer.rb index 417ce43..f9d9413 100644 --- a/lib/json/stream/buffer.rb +++ b/lib/json/stream/buffer.rb @@ -29,7 +29,7 @@ def initialize def <<(data) # Avoid state machine for complete UTF-8. if @buffer.empty? - (+data).force_encoding(Encoding::UTF_8) + data = (+data).force_encoding(Encoding::UTF_8) return data if data.valid_encoding? end