Changeset 42ee1be


Ignore:
Timestamp:
Mar 24, 2011, 12:48:25 AM (10 years ago)
Author:
Anders Kaseorg <andersk@mit.edu>
Branches:
master, 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)
Message:
Reimplement owl_text_substitute with g_strsplit and g_strjoinv

(I have no idea what the rambling about backslash quoting was about.)

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Reviewed-by: David Benjamin <davidben@mit.edu>
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • tester.c

    r010a951 r42ee1be  
    137137    } while (0)
    138138
    139   CHECK_STR_AND_FREE("owl_util_substitute 1", "foo",
    140                      owl_text_substitute("foo", "", "Y"));
    141139  CHECK_STR_AND_FREE("owl_text_substitute 2", "fYZYZ",
    142140                     owl_text_substitute("foo", "o", "YZ"));
  • text.c

    rd4927a7 r42ee1be  
    267267
    268268/* Return a string with any occurances of 'from' replaced with 'to'.
    269  * Does not currently handle backslash quoting, but may in the future.
    270269 * Caller must free returned string.
    271270 */
    272271char *owl_text_substitute(const char *in, const char *from, const char *to)
    273272{
    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;
    299277}
    300278
Note: See TracChangeset for help on using the changeset viewer.