src/lib/common.c 1.131
1.131 2009/06/26 19:57:33 tdreszer
Added strSwapStrs like replaceChrs but swaps in place.
Index: src/lib/common.c
===================================================================
RCS file: /projects/compbio/cvsroot/kent/src/lib/common.c,v
retrieving revision 1.130
retrieving revision 1.131
diff -b -B -U 4 -r1.130 -r1.131
--- src/lib/common.c 10 May 2009 02:09:16 -0000 1.130
+++ src/lib/common.c 26 Jun 2009 19:57:33 -0000 1.131
@@ -1154,8 +1154,29 @@
strcpy(resultPtr, string);
return result;
}
+int strSwapStrs(char *string, int sz,char *old, char *new)
+/* Swaps all occurnces of the old with the new in string. Need not be same size
+ Swaps in place but restricted by sz. Returns count of swaps or -1 for sz failure. */
+{
+// WARNING: called at low level, so no errors allowed.
+int count = 0;
+char *p=NULL;
+for(p=strstr(string,old);p!=NULL;p=strstr(p+strlen(old),old))
+ count++;
+if(count == 0)
+ return 0;
+if((strlen(string)+(count*(strlen(new) - strlen(old))))>=sz)
+ return -1;
+for(p=strstr(string,old);p!=NULL;p=strstr(p+strlen(new),old))
+ {
+ memmove(p+strlen(new),p+strlen(old),strlen(p+strlen(old))+1); // NULL at end is also moved!
+ memcpy(p,new,strlen(new));
+ }
+return count;
+}
+
char *strLower(char *s)
/* Convert entire string to lower case */
{
char c;