Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Leung <aligorith@gmail.com>2009-10-09 16:18:32 +0400
committerJoshua Leung <aligorith@gmail.com>2009-10-09 16:18:32 +0400
commit2cf7d4867d4f6651fa54c3824f5e9f8f8a3e3770 (patch)
tree988fc47eb28db68bd59eb23f4670f54fd84182fe /source/blender/blenlib
parent9f7038c5a73d8e73b858a129561d4721481f62e2 (diff)
(Untested) String Replacement Function in blenlib
Currently this hasn't been tested yet, but committing this first before I potentially use it for fixing RNA-paths...
Diffstat (limited to 'source/blender/blenlib')
-rw-r--r--source/blender/blenlib/BLI_string.h12
-rw-r--r--source/blender/blenlib/intern/string.c71
2 files changed, 83 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h
index bf93dc19cc5..53563f85eb9 100644
--- a/source/blender/blenlib/BLI_string.h
+++ b/source/blender/blenlib/BLI_string.h
@@ -68,6 +68,18 @@ char *BLI_strdupn(const char *str, int len);
*/
char *BLI_strncpy(char *dst, const char *src, int maxncpy);
+ /**
+ * Returns a copy of the cstring @a str into a newly mallocN'd
+ * string with all instances of oldText replaced with newText,
+ * and returns it.
+ *
+ * @param str The string to replace occurances of oldText in
+ * @param oldText The text in the string to find and replace
+ * @param newText The text in the string to find and replace
+ * @retval Returns the duplicated string
+ */
+char *BLI_replacestr(char *str, const char *oldText, const char *newText);
+
/*
* Replacement for snprintf
*/
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index 4cd04aa232c..de4b53cbd93 100644
--- a/source/blender/blenlib/intern/string.c
+++ b/source/blender/blenlib/intern/string.c
@@ -100,6 +100,77 @@ char *BLI_sprintfN(const char *format, ...)
return n;
}
+/* Replaces all occurances of oldText with newText in str, returning a new string that doesn't
+ * contain the 'replaced' occurances.
+ */
+// A rather wasteful string-replacement utility, though this shall do for now...
+// Feel free to replace this with an even safe + nicer alternative
+char *BLI_replacestr(char *str, const char *oldText, const char *newText)
+{
+ DynStr *ds= NULL;
+ int lenOld= strlen(oldText);
+ char *match;
+
+ /* sanity checks */
+ if ((str == NULL) || (str[0]==0))
+ return NULL;
+ else if ((oldText == NULL) || (newText == NULL) || (oldText[0]==0))
+ return BLI_strdup(str);
+
+ /* while we can still find a match for the old substring that we're searching for,
+ * keep dicing and replacing
+ */
+ while ( (match = strstr(str, oldText)) ) {
+ /* the assembly buffer only gets created when we actually need to rebuild the string */
+ if (ds == NULL)
+ ds= BLI_dynstr_new();
+
+ /* if the match position does not match the current position in the string,
+ * copy the text up to this position and advance the current position in the string
+ */
+ if (str != match) {
+ /* replace the token at the 'match' position with \0 so that the copied string will be ok,
+ * add the segment of the string from str to match to the buffer, then restore the value at match
+ */
+ match[0]= 0;
+ BLI_dynstr_append(ds, str);
+ match[0]= oldText[0];
+
+ /* now our current position should be set on the start of the match */
+ str= match;
+ }
+
+ /* add the replacement text to the accumulation buffer */
+ BLI_dynstr_append(ds, newText);
+
+ /* advance the current position of the string up to the end of the replaced segment */
+ str += lenOld;
+ }
+
+ /* finish off and return a new string that has had all occurances of */
+ if (ds) {
+ char *newStr;
+
+ /* add what's left of the string to the assembly buffer
+ * - we've been adjusting str to point at the end of the replaced segments
+ */
+ if (str != NULL)
+ BLI_dynstr_append(ds, str);
+
+ /* convert to new c-string (MEM_malloc'd), and free the buffer */
+ newStr= BLI_dynstr_get_cstring(ds);
+ BLI_dynstr_free(ds);
+
+ return newStr;
+ }
+ else {
+ /* just create a new copy of the entire string - we avoid going through the assembly buffer
+ * for what should be a bit more efficiency...
+ */
+ return BLI_strdup(str);
+ }
+}
+
int BLI_streq(const char *a, const char *b)
{
return (strcmp(a, b)==0);