Don't use Regexp match for substring-after() XPath function - #355
Merged
Conversation
Contributor
Author
|
Not sure if the test case is in the right spot, or if it should not actually use the full |
Contributor
Author
|
BTW trying to dig into history, the RegExp behavior was introduced by 0d1b4da . But that is just some squashed commit. Trying to find the specific reasons for the RegExp, I was able to find the original commit here: And this is roughly reconstruction of the patch: From b41dec417658c7804ccbc7db25e929afc81a4f8d Mon Sep 17 00:00:00 2001
From: anonymous
Date: Thu, 15 Aug 2005 05:17:37 +0200
Subject: [PATCH] Fixes roundup issue 43: substring-after bug.
See: http://www.germane-software.com/cgi-bin/roundup/rexml/issue43
---
lib/rexml/functions.rb | 9 +++------
test/functions.rb | 5 +++++
2 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/lib/rexml/functions.rb b/lib/rexml/functions.rb
index 2550cb3..33895ca 100644
--- a/lib/rexml/functions.rb
+++ b/lib/rexml/functions.rb
@@ -158,12 +158,9 @@ module REXML
# Kouhei fixed this too
def Functions::substring_after( string, test )
ruby_string = string(string)
- ruby_index = ruby_string.index(string(test))
- if ruby_index.nil?
- ""
- else
- ruby_string[ ruby_index+1..-1 ]
- end
+ test_string = string(test)
+ return $1 if ruby_string =~ /#{test}(.*)/
+ ""
end
# Take equal portions of Mike Stok and Sean Russell; mix
diff --git a/test/functions.rb b/test/functions.rb
index 16dec57..82e08a6 100644
--- a/test/functions.rb
+++ b/test/functions.rb
@@ -100,4 +100,9 @@
end
+def test_substring_angrez
+ testString = REXML::Functions::substring_after("helloworld","hello")
+ assert_equal( 'world', testString )
+end
+
def test_translate
source = <<-EOFBut it seems the original Roundup tracker with the details is lost :( |
Contributor
Author
|
I also think that this implementation might be faster then the RegExp, but I have not tried and it was not the reason for this PR |
substring-after() XPath functionsubstring-after() XPath function
kou
reviewed
Aug 14, 2026
Comment on lines
+208
to
+211
| needle = string(test) | ||
| index = ruby_string.index(needle) | ||
| return "" if index.nil? | ||
| ruby_string[(index + needle.length)..-1] |
Member
There was a problem hiding this comment.
Could you use the same style?
Suggested change
| needle = string(test) | |
| index = ruby_string.index(needle) | |
| return "" if index.nil? | |
| ruby_string[(index + needle.length)..-1] | |
| ruby_test = string(test) | |
| ruby_index = ruby_string.index(ruby_test) | |
| if ruby_index.nil? | |
| "" | |
| else | |
| ruby_string[(ruby_index + ruby_test.length)..-1] | |
| end |
XPath expects `string` match according to specification: https://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring-after So these two should not provide the same result (while they previously did): ~~~ REXML::Functions::substring_after("helloworld","hello") REXML::Functions::substring_after("helloworld","hel.o") ~~~
voxik
force-pushed
the
substring-after-no-regexp
branch
from
August 17, 2026 08:19
4dbc86b to
2e9903c
Compare
Member
|
Thanks. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is the current behavior:
However, XPath expects
stringmatch according to specification:https://www.w3.org/TR/1999/REC-xpath-19991116/#function-substring-after
therefore the
matchforxpath2should actually return:Just for comparison, this is similar test case in Python: