[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [help-cgicc] add: escapeString()
From: |
Quintin Connell |
Subject: |
Re: [help-cgicc] add: escapeString() |
Date: |
Thu, 12 Jun 2003 15:29:45 +0200 (SAST) |
User-agent: |
IMP/PHP IMAP webmail program 2.2.6 |
This is my version of this very useful function that should be included as part
of the cgicc library.
STDNS string
CGICCNS escapeString(const STDNS string& src)
{
STDNS string result;
STDNS string::const_iterator iter;
for (iter = src.begin(); iter != src.end(); ++iter) {
if (isalnum(*iter) || (*iter == '*') || (*iter == '@') || (*iter == '-')
|| (*iter == '_') || (*iter == '+') || (*iter == '.') || (*iter == '/')) {
result.append(1, *iter);
}
else {
result.append("%");
result.append(charToHex(*iter));
}
}
return result;
}
Note the standard function isalnum() does the same job as your is_save()
function. The list of characters that i include not to be encoded i got from
somewhere, i'm sure in the HTTP 1.1 RFC but i can't find a reference to it at
the moment. But i think the point is that not all non alphanumeric characters
need to be encoded. Maybe someone out there can shed light on this point.
Quintin
Quoting Karl Pitrich <address@hidden>:
> hi,
>
> i added a _simple_ escapeString() to CgiUtils.cpp, maybe it is usefult
> to someone else.
> as it is really not much code, i paste it plainly instead of a diff.
>
> / karl
>
>
> static const char *hex_chars = "0123456789ABCDEF";
>
> static bool is_save(char c) {
> if((c >= '0' && c <= '9')
> || (c >= 'A' && c <= 'Z')
> || (c >= 'a' && c <= 'z'))
> return true;
> return false;
> }
>
> STDNS string
> CGICCNS escapeString(const STDNS string& src)
> {
> STDNS string result;
> STDNS string::const_iterator iter;
>
> for(iter = src.begin(); iter != src.end(); ++iter) {
> if(is_save(*iter)) {
> result.append(1, *iter);
> } else {
> result.append(1, '%');
> result.append(1, hex_chars[*iter >> 4]);
> result.append(1, hex_chars[*iter & 15]);
> }
> }
> return result;
> }
>
>
> --
> Karl Pitrich [GCS C+++ L UL+++ L++++ P--- E--- w--- PGP++ t+++]
> doin' the Linux at
> Fabasoft R&D Software GmbH & Co KG, Linz, Austria
>
>