• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Any way to get 16-bit string literals?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Any way to get 16-bit string literals?


  • Subject: Re: Any way to get 16-bit string literals?
  • From: Chris Espinosa <email@hidden>
  • Date: Mon, 5 Oct 2009 10:50:31 -0700


On Oct 2, 2009, at 11:44 AM, Jens Alfke wrote:

C++ has a 'wide string literal' syntax that looks like L"some string", which creates an array of wchar_t. Unfortunately wchar_t is 32 bits wide, not 16. It's possible to use a #pragma to make GCC change wchar_t to 16 bit, but that breaks compatibility with all the standard library calls that operate on wide strings.

So: Is there any clever way to get the compiler to generate a 16-bit string literal? (I've looked at the CFSTR() macro, which has a very similar purpose, but it seems to use a special-purpose hack in GCC that only generates CFString objects.)

The best way to do this would be to isolate the string literals into a single file, build that file with -fshort-wchar, and typecast them to UniChar.


e.g.:

-----------------------------------------------------------------------
StaticStrings.h:

extern const uint16_t string1[];
extern const wchar_t *string2;

-----------------------------------------------------------------------
StaticStrings.cpp:

const uint16_t string1[] = {'H','e','l','l','o',',',' ','W','o','r','l','d','!','\n',0};
const wchar_t * string2= L"Hello, World";


-----------------------------------------------------------------------
main.cp:

#include "StaticStrings.h"

int main (int argc, char * const argv[]) {
std::cout << "String 1 is !\n" << string1 << " and string 2 is " << string2 << "\n";
return 0;
}


-----------------------------------------------------------------------

If you build normally and open string1 and string2 in the Memory Browser, you'll see that string1 starts off 0x47 0x00 0x65 0x00 while string2 is 0x47 0x00 0x00 0x00 0x65 0x00 0x00 0x00. But if you add - fshort-wchar to StaticStrings.cpp alone and rebuild, string1 and string2 are bytewise identical.

You can't really do anything further with the uint16_t or wchar_t except convert them to NSStrings.

And if your const strings are widely scattered throughout source files, this won't work; you need to compile just the definitions with - fshort-wchar.

Chris
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Any way to get 16-bit string literals? (From: Jens Alfke <email@hidden>)

  • Prev by Date: RE: Any way to get 16-bit string literals?
  • Next by Date: Re: Nib exploded (according to SCM)
  • Previous by thread: Any way to get 16-bit string literals?
  • Next by thread: re: Any way to get 16-bit string literals?
  • Index(es):
    • Date
    • Thread