Skip to content

Commit ab1816b

Browse files
committed
Remove Source#string= method
## Why? ruby#114 (comment) > I want to just change scan pointer (StringScanner#pos=) instead of changing @scanner.string.
1 parent 370666e commit ab1816b

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

lib/rexml/parsers/baseparser.rb

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ def pull_event
211211
#STDERR.puts @source.encoding
212212
#STDERR.puts "BUFFER = #{@source.buffer.inspect}"
213213
if @document_status == nil
214+
start_position = @source.position
214215
if @source.match("<?", true)
215-
return process_instruction
216+
return process_instruction(start_position)
216217
elsif @source.match("<!", true)
217218
if @source.match("--", true)
218219
return [ :comment, @source.match(/(.*?)-->/um, true)[1] ]
@@ -224,7 +225,7 @@ def pull_event
224225
else
225226
message = "#{base_error_message}: invalid name"
226227
end
227-
@source.string = "<!DOCTYPE" + @source.buffer
228+
@source.position = start_position
228229
raise REXML::ParseException.new(message, @source)
229230
end
230231
@nsstack.unshift(curr_ns=Set.new)
@@ -266,6 +267,7 @@ def pull_event
266267
end
267268
if @document_status == :in_doctype
268269
@source.match(/\s*/um, true) # skip spaces
270+
start_position = @source.position
269271
if @source.match("<!", true)
270272
if @source.match("ELEMENT", true)
271273
md = @source.match(/(.*?)>/um, true)
@@ -325,7 +327,7 @@ def pull_event
325327
else
326328
message = "#{base_error_message}: invalid name"
327329
end
328-
@source.string = " <!NOTATION" + @source.buffer
330+
@source.position = start_position
329331
raise REXML::ParseException.new(message, @source)
330332
end
331333
name = parse_name(base_error_message)
@@ -355,6 +357,7 @@ def pull_event
355357
@source.match(/\s*/um, true)
356358
end
357359
begin
360+
start_position = @source.position
358361
if @source.match("<", true)
359362
if @source.match("/", true)
360363
@nsstack.shift
@@ -367,7 +370,7 @@ def pull_event
367370
if md.nil? or last_tag != md[1]
368371
message = "Missing end tag for '#{last_tag}'"
369372
message += " (got '#{md[1]}')" if md
370-
@source.string = "</" + @source.buffer if md.nil?
373+
@source.position = start_position if md.nil?
371374
raise REXML::ParseException.new(message, @source)
372375
end
373376
return [ :end_element, last_tag ]
@@ -391,12 +394,12 @@ def pull_event
391394
raise REXML::ParseException.new( "Declarations can only occur "+
392395
"in the doctype declaration.", @source)
393396
elsif @source.match("?", true)
394-
return process_instruction
397+
return process_instruction(start_position)
395398
else
396399
# Get the next tag
397400
md = @source.match(TAG_PATTERN, true)
398401
unless md
399-
@source.string = "<" + @source.buffer
402+
@source.position = start_position
400403
raise REXML::ParseException.new("malformed XML: missing tag start", @source)
401404
end
402405
tag = md[1]
@@ -578,11 +581,11 @@ def parse_id_invalid_details(accept_external_id:,
578581
end
579582
end
580583

581-
def process_instruction
584+
def process_instruction(start_position)
582585
match_data = @source.match(INSTRUCTION_END, true)
583586
unless match_data
584587
message = "Invalid processing instruction node"
585-
@source.string = "<?" + @source.buffer
588+
@source.position = start_position
586589
raise REXML::ParseException.new(message, @source)
587590
end
588591
if @document_status.nil? and match_data[1] == "xml"
@@ -625,7 +628,7 @@ def parse_attributes(prefixes, curr_ns)
625628
break if scanner.eos?
626629
end
627630

628-
pos = scanner.pos
631+
start_position = scanner.pos
629632
while true
630633
break if scanner.scan(ATTRIBUTE_PATTERN)
631634
unless scanner.scan(QNAME)
@@ -648,7 +651,7 @@ def parse_attributes(prefixes, curr_ns)
648651
scanner << "/" if closed
649652
scanner << ">"
650653
scanner << match_data[1]
651-
scanner.pos = pos
654+
scanner.pos = start_position
652655
closed = !match_data[2].nil?
653656
next
654657
end

lib/rexml/source.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ def match(pattern, cons=false)
7676
end
7777
end
7878

79-
def string=(string)
80-
@scanner.string = string
79+
def position
80+
@scanner.pos
81+
end
82+
83+
def position=(pos)
84+
@scanner.pos = pos
8185
end
8286

8387
# @return true if the Source is exhausted

test/parse/test_notation_declaration.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_no_name
3535
Line: 5
3636
Position: 72
3737
Last 80 unconsumed characters:
38-
<!NOTATION> ]> <r/>
38+
<!NOTATION> ]> <r/>
3939
DETAIL
4040
end
4141

0 commit comments

Comments
 (0)