diff options
author | unknown <patg@krsna.patg.net> | 2005-07-31 21:28:52 -0700 |
---|---|---|
committer | unknown <patg@krsna.patg.net> | 2005-07-31 21:28:52 -0700 |
commit | 16c45930897a3221fbf11acbad9403a267e9501b (patch) | |
tree | 507c672dbbe5bca974611526c1a4a5564f3344ee /sql/item_strfunc.cc | |
parent | 90e41facf78150df4f8d387f6d44647957c7a8a0 (diff) | |
download | mariadb-git-16c45930897a3221fbf11acbad9403a267e9501b.tar.gz |
BUG 11104 (same as changeset 1.1891 on the 5.0 tree, but realised this
needed to be fixed in earlier versions)
Fixed the iteration of how substrings are handled with negative indexes in
SUBSTRING_INDEX
mysql-test/r/func_str.result:
New results for the fix to substring_index
mysql-test/t/func_str.test:
Added tests for fix to substring_index, various lenth search patterns. Also included
are the queuries the user who reported the bug listed in the bug report
sql/item_strfunc.cc:
Fix to BUG 11104
Took out the offset-=delimiter_length-1 out of the for loop. It was causing
basically this:
select substring_index('the king of the the hill', 'the', -2) to not work.
The first iteration, offset would be initialised to 24, then strstr would
point at 'the king of the the* hill' ('*'means right before the
character following), returning a offset of 16. The for loop would then
decrement offset by two (3 - 1), to 14, now pointing at
"the king of th*e the hill", _skipping_ past the 'e' in the second to last
'the', and therefore strstr would never have a chance of matching the
second to last 'the', then moving on to the 'the' at the begginning of the
string!
In a nutshell, offset was being decremented by too great a value, preventing
the second to last 'the' from being ever found, hence the result of
'king of the the hill' from the query that is reported in the bug report
Diffstat (limited to 'sql/item_strfunc.cc')
-rw-r--r-- | sql/item_strfunc.cc | 16 |
1 files changed, 14 insertions, 2 deletions
diff --git a/sql/item_strfunc.cc b/sql/item_strfunc.cc index 930014de771..f070382e5c1 100644 --- a/sql/item_strfunc.cc +++ b/sql/item_strfunc.cc @@ -1084,11 +1084,23 @@ String *Item_func_substr_index::val_str(String *str) } } else - { // Start counting at end - for (offset=res->length() ; ; offset-=delimeter_length-1) + { + /* + Negative index, start counting at the end + */ + for (offset=res->length(); offset ;) { + /* + this call will result in finding the position pointing to one + address space less than where the found substring is located + in res + */ if ((int) (offset=res->strrstr(*delimeter,offset)) < 0) return res; // Didn't find, return org string + /* + At this point, we've searched for the substring + the number of times as supplied by the index value + */ if (!++count) { offset+=delimeter_length; |