Locating line breaks in text.

Login to reply to this topic.
Fri, 2004-10-22 10:20
Joined: 2004-05-26
Forum posts: 73
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 "&amp;").
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?

Sat, 2004-10-23 04:08
Joined: 2004-09-25
Forum posts: 75
Locating line breaks in text.
This example exercises the TDesC16::Find method:

Code:
   HBufC* tmp = HBufC::New(255);
   *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;
and it works as expected:

Quote
i = 14
j = 10
or at least it does so in Series60 1.2 SDK. What SDK are you using? BTW, in my case "\n" is stored as 0x000A, just as I would expect. You must be doing something wrong.

Cheers!
-Slava
Sat, 2004-10-23 05:40
Joined: 2004-08-18
Forum posts: 58
Locating line breaks in text.
Hi,

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:

Code:
while(*pSrc)  // null terminated
{
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

Mon, 2004-10-25 17:14
Joined: 2004-05-26
Forum posts: 73
Locating line breaks in text.
asy: That was it! I was under the impression that the 0x2029 char was some sort of weird Unicode representation of a line feed.. I didn't know it came from the CEditableText class. I know replace those characters with "\n" characters when I read the strings, and it works like a charm.

Thanks!

-- arno
  • Login to reply to this topic.