[OpenSIPS-Devel] [ opensips-Patches-3609533 ] String Transformations: Add s.index and s.rindex
SourceForge.net
noreply at sourceforge.net
Sat Mar 30 05:54:11 CET 2013
Patches item #3609533, was opened at 2013-03-29 21:54
Message generated for change (Tracker Item Submitted) made by rrb3942
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=1086412&aid=3609533&group_id=232389
Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: core
Group: trunk
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Ryan Bullock (rrb3942)
Assigned to: Nobody/Anonymous (nobody)
Summary: String Transformations: Add s.index and s.rindex
Initial Comment:
This patch adds the s.index and s.rindex transformations. These transformations can be used to find the first or last occurrence of a string in another string, returning its
index (beginning at 0) or -1 if it is not found. They also optionally take an index parameter which is explained more below.
Examples:
s.index transformation
Searches for one string within another starting at the beginning of the first string. Returns starting index of the string found or -1 if not found.
The optional index specifies the offset to begin the search at in the string. Negative offsets are supported and will wrap.
$var(strtosearch) = 'onetwothreeone';
$var(str) = 'one';
#Search the string starting at 0 index
$(var(strtosearch){s.index, $var(str)}) #will return 0
$(var(strtosearch){s.index, $var(str), 0}) #Same as above
$(var(strtosearch){s.index, $var(str), 3}) #returns 11
#Negative offset
$(var(strtosearch){s.index, $var(str), -11}) #Same as above
#Negative wrapping offset
$(var(strtosearch){s.index, $var(str), -25}) #Same as above
#Test for existence of string in another
if ($(var(strtosearch){s.index, $var(str)}) >=0) {
xlog("found $var(sstr) in $var(strtosearch)");
....
do some stuff
....
}
#Case insensitive match
$var(str) = 'ONE';
$(var(strtosearch){s.index, $var(str)}) #will return -1
$(var(strtosearch){s.toupper}{s.index, $var(str)}) #will return 0
r.index transformation
Searches for one string within another starting at the end of the first string. Returns starting index of the string found or -1 if not found.
The optional index specifies an offset to start the search before, e.g the start of the found string will be before the supplied offset. Negative offsets are supported and will wrap.
$(var(strtosearch){s.rindex, $var(str)}) #will return 11
$(var(strtosearch){s.rindex, $var(str), -3}) #will return 11
$(var(strtosearch){s.rindex, $var(str), 11}) #will return 11
$(var(strtosearch){s.rindex, $var(str), -4}) #will return 0
These transformations should be helpful for general string manipulation in conjunction with the s.substr transformation.
They also provide a mechanism for searching a string that does not rely on regular expressions.
----------------------------------------------------------------------
You can respond by visiting:
https://sourceforge.net/tracker/?func=detail&atid=1086412&aid=3609533&group_id=232389
More information about the Devel
mailing list