Sachin
2009-10-05 04:24:01 UTC
Hi i have a requirement of searching into Character array for a character
with ASCII value greater than 255 and replacing the character by another
value..
to be more specific what i am doing is escaping characters in char buffer by
its equivalent escape sequence to form an HTML
so i will be escaping all characters with value greater than 255 with
&<intvalue>;
so that the resultant HTML would render it properly .
now in average case my char array will contain more than 50000 characters
the very priliminary algorithm would look like
void CHTML::EscapeUnicodeCharacters(wstring& value)
{
wstring newbuff;
const WCHAR* buff = value.c_str();
int len = value.length();
for (int i = 0; i<len ; i++)
{
if( value[i] >127)
{
WCHAR* wchr = new WCHAR[10];
memset(wchr, 0, 10);
swprintf_s(wchr, 10, L"&%ld;",value[i]);
newbuff.append(wchr);
delete[] wchr;
}
else
newbuff.insert(newbuff.length(), 1, value[i]);
}
value.clear();
value.assign(newbuff);
}
to sumup what i have done is search linearly in input array and side by side
prepare second array , if character value is less than 127 put it as it is in
output array .. else
insert escape sequence in output array.
case we optimise the alogorithm from linear to logarithmic ?
with ASCII value greater than 255 and replacing the character by another
value..
to be more specific what i am doing is escaping characters in char buffer by
its equivalent escape sequence to form an HTML
so i will be escaping all characters with value greater than 255 with
&<intvalue>;
so that the resultant HTML would render it properly .
now in average case my char array will contain more than 50000 characters
the very priliminary algorithm would look like
void CHTML::EscapeUnicodeCharacters(wstring& value)
{
wstring newbuff;
const WCHAR* buff = value.c_str();
int len = value.length();
for (int i = 0; i<len ; i++)
{
if( value[i] >127)
{
WCHAR* wchr = new WCHAR[10];
memset(wchr, 0, 10);
swprintf_s(wchr, 10, L"&%ld;",value[i]);
newbuff.append(wchr);
delete[] wchr;
}
else
newbuff.insert(newbuff.length(), 1, value[i]);
}
value.clear();
value.assign(newbuff);
}
to sumup what i have done is search linearly in input array and side by side
prepare second array , if character value is less than 127 put it as it is in
output array .. else
insert escape sequence in output array.
case we optimise the alogorithm from linear to logarithmic ?