This is the mail archive of the
systemtap@sourceware.org
mailing list for the systemtap project.
[PATCH 1/2] [TAKE 3] Add str_replace() To Tapsets
- From: Varun Chandramohan <varunc at linux dot vnet dot ibm dot com>
- To: systemtap at sourceware dot org
- Cc: Josh Stone <jistone at redhat dot com>
- Date: Thu, 18 Jun 2009 15:29:05 +0530
- Subject: [PATCH 1/2] [TAKE 3] Add str_replace() To Tapsets
Hi,
This patch adds a search and replace string functionality to existing tapsets.
The functionality is as follows:
The function takes in a parent string and searches for a substring as specified by the user. If substing not found, the parent string is returned. If substring is found, it is replaced by another string and returned.
NOTE: The function will search and replace all the occurance of substrings in a parent string when matched.
Signed-off-by: Varun Chandramohan <varunc@linux.vnet.ibm.com>
---
tapset/string.stp | 39 +++++++++++++++++++++++++++++++++++++++
1 files changed, 39 insertions(+), 0 deletions(-)
diff --git a/tapset/string.stp b/tapset/string.stp
index 35ee9fa..8c015d6 100644
--- a/tapset/string.stp
+++ b/tapset/string.stp
@@ -92,6 +92,45 @@ function tokenize:string(input:string, delim:string)
%}
/*
+ * str_replace - Takes a parent string, uses the second string which is a
+ * substring to parent string and replaces that with the
+ * third string of same lenght as second and returns the
+ * new converted parent string.
+ * prnt_str The parent string.
+ *
+ * srch_str The substring which is used to search in the parent string
+ * prnt_str. The srch_str can't be an empty string.
+ *
+ * rplc_str The substring which is used to replace the searched string
+ * srch_str.
+ */
+function str_replace:string (prnt_str:string, srch_str:string, rplc_str:string)
+%{
+ char *ptr = THIS->prnt_str;
+ char *ptr_base = THIS->prnt_str;
+ int strlen_prnt_str = strlen(THIS->prnt_str);
+ int strlen_srch_str = strlen(THIS->srch_str);
+ int strlen_rplc_str = strlen(THIS->rplc_str);
+
+ if(strlen_srch_str == 0) {
+ CONTEXT->last_error = "Invalid Search String";
+ return;
+ }
+
+ while((ptr = strstr(ptr, THIS->srch_str)) != NULL) {
+
+ *ptr = '\0';
+ strlcat(THIS->__retvalue, ptr_base, MAXSTRINGLEN);
+ strlcat(THIS->__retvalue, THIS->rplc_str, MAXSTRINGLEN);
+ ptr = ptr + strlen_srch_str;
+ ptr_base = ptr;
+ }
+
+ strlcat(THIS->__retvalue, ptr_base, MAXSTRINGLEN);
+ return;
+%}
+
+/*
* strtol - Convert a string to a long
* str String to convert
* base The base to use
--
1.6.0.4