Locating line breaks in text.
| Fri, 2004-10-22 10:20 | |
|
Stupid problem: I have code to generate xml. To make sure it generates valid xml, it replaces 'special' characters (like &, ", < and >) with their appropriate xml code (like "&").
It also needs to do this for line breaks and carriage returns (ASCII values 10 and 13, respectively). However, I can't figure out how to locate these characters in a descriptor. The following does *not* work: Code: TInt pos = aSource->Find(_L("\n")); This will always return a KErrNotFound.... Looking with the debugger, I see that a like break is stored as character \x2029. However, Find(_L("\x2029")) does work either. Besides, this will not work in plain ASCII (non-Unicode) environments... Is there a cross-environment way to locate special characters, like line breaks, in a descriptor? |
|






Forum posts: 75
*tmp = _L("This is a test\nYes!\n");
int i = tmp->Find(_L("\n"));
int j = tmp->Find(_L("test"));
RDebug::Print(_L("i = %d\nj = %d"), i, j);
delete tmp;
j = 10
Cheers!
-Slava
Forum posts: 58
Here is my few cents... 0x2029 that you are seeing is CEditableText::EParagraphDelimiter which is what the editor uses as paragraph breaks when you type text.
How you handle this depends on your application. You can just run a pointer across your unicode text and replace all 2029 to '\n' and then do a unicode to utf (ascii) conversion, in which case you will result with text that contains '\n' as line breaks.
I am assuming that you are doing conversion from unicode to utf because you are concerned in your question about '\n' compatibility in other environments.
Each TDesC8 and TDesC16 has its own variant of Find() function, that allows you to handle unicode and non-unicode searches...
In the current project I am working on, I had to do a translation from 0x2029 to '\r\n' which I do in a traditional manner:
{
if(*pSrc == CEditableText::EParagraphDelimiter)
{
*pDest = 0x000D;
pDest++;
*pDest = 0x000A;
}
else
*pDest = *pSrc;
pSrc++;
pDest++;
}
Above method needs to pre-count amount of 0x2029 characters in pSrc to size pDest buffer appropriately.
Hope this helps,
ASY
www.fxuiq.com
Forum posts: 73
Thanks!
-- arno