Re: How to get the length of a string?
Re: How to get the length of a string?
- Subject: Re: How to get the length of a string?
- From: Rainer Brockerhoff <email@hidden>
- Date: Tue, 21 Aug 2001 19:33:11 -0300
>
Date: Tue, 21 Aug 2001 11:28:19 -0700
>
From: Aki Inoue <email@hidden>
>
>
It's not just future proof, but required.
>
>
The filesystem representation on Mac OS X is DECOMPOSED (or Form D)
>
UTF-8.
>
-UTF8String just convert NSString/CFString to UTF8 where
>
fileSystemRepresentation also decomposes on the fly.
Decomposed, AFAIK, means that accented characters are split into base characters and accents, right? I was under the impression that UTF8String also did decomposition. I'll go over my previous code and check this out.
>
On 2001.08.21, at 11:02, Jim Correia wrote:
>
>
> At 1:20 PM -0300 8/21/01, Rainer Brockerhoff wrote:
>
>
>
>> If you're handling file paths, always use:
>
>> n = strlen([my_string UTF8String]);
>
>
>
> This may be splitting hairs, but if you want a file system path encoded
>
> string, you should use fileSystemRepresentation. Yes, today on Mac OS
>
> X this is UTF8 encoded, but if you use this message your code should be
>
> future proof.
In all tests I did, the only difference today (emphasis on _today_) between writing:
[mystring UTF8String]
and writing
[[NSFileManager defaultManager] fileSystemRepresentationWithPath:my_string]
seemed to be that the second form cleans up the path to it's canonical form - removing redundant slashes and so forth. Granted I don't have a wealth of examples to test. I'm glad to be corrected before I publish something :-)
According to the docs, the converse operation, stringWithFileSystemRepresentation:length:, expects a C string (NOT an UTF8-string) as input. But the file routines which read a POSIX path state they need UTF8-strings. I suppose the docs are wrong on that point?
And if they are, what length do I pass in the length: parameter, the one calculated by strlen()?
Still on that subject, many file routines need a HFSUniStr255 filename. I've been converting this to an NSString as follows:
HFSUniStr255 ustr;
NSString* fileName;
ustr->length = CFStringGetLength((CFStringRef) fileName);
CFStringGetCharacters((CFStringRef)fileName,CFRangeMake(0,ustr->length),ustr->unicode);
Will this handle UTF8 decomposition correctly, or is there a better way?
Thanks for your comments...
--
Rainer Brockerhoff <email@hidden>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."
http://www.brockerhoff.net/ (updated July 2000)