summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPrzemyslaw Pawelczyk <przemyslaw@pawelczyk.it>2009-06-18 01:50:31 +0200
committerJosh Stone <jistone@redhat.com>2009-06-17 17:43:48 -0700
commit7f12f9a3f6aeb2452acedced5a54c66c4a19382b (patch)
tree491f2b5f26cc83319d43c2ef45417b446448c276
parent44b73c9d467fe0383d33dce5f1217e023f3b203b (diff)
downloadsystemtap-steved-7f12f9a3f6aeb2452acedced5a54c66c4a19382b.tar.gz
systemtap-steved-7f12f9a3f6aeb2452acedced5a54c66c4a19382b.tar.xz
systemtap-steved-7f12f9a3f6aeb2452acedced5a54c66c4a19382b.zip
Fix tokenize function and test.
Previous implementation was error-prone, because allowed returning empty tokens (mimiced strsep()), which is fine if there is a NULL semantic. Unfortunately SystemTap doesn't provide it in scripts and has only blank string (""), therefore testing against it was misleading. The solution is to return only non-empty tokens (mimic strtok()). * tapset/string.stp: Fix tokenize. * testsuite/systemtap.string/tokenize.stp: Improve and add case with more than one delimiter in the delim string. * testsuite/systemtap.string/tokenize.exp: Ditto. * stapfuncs.3stap.in: Update tokenize description. * doc/langref.tex: Ditto. Signed-off-by: Josh Stone <jistone@redhat.com>
-rw-r--r--doc/langref.tex4
-rw-r--r--stapfuncs.3stap.in6
-rw-r--r--tapset/string.stp23
-rw-r--r--testsuite/systemtap.string/tokenize.exp16
-rw-r--r--testsuite/systemtap.string/tokenize.stp75
5 files changed, 82 insertions, 42 deletions
diff --git a/doc/langref.tex b/doc/langref.tex
index 5aefa278..5a149d19 100644
--- a/doc/langref.tex
+++ b/doc/langref.tex
@@ -3160,8 +3160,8 @@ General syntax:
tokenize:string (input:string, delim:string)
\end{verbatim}
\end{vindent}
-This function returns the next token in the given input string, where
-the tokens are delimited by one of the characters in the delim string.
+This function returns the next non-empty token in the given input string,
+where the tokens are delimited by characters in the delim string.
If the input string is non-NULL, it returns the first token. If the input string
is NULL, it returns the next token in the string passed in the previous call
to tokenize. If no delimiter is found, the entire remaining input string
diff --git a/stapfuncs.3stap.in b/stapfuncs.3stap.in
index 518ff2bb..3d88b2ea 100644
--- a/stapfuncs.3stap.in
+++ b/stapfuncs.3stap.in
@@ -166,11 +166,11 @@ specified by base. For example, strtol("1000", 16) returns 4096. Returns 0 if
string cannot be converted.
.TP
tokenize:string (str:string, delim:string)
-Return the next token in the given str string, where the tokens are delimited
-by one of the characters in the delim string. If the str string is not blank,
+Return the next non-empty token in the given str string, where the tokens are
+delimited by characters in the delim string. If the str string is not blank,
it returns the first token. If the str string is blank, it returns the next
token in the string passed in the previous call to tokenize. If no delimiter
-is found, the entire remaining str string is returned. Returns blank when
+is found, the entire remaining str string is returned. Returns blank when
no more tokens are left.
.SS TIMESTAMP
diff --git a/tapset/string.stp b/tapset/string.stp
index 35ee9fa2..cc842929 100644
--- a/tapset/string.stp
+++ b/tapset/string.stp
@@ -70,25 +70,32 @@ function text_strn:string(input:string, len:long, quoted:long)
/*
* tokenize - Given a string and a token delimiter,
- * return the next token in the string
- * input String to tokenize. If NULL, returns the next token in the
- * string passed in the previous call to tokenize().
- * delim Token delimiter. Note this is a string, but only the first
- * character is used as the delimiter.
+ * return the next non-empty token in the string
+ * or blank when no more non-empty tokens are left
+ * input String to tokenize. If NULL, returns the next non-empty token
+ * in the string passed in the previous call to tokenize().
+ * delim Token delimiter. Set of characters that delimit the tokens.
*/
function tokenize:string(input:string, delim:string)
%{ /* pure */
static char str[MAXSTRINGLEN];
static char *str_start;
+ static char *str_end;
char *token = NULL;
+ char *token_end = NULL;
if (THIS->input[0]) {
strncpy(str, THIS->input, MAXSTRINGLEN);
str_start = &str[0];
+ str_end = &str[0] + strlen(str);
+ }
+ do {
+ token = strsep(&str_start, THIS->delim);
+ } while (token && !token[0]);
+ if (token) {
+ token_end = (str_start ? str_start - 1 : str_end);
+ memcpy(THIS->__retvalue, token, token_end - token + 1);
}
- token = strsep(&str_start, THIS->delim);
- if (token)
- strncpy (THIS->__retvalue, token, MAXSTRINGLEN);
%}
/*
diff --git a/testsuite/systemtap.string/tokenize.exp b/testsuite/systemtap.string/tokenize.exp
index 697b7c7e..aa28f855 100644
--- a/testsuite/systemtap.string/tokenize.exp
+++ b/testsuite/systemtap.string/tokenize.exp
@@ -9,7 +9,9 @@ seven
eight
nine
ten
+-
one|two|three|four|five|six|seven|eight|nine|ten
+-
a
b
c
@@ -17,10 +19,22 @@ d
e
f
g
+-
1
2
3
4
-this is a string with no delimiters}
+-
+-
+this is a string with no delimiters
+-
+this
+is
+a
+string
+which
+has
+two
+delimiters}
stap_run2 $srcdir/$subdir/$test.stp
diff --git a/testsuite/systemtap.string/tokenize.stp b/testsuite/systemtap.string/tokenize.stp
index 10703d90..1b253c8d 100644
--- a/testsuite/systemtap.string/tokenize.stp
+++ b/testsuite/systemtap.string/tokenize.stp
@@ -5,42 +5,61 @@ probe begin
teststr3 = "1,,2,3, ,4"
teststr4 = ""
teststr5 = "this is a string with no delimiters"
+ teststr6 = "this is a string, which has two delimiters"
tok = tokenize(teststr1, "|")
while (tok != "") {
- printf("%s\n", tok)
+ println(tok)
tok = tokenize("", "|")
}
- tok = tokenize(teststr1, ",")
- while (tok != "") {
- printf("%s\n", tok)
- tok = tokenize("", "|")
- }
+ println("-")
+
+ tok = tokenize(teststr1, ",")
+ while (tok != "") {
+ println(tok)
+ tok = tokenize("", "|")
+ }
+ println("-")
+
tok = tokenize(teststr2, ",")
- while (tok != "") {
- printf("%s\n", tok)
- tok = tokenize("", ",")
- }
-
- tok = tokenize(teststr3, ",")
- while (tok != "") {
- printf("%s\n", tok)
- tok = tokenize("", ",")
- }
-
- tok = tokenize(teststr4, ",")
- while (tok != "") {
- printf("%s\n", tok)
- tok = tokenize("", ",")
- }
-
- tok = tokenize(teststr5, ",")
- while (tok != "") {
- printf("%s\n", tok)
- tok = tokenize("", ",")
- }
+ while (tok != "") {
+ println(tok)
+ tok = tokenize("", ",")
+ }
+
+ println("-")
+
+ tok = tokenize(teststr3, ",")
+ while (tok != "") {
+ println(tok)
+ tok = tokenize("", ",")
+ }
+
+ println("-")
+
+ tok = tokenize(teststr4, ",")
+ while (tok != "") {
+ println(tok)
+ tok = tokenize("", ",")
+ }
+
+ println("-")
+
+ tok = tokenize(teststr5, ",")
+ while (tok != "") {
+ println(tok)
+ tok = tokenize("", ",")
+ }
+
+ println("-")
+
+ tok = tokenize(teststr6, ", ")
+ while (tok != "") {
+ println(tok)
+ tok = tokenize("", ", ")
+ }
exit()
}