Skip to content

Instantly share code, notes, and snippets.

@shafreeck
Last active August 29, 2015 14:13
Show Gist options
  • Save shafreeck/60d9e97bfa5bbc2efc8e to your computer and use it in GitHub Desktop.
Save shafreeck/60d9e97bfa5bbc2efc8e to your computer and use it in GitHub Desktop.
sds sdscatescape(sds s, const char *p, size_t len) {
s = sdscatlen(s,"\"",1);
while(len--) {
switch(*p) {
case '\\':
case '"':
s = sdscatprintf(s,"\\%c",*p);
break;
case '\n': s = sdscatlen(s,"\\n",2); break;
case '\r': s = sdscatlen(s,"\\r",2); break;
case '\t': s = sdscatlen(s,"\\t",2); break;
case '\a': s = sdscatlen(s,"\\a",2); break;
case '\b': s = sdscatlen(s,"\\b",2); break;
case '{': s = sdscatlen(s,"\\{",2); break;
case '}': s = sdscatlen(s,"\\}",2); break;
default:
if (isprint(*p))
s = sdscatprintf(s,"%c",*p);
else
s = sdscatprintf(s,"\\x%02x",(unsigned char)*p);
break;
}
p++;
}
return sdscatlen(s,"\"",1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment