From af46212b241adc1a642bbdd20050eb4a0040922b Mon Sep 17 00:00:00 2001 From: jagapen Date: Tue, 20 Mar 2001 03:42:41 +0000 Subject: [PATCH] More NSString changes. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@9462 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++++++ Source/NSString.m | 46 ++++++++++++++++++++++++++++++---------------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/ChangeLog b/ChangeLog index 264c24328..d6552ae04 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,12 @@ * Source/NSString.m: Implement ([-localizedStringWithFormat:]). Fix ([-localizedCompare:]) and ([-localizedCaseInsensitiveCompare:]). (They will work when ([-compare:options:range:locale:]) works.) + Fix ([-initWithContentsOfFile:]) and ([-initWithContentsOfURL:]) so + that they deal with 1 byte of data, and clarify Unicode BOM test with + new class variables byteOrderMark and byteOrderMarkSwapped. + In ([-initWithData:encoding:]) and ([-initWithCoder:]), don't treat + the default C string encoding the same as ASCII. GCC 3.0 supports + UTF-8 as a C string encoding, so this behavior is undesirable. 2001-03-19 Jonathan Gapen diff --git a/Source/NSString.m b/Source/NSString.m index 6073e4bc3..e13227c04 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -222,6 +222,8 @@ pathSepMember(unichar c) @implementation NSString static NSStringEncoding _DefaultStringEncoding; +static const unichar byteOrderMark = 0xFFFE; +static const unichar byteOrderMarkSwapped = 0xFEFF; #if HAVE_REGISTER_PRINTF_FUNCTION #include @@ -998,8 +1000,7 @@ handle_printf_atsign (FILE *stream, - (id) initWithData: (NSData*)data encoding: (NSStringEncoding)encoding { - if ((encoding == [NSString defaultCStringEncoding]) - || (encoding == NSASCIIStringEncoding)) + if (encoding == NSASCIIStringEncoding) { unsigned len = [data length]; @@ -1091,19 +1092,26 @@ handle_printf_atsign (FILE *stream, { NSStringEncoding enc; NSData *d = [NSDataClass dataWithContentsOfFile: path]; - const unsigned char *test; + unsigned int len = [d length]; + const unichar *test; if (d == nil) return nil; - if ([d length] < 2) + + if (len == 0) return @""; + test = [d bytes]; - if (test != 0 && - (((test[0]==0xFF) && (test[1]==0xFE)) - || ((test[1]==0xFF) && (test[0]==0xFE)))) - enc = NSUnicodeStringEncoding; + if ((test != NULL) && (len > 1) + && ((test[0] == byteOrderMark) || (test[0] == byteOrderMarkSwapped))) + { + /* somebody set up us the BOM! */ + enc = NSUnicodeStringEncoding; + } else - enc = [NSString defaultCStringEncoding]; + { + enc = [NSString defaultCStringEncoding]; + } return [self initWithData: d encoding: enc]; } @@ -1111,19 +1119,25 @@ handle_printf_atsign (FILE *stream, { NSStringEncoding enc; NSData *d = [NSDataClass dataWithContentsOfURL: url]; + unsigned int len = [d length]; const unsigned char *test; if (d == nil) return nil; - if ([d length] < 2) + + if (len == 0) return @""; + test = [d bytes]; - if (test != 0 - && (((test[0]==0xFF) && (test[1]==0xFE)) - || ((test[1]==0xFF) && (test[0]==0xFE)))) - enc = NSUnicodeStringEncoding; + if ((test != NULL) && (len > 1) + && ((test[0] == byteOrderMark) || (test[0] == byteOrderMarkSwapped))) + { + enc = NSUnicodeStringEncoding; + } else - enc = [NSString defaultCStringEncoding]; + { + enc = [NSString defaultCStringEncoding]; + } return [self initWithData: d encoding: enc]; } @@ -3094,7 +3108,7 @@ handle_printf_atsign (FILE *stream, length: count freeWhenDone: YES]; } - else if (enc == NSASCIIStringEncoding || enc == _DefaultStringEncoding) + else if (enc == NSASCIIStringEncoding) { unsigned char *chars;