[Libreoffice] OString += OString + char ... ?

Kevin Hunter hunteke at earlham.edu
Sun Feb 27 01:27:26 PST 2011

Hullo List,

I hope this isn't a silly question, but I'm a little confused by a 
current behavior of the O*String classes, and wondering if it's A) 
known, and B) expected.

Please consider this code snippet:

using ::rtl::OString;

OString a( "My Test String: " );
OString b( "Gobble Wobble" );
OSL_TRACE( "\n\n" );
OSL_TRACE( "a  : %s", a.getStr() );
OSL_TRACE( "b  : %s", b.getStr() );

a += b;
OSL_TRACE( "a+b: %s", a.getStr() );

// a += '\n';                         Does not work.  Should it?

a += b + '\n';                     // This is the suspect line
OSL_TRACE( "a+n: %s", a.getStr() );

Thread:     10 :a  : My Test String:
Thread:     10 :b  : Gobble Wobble
Thread:     10 :a+b: My Test String: Gobble Wobble
Thread:     10 :a+n: My Test String: Gobble Wobbleble

The a+b line is as expected, but adding a newline single character seems 
to break.  I expected to receive this:

"a+n: My Test String: Gobble WobbleGobbleWobble

(Note the newline at the end, and repeated "Gobble Wobble" bit.)

Is that + operator expected to work with an O*String and a single character?

 From analysis with Sébastien Le Ray, it looks like what's happening is 
that the b OString is converted to sal_Char *, and the compiler uses 
int( '\n' ) (=10) for the + operator.  So, the pointer of "Gobble 
Wobble" is moved to "ble" before the += operator takes over.

Changing the suspect line to

a += b + "\n";

gives me the results I was originally expecting. (double quotes " for 
the single quotes ').

Is this expected behavior?  It's admittedly a corner case, but I'm 
wondering if the is a bug in the API.



P.S. I'm aware that I should be using O*StringBuffer for concatenation, 
but that doesn't detract from this issue.

More information about the LibreOffice mailing list