[Issue 983] constant cfstrings for Darwin

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Feb 19 06:13:16 PST 2007


http://d.puremagic.com/issues/show_bug.cgi?id=983





------- Comment #2 from afb at algonet.se  2007-02-19 08:13 -------

Suggested usage of the built-in would be something like this:


/* Macro to allow creation of compile-time constant strings; the argument
should be a constant string.

CFSTR(), not being a "Copy" or "Create" function, does not return a new
reference for you. So, you should not release the return value. This is
much like constant C or Pascal strings --- when you use "hello world"
in a program, you do not free it.

However, strings returned from CFSTR() can be retained and released in a
properly nested fashion, just like any other CF type. That is, if you pass
a CFSTR() return value to a function such as SetMenuItemWithCFString(), the
function can retain it, then later, when it's done with it, it can release it.

At this point non-7 bit characters (that is, characters > 127) in CFSTR() are
not 
supported and using them will lead to unpredictable results. This includes
escaped
(\nnn) characters whose values are > 127. Even if it works for you in testing, 
it might not work for a user with a different language preference.
*/

/+
#ifdef __CONSTANT_CFSTRINGS__
#define CFSTR(cStr)  ((CFStringRef) __builtin___CFStringMakeConstantString (""
cStr ""))
#else
#define CFSTR(cStr)  __CFStringMakeConstantString("" cStr "")
#endif
+/
version (GNU_Constant_CFStrings) {
    private import gcc.builtins;
    alias __builtin___CFStringMakeConstantString CFSTR;
} else {
    extern(C) CFStringRef __CFStringMakeConstantString(char *cStr);
    alias __CFStringMakeConstantString CFSTR;
}


GCC toggles which version it uses with -fconstant-cfstrings (default)
or -fno-constant-cfstrings (default in earlier versions of Mac OS X)


-- 



More information about the D.gnu mailing list