-
Notifications
You must be signed in to change notification settings - Fork 98
Use more StringScanner based API for parse_id #299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
84512f3
Use more StringScanner based API for parse_id_invalid_details
naitoh f62efc3
Use more StringScanner based API for parse_id
naitoh 1280564
Remove unnecessary checks
naitoh 00b0a80
add test_garbage_after_public_ID_literal
naitoh 4780f79
Remove the unnecessary accept_external_id variable
naitoh 15e75eb
Integrate the parse_id_invalid_details_system method
naitoh 4e363ca
Remove the unnecessary parse_id_invalid_details_public path
naitoh 30118a9
Integrate the parse_id_invalid_details_public method
naitoh d42a439
Move the raise statements in parse_id individually.
naitoh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,6 +158,9 @@ module Private | |
| DEFAULT_ENTITIES_PATTERNS[term] = /&#{term};/ | ||
| end | ||
| XML_PREFIXED_NAMESPACE = "http://www.w3.org/XML/1998/namespace" | ||
| EXTERNAL_ID_PUBLIC_PATTERN = /\s+#{PUBIDLITERAL}\s+#{SYSTEMLITERAL}/um | ||
| EXTERNAL_ID_SYSTEM_PATTERN = /\s+#{SYSTEMLITERAL}/um | ||
| PUBLIC_ID_PATTERN = /\s+#{PUBIDLITERAL}/um | ||
| end | ||
| private_constant :Private | ||
|
|
||
|
|
@@ -307,7 +310,6 @@ def pull_event | |
| @source.ensure_buffer | ||
| else | ||
| id = parse_id(base_error_message, | ||
| accept_external_id: true, | ||
| accept_public_id: false) | ||
| if id[0] == "SYSTEM" | ||
| # For backward compatibility | ||
|
|
@@ -409,7 +411,6 @@ def pull_event | |
| end | ||
| name = parse_name(base_error_message) | ||
| id = parse_id(base_error_message, | ||
| accept_external_id: true, | ||
| accept_public_id: true) | ||
| @source.skip_spaces | ||
| unless @source.match?(">", true) | ||
|
|
@@ -667,68 +668,41 @@ def parse_name(base_error_message) | |
| end | ||
|
|
||
| def parse_id(base_error_message, | ||
| accept_external_id:, | ||
| accept_public_id:) | ||
| if accept_external_id and (md = @source.match(EXTERNAL_ID_PUBLIC, true)) | ||
| pubid = system = nil | ||
| pubid_literal = md[1] | ||
| pubid = pubid_literal[1..-2] if pubid_literal # Remove quote | ||
| system_literal = md[2] | ||
| system = system_literal[1..-2] if system_literal # Remove quote | ||
| ["PUBLIC", pubid, system] | ||
| elsif accept_public_id and (md = @source.match(PUBLIC_ID, true)) | ||
| pubid = system = nil | ||
| pubid_literal = md[1] | ||
| pubid = pubid_literal[1..-2] if pubid_literal # Remove quote | ||
| ["PUBLIC", pubid, nil] | ||
| elsif accept_external_id and (md = @source.match(EXTERNAL_ID_SYSTEM, true)) | ||
| system = nil | ||
| system_literal = md[1] | ||
| system = system_literal[1..-2] if system_literal # Remove quote | ||
| ["SYSTEM", nil, system] | ||
| else | ||
| details = parse_id_invalid_details(accept_external_id: accept_external_id, | ||
| accept_public_id: accept_public_id) | ||
| message = "#{base_error_message}: #{details}" | ||
| raise REXML::ParseException.new(message, @source) | ||
| end | ||
| end | ||
|
|
||
| def parse_id_invalid_details(accept_external_id:, | ||
| accept_public_id:) | ||
| public = /\A\s*PUBLIC/um | ||
| system = /\A\s*SYSTEM/um | ||
| if (accept_external_id or accept_public_id) and @source.match?(/#{public}/um) | ||
| if @source.match?(/#{public}(?:\s+[^'"]|\s*[\[>])/um) | ||
| return "public ID literal is missing" | ||
| end | ||
| unless @source.match?(/#{public}\s+#{PUBIDLITERAL}/um) | ||
| return "invalid public ID literal" | ||
| end | ||
| if accept_public_id | ||
| if @source.match?(/#{public}\s+#{PUBIDLITERAL}\s+[^'"]/um) | ||
| return "system ID literal is missing" | ||
| end | ||
| unless @source.match?(/#{public}\s+#{PUBIDLITERAL}\s+#{SYSTEMLITERAL}/um) | ||
| return "invalid system literal" | ||
| end | ||
| "garbage after system literal" | ||
| @source.skip_spaces | ||
| if @source.match?("PUBLIC", true) | ||
| if (md = @source.match(Private::EXTERNAL_ID_PUBLIC_PATTERN, true)) | ||
| pubid = system = nil | ||
| pubid_literal = md[1] | ||
| pubid = pubid_literal[1..-2] if pubid_literal # Remove quote | ||
| system_literal = md[2] | ||
| system = system_literal[1..-2] if system_literal # Remove quote | ||
| ["PUBLIC", pubid, system] | ||
| elsif accept_public_id and (md = @source.match(Private::PUBLIC_ID_PATTERN, true)) | ||
| pubid = system = nil | ||
| pubid_literal = md[1] | ||
| pubid = pubid_literal[1..-2] if pubid_literal # Remove quote | ||
| ["PUBLIC", pubid, nil] | ||
| elsif @source.match?(/(?:\s+[^'"]|\s*[\[>])/um) | ||
| raise REXML::ParseException.new("#{base_error_message}: public ID literal is missing", @source) | ||
| elsif [email protected]?(Private::PUBLIC_ID_PATTERN) | ||
| raise REXML::ParseException.new("#{base_error_message}: invalid public ID literal", @source) | ||
| else | ||
| "garbage after public ID literal" | ||
| raise REXML::ParseException.new("#{base_error_message}: garbage after public ID literal", @source) | ||
| end | ||
| elsif accept_external_id and @source.match?(/#{system}/um) | ||
| if @source.match?(/#{system}(?:\s+[^'"]|\s*[\[>])/um) | ||
| return "system literal is missing" | ||
| end | ||
| unless @source.match?(/#{system}\s+#{SYSTEMLITERAL}/um) | ||
| return "invalid system literal" | ||
| elsif @source.match?("SYSTEM", true) | ||
| if (md = @source.match(Private::EXTERNAL_ID_SYSTEM_PATTERN, true)) | ||
| system = nil | ||
| system_literal = md[1] | ||
| system = system_literal[1..-2] if system_literal # Remove quote | ||
| ["SYSTEM", nil, system] | ||
| elsif @source.match?(/(?:\s+[^'"]|\s*[\[>])/um) | ||
| raise REXML::ParseException.new("#{base_error_message}: system literal is missing", @source) | ||
| else | ||
| raise REXML::ParseException.new("#{base_error_message}: invalid system literal", @source) | ||
| end | ||
| "garbage after system literal" | ||
| else | ||
| unless @source.match?(/\A\s*(?:PUBLIC|SYSTEM)\s/um) | ||
| return "invalid ID type" | ||
| end | ||
| "ID type is missing" | ||
| raise REXML::ParseException.new("#{base_error_message}: invalid ID type", @source) | ||
| end | ||
| end | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove this keyword argument because it's always
true, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
rexml/lib/rexml/parsers/baseparser.rb
Lines 309 to 310 in 39e7e28
rexml/lib/rexml/parsers/baseparser.rb
Lines 411 to 412 in 39e7e28