This is the mail archive of the systemtap@sourceware.org mailing list for the systemtap project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Add Strrplc() To Tapsets


Hi,

This patch adds a search and replace string functionality to existing tapsets. I found this function to be helpful in many use cases.
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 of the same lenght as substring 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 |   36 ++++++++++++++++++++++++++++++++++++
 1 files changed, 36 insertions(+), 0 deletions(-)

diff --git a/tapset/string.stp b/tapset/string.stp
index 35ee9fa..b277479 100644
--- a/tapset/string.stp
+++ b/tapset/string.stp
@@ -92,6 +92,42 @@ function tokenize:string(input:string, delim:string)
 %}
 
 /*
+ * strrplc - 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.
+ * s1	The parent string. If NULL, the function returns NULL.
+ * 
+ * s2	The substring which is used to search in the parent string s1.
+ * 
+ * s3	The substring which is used to replace the searched string s2.
+ *	Both s2 and s3 have to be of same lenght. Else s1 is returned.
+ */
+function strrplc:string (s1:string, s2:string, s3:string)
+%{
+	static char str[MAXSTRINGLEN];
+	char *ptr = str;
+
+	if(THIS->s1 == NULL)
+		return THIS->__retvalue = NULL;
+
+	if(strlen(THIS->s2) != strlen(THIS->s3)) {
+		strncpy(THIS->__retvalue, THIS->s1, MAXSTRINGLEN);
+		return;
+	}
+
+	strncpy(str, THIS->s1, MAXSTRINGLEN);
+
+	while((ptr = strstr(ptr, THIS->s2)) != NULL) {
+		memcpy(ptr, THIS->s3, strlen(THIS->s3));
+		ptr = ptr + strlen(THIS->s3);
+	}
+
+	strncpy(THIS->__retvalue, str, MAXSTRINGLEN);
+	return;
+%}
+
+/*
  * strtol - Convert a string to a long
  *   str   String to convert
  *   base  The base to use
-- 
1.6.0.4


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]