[Openmcl-devel] cocoa and unicode

Gary Byers gb at clozure.com
Wed Sep 22 09:53:49 PDT 2004

A "unichar" in the Cocoa libraries is an unsigned 16-bit integer.  I -think-
that that implies that a string composed of "unichar"s is UTF-16 encoded;
it's also possible that some of this code was written at a time when 16
bits was presumed to be enough bits to encode any interesting character (I
think that that was true in some earlier versions of the Unicode standard;
I think that you'd now need ~21 bits.)

I think that it's possible to map from 7-bit characters (ASCII or the
equivalent ISO charset) to UTF-16 without having to escape anything:

(defun 7-bit-string-to-utf-16 (s p)
  "p points to a block of memory that contains 2 bytes for each char in s"
  (do* ((i 0 (1+ i))
        (j 0 (+ j 2)))
       ((= i (length s)))
    (setf (%get-unsigned-word p j) (char-code (char s i)))))

If the #x80 bit is on in the lisp char code, it may be a little harder;
if OpenMCL actually supported Unicode, some of this would be easier (at
least for people -using- that support.)

On Wed, 22 Sep 2004, Raffael Cavallaro wrote:

> On Sep 21, 2004, at 11:09 PM, alex crain wrote:
> > Can someone put together an example for me on how I would complete
> > this expression:
> >
> > (make-instance 'ns:ns-attributed-string
> > 					 :with-string (make-instance 'ns:ns-string :with-characters
> > ??????)
> > 					 :attributes some-attributes))
> >
> > The question marks refer to an ojb-c unichar *
> >
> > Alternate approaches would be well received as well.
> How are you going to create these unicode characters? Are they fixed at
> compile time, or are you getting them from user input?
> If these strings are fixed at compile time, they are supposed to be in
> a string table (which you can create in XCode or Project Builder). The
> strings for each language live in the file:
> YourApp.app/Contents/Resources/SpecificLanguage.lproj/
> Localizable.strings
> wherre YourApp.app is your application's app bundle, and
> SpecificLanguage is French, Italian, Danish, etc.
> Then you should use [NSBundle mainBundle]
> localizedStringForKey:value:table:
> to retrieve the unicode string that corresponds to the english string
> key. This approach requires that the localized language be set to
> something other than english for it to work (i.e., the localized
> language on the machine running your app must be French to get the
> French unicode string).
> If you don't want this sort of localization behavior, or if you're not
> creating an app bundle,  you could create a file with the strings (or
> characters) that you want to use, and read them in with:
> - (id)initWithContentsOfFile:(NSString *)path
> but make sure that the file you're reading from has been saved in UTF8.
> If it is plain ASCII, then the string will be read in as a plain old c
> string, which is not what you want.
> If you know the bytes that the string is composed of, and the unicode
> encoding (for example, NSUTF8Encoding) then you could use
> – initWithBytes:length:encoding
> or
> – initWithBytesNoCopy:length:encoding:freeWhenDone:
> Finally, if you're getting these Unicode characters/strings from user
> input in an NSTextField, it should just work - i.e., the user will be
> able to enter any unicode characters they want by setting the
> appropriate preferences in the International prefpane. Then you get the
> NSString (which is unicode) by sending your NSTextField instance:
> stringValue
> I've never tried any of this from OpenMCL so I don't know how well this
> corner of Cocoa  is supported yet - Gary and Randall might know.
> regards,
> Ralph
> Raffael Cavallaro, Ph.D.
> raffaelcavallaro at mac.com

More information about the Openmcl-devel mailing list