I wrote a custom URL encoding function and I'd like to run it past a few other experienced C developers. I have tested it on a few strings, and it has worked on all of them.
This is to be run on iOS devices, so memory and processor use are potentially a small concern.
Do you see any potential problems - UB, wrong return, excessive memory or processor usage, difficulty reading?
Any suggestions for improvement?
NSString *urlEncode(NSString *orig)
{
static const BOOL safe[0x100] = {
['a'...'z'] = YES,
['A'...'Z'] = YES,
['0'...'9'] = YES,
['-'] = YES,
['_'] = YES,
['.'] = YES,
['~'] = YES
};
const char *digits = "0123456789ABCDEF";
const char *cstr = orig.UTF8String;
int clen = orig.length;
int nlen = 3*clen;
char newstr[nlen + 1];
const char *o = cstr + clen;
char *p = newstr + nlen;
*p = 0;
while(o > cstr) {
unsigned char c = *--o;
if(safe[c]) {
*--p = c;
} else {
*--p = digits[c&0xf];
*--p = digits[c>>4];
*--p = '%';
}
}
NSString *str = [NSString stringWithUTF8String:p];
return str;
}
Yes, newStr
could get long, but given that a URL > 2kB is unreliable, I think it's unlikely to be a problem in reality.
NSString
'sstringByAddingPercentEscapesUsingEncoding
? Or, if you need to expand on what is escaped (as you likely would to implement a true URL encoding), you should be able to useCFURLCreateStringByAddingPercentEscapes
(example of custom escaping: stackoverflow.com/a/8086845/567864) \$\endgroup\$stringByAddingPercentEscapesUsingEncoding
doesn't convert everything it should. Sure, I could useCFURLCreateStringByAddingPercentEscapes
(and have for other projects), but I thought I'd roll my own this time. \$\endgroup\$