Jon Smirl wrote:

> I haven't been following the string API changes for the last couple of
> months and my old code doesn't compile anymore. What is the current accepted
> way for doing:


A summary of the answers already given + some extra tidbits of info:

 
> 1) get a "const nsAReadableString&" into a buffer suitable to pass to the
> OS. My string is Unicode and the OS call wants Unicode too. Does this
> require a copy or is there a trick using nsPromiseFlatString where I can get
> a buffer pointer for the 99.9% of time the string is only in one buffer? I
> was doing this: "(void*)(const PRUnichar*)nsPromiseFlatString(value)"


PromiseFlatString(value).get() (no, not a typo) will give you a |const 
PRUnichar*| which is pretty cheap if |value| was (known to be) flat to begin 
with, and will copy when unknown.


> 2) I have a PRUnichar* to a string. I want to pass this to a routine that
> takes "const nsAReadableString&". The routine being called is going to copy
> the string so I just want to make a light weight wrapper around the
> PRUnichar*. I was using temp nsLiteralString variables to do this.


Like said elsewhere:

foo(nsDependentString(uniPtr, length)); (you can leave the length off and 

it will determine it for you, but if you happen to know it it's cheaper to 

pass it in.


> 3) Is there a macro for an empty string? I was using this: static
> nsLiteralString static_empty(L"", 0);


NS_LITERAL_STRING("") is the cross platform way of doing this. On most 
platforms it will expand to what you just wrote, on others it will expand to 
NS_ConvertASCIItoUCS2("", 0).

 
> 4) I need to return an interface to a constant string. Old code....
> const nsAReadableString& Template::getTemplateName() {
>  static nsLiteralString xmlfile(NS_LITERAL_STRING("XMLFile"));
>   return xmlfile;
> }

dbaron: Know a way to do this better than |static 
NS_NAMED_LITERAL_STRING(xmlfile, "XMLFile");| ?

   jag


Reply via email to