Changeset 42ee1be
- Timestamp:
- Mar 24, 2011, 12:48:25 AM (14 years ago)
- Branches:
- master, release-1.10, release-1.8, release-1.9
- Children:
- e56303f
- Parents:
- 4479497
- git-author:
- Anders Kaseorg <andersk@mit.edu> (02/12/11 00:04:30)
- git-committer:
- Anders Kaseorg <andersk@mit.edu> (03/24/11 00:48:25)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
tester.c
r010a951 r42ee1be 137 137 } while (0) 138 138 139 CHECK_STR_AND_FREE("owl_util_substitute 1", "foo",140 owl_text_substitute("foo", "", "Y"));141 139 CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ", 142 140 owl_text_substitute("foo", "o", "YZ")); -
text.c
rd4927a7 r42ee1be 267 267 268 268 /* Return a string with any occurances of 'from' replaced with 'to'. 269 * Does not currently handle backslash quoting, but may in the future.270 269 * Caller must free returned string. 271 270 */ 272 271 char *owl_text_substitute(const char *in, const char *from, const char *to) 273 272 { 274 275 char *out; 276 int outlen, tolen, fromlen, inpos=0, outpos=0; 277 278 if (!*from) return g_strdup(in); 279 280 outlen = strlen(in)+1; 281 tolen = strlen(to); 282 fromlen = strlen(from); 283 out = g_new(char, outlen); 284 285 while (in[inpos]) { 286 if (!strncmp(in+inpos, from, fromlen)) { 287 outlen += tolen; 288 out = g_renew(char, out, outlen); 289 strcpy(out+outpos, to); 290 inpos += fromlen; 291 outpos += tolen; 292 } else { 293 out[outpos] = in[inpos]; 294 inpos++; outpos++; 295 } 296 } 297 out[outpos] = '\0'; 298 return(out); 273 char **split = g_strsplit(in, from, 0), *out; 274 out = g_strjoinv(to, split); 275 g_strfreev(split); 276 return out; 299 277 } 300 278
Note: See TracChangeset
for help on using the changeset viewer.