diff options
author | rsc <devnull@localhost> | 2003-11-23 18:12:54 +0000 |
---|---|---|
committer | rsc <devnull@localhost> | 2003-11-23 18:12:54 +0000 |
commit | fd04aacee17b348da206c13a550dc1029669805f (patch) | |
tree | 9bdd35a25ff6e3d6e9a0171b06240a76723f922c /src/lib9/quote.c | |
parent | 74f990ad84deb1591ddb91be4fc8152ec0c46222 (diff) | |
download | plan9port-fd04aacee17b348da206c13a550dc1029669805f.tar.gz plan9port-fd04aacee17b348da206c13a550dc1029669805f.tar.bz2 plan9port-fd04aacee17b348da206c13a550dc1029669805f.zip |
Various additions and fixes.
Diffstat (limited to 'src/lib9/quote.c')
-rw-r--r-- | src/lib9/quote.c | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/src/lib9/quote.c b/src/lib9/quote.c new file mode 100644 index 00000000..4467377a --- /dev/null +++ b/src/lib9/quote.c @@ -0,0 +1,136 @@ +#include <u.h> +#include <libc.h> + +int (*doquote)(int); + +/* in libfmt */ +extern int __needsquotes(char*, int*); +extern int __runeneedsquotes(Rune*, int*); + +char* +unquotestrdup(char *s) +{ + char *t, *ret; + int quoting; + + ret = s = strdup(s); /* return unquoted copy */ + if(ret == nil) + return ret; + quoting = 0; + t = s; /* s is output string, t is input string */ + while(*t!='\0' && (quoting || (*t!=' ' && *t!='\t'))){ + if(*t != '\''){ + *s++ = *t++; + continue; + } + /* *t is a quote */ + if(!quoting){ + quoting = 1; + t++; + continue; + } + /* quoting and we're on a quote */ + if(t[1] != '\''){ + /* end of quoted section; absorb closing quote */ + t++; + quoting = 0; + continue; + } + /* doubled quote; fold one quote into two */ + t++; + *s++ = *t++; + } + if(t != s) + memmove(s, t, strlen(t)+1); + return ret; +} + +Rune* +unquoterunestrdup(Rune *s) +{ + Rune *t, *ret; + int quoting; + + ret = s = runestrdup(s); /* return unquoted copy */ + if(ret == nil) + return ret; + quoting = 0; + t = s; /* s is output string, t is input string */ + while(*t!='\0' && (quoting || (*t!=' ' && *t!='\t'))){ + if(*t != '\''){ + *s++ = *t++; + continue; + } + /* *t is a quote */ + if(!quoting){ + quoting = 1; + t++; + continue; + } + /* quoting and we're on a quote */ + if(t[1] != '\''){ + /* end of quoted section; absorb closing quote */ + t++; + quoting = 0; + continue; + } + /* doubled quote; fold one quote into two */ + t++; + *s++ = *t++; + } + if(t != s) + memmove(s, t, (runestrlen(t)+1)*sizeof(Rune)); + return ret; +} + +char* +quotestrdup(char *s) +{ + char *t, *u, *ret; + int quotelen; + Rune r; + + if(__needsquotes(s, "elen) == 0) + return strdup(s); + + ret = malloc(quotelen+1); + if(ret == nil) + return nil; + u = ret; + *u++ = '\''; + for(t=s; *t; t++){ + r = *t; + if(r == L'\'') + *u++ = r; /* double the quote */ + *u++ = r; + } + *u++ = '\''; + *u = '\0'; + return ret; +} + +Rune* +quoterunestrdup(Rune *s) +{ + Rune *t, *u, *ret; + int quotelen; + Rune r; + + if(__runeneedsquotes(s, "elen) == 0) + return runestrdup(s); + + ret = malloc((quotelen+1)*sizeof(Rune)); + if(ret == nil) + return nil; + u = ret; + *u++ = '\''; + for(t=s; *t; t++){ + r = *t; + if(r == L'\'') + *u++ = r; /* double the quote */ + *u++ = r; + } + *u++ = '\''; + *u = '\0'; + return ret; +} |