Subj : Re: String concat problem To : netscape.public.mozilla.jseng From : Brendan Eich Date : Tue Apr 20 2004 11:37 am Niklas Lindquist wrote: > I have created a class exposing a string property. It works like a charm, > except when it comes to using it to build longer strings. > In JavaScript: > s = myobj.name; > s = 'Pre: ' + myobj.name; > s = myobj.name.length + ' post'; > all works fine, but: > s = myobj.name + ' post'; > only returns the name property and doesn't concatenate it with ' post'. I > can run toLowerCase() and similar functions on my string, so it seems to be > correct. > > I have initialized my string in the getProperty hook: > int length = ::strlen(pDoc->getName()) + 1; > char* buffer = reinterpret_cast(::JS_malloc(cx, length)); You should let length be strlen() and add 1 here in the malloc call. > ::strcpy(buffer, pDoc->getName()); > JSString *str = ::JS_NewString(cx, buffer, length); because you need to pass the length, not the size, here to JS_NewString. What you are doing now wrongly informs the JS engine that the string has a NUL included in its characters, at the end. That crops printing in the JS shell and any other 8-bit environment. /be > *vp = STRING_TO_JSVAL(str); > > Have I missed something? > > .