From 3c2b3f6699fb2697425cfc75c8a8dcff2f3c06b2 Mon Sep 17 00:00:00 2001 From: CaS Date: Thu, 19 Jan 2006 13:28:02 +0000 Subject: [PATCH] Various minor fixes and optimisations ... see ChangeLog git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@22334 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 8 +++ Source/GSString.m | 149 ++++++++++++++++++++++++++++++++++------------ Source/NSString.m | 90 +++++++++++++++++++++++----- Source/NSURL.m | 2 +- Tools/plget.m | 1 - 5 files changed, 197 insertions(+), 53 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5ca5f9571..b56086d20 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2006-01-19 Richard Frith-Macdonald + + * Tools/plget.m: remove unused variable + * Source/GGSString.m: optimise ([canBeConvertedToEncoding:]) + * Source/NSURL.m: correct misleading/wrong exception text. + * Source/NSString.m: mingw path handling tweaks ... build new + paths using native separator by default. + 2006-01-19 Richard Frith-Macdonald * Headers/Foundation/NSThread.h: diff --git a/Source/GSString.m b/Source/GSString.m index cacc40da1..b54207995 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -797,60 +797,135 @@ boolValue_u(GSStr self) static inline BOOL canBeConvertedToEncoding_c(GSStr self, NSStringEncoding enc) { - if (enc == intEnc) - { - return YES; - } - else - { - BOOL result = (*convertImp)((id)self, convertSel, enc); + unsigned c = self->_count; + BOOL result = YES; - return result; + /* + * If the length is zero, or we are already using the required encoding, + * or the required encoding is unicode (can hold any character) then we + * can assume that a conversion would succeed. + * We also know a conversion must succeed if the internal encoding is + * ascii and the required encoding has ascii as a subset. + */ + if (c > 0 + && enc != intEnc + && enc != NSUTF8StringEncoding + && enc != NSUnicodeStringEncoding + && ((intEnc != NSASCIIStringEncoding) + || ((enc != NSISOLatin1StringEncoding) + && (enc != NSISOLatin2StringEncoding) + && (enc != NSNEXTSTEPStringEncoding) + && (enc != NSNonLossyASCIIStringEncoding)))) + { + unsigned l = 0; + unichar *r = 0; + + /* + * To check whether conversion is possible, we first convert to + * unicode and then check to see whether it is possible to convert + * to the desired encoding. + */ + result = GSToUnicode(&r, &l, self->_contents.c, self->_count, intEnc, + NSDefaultMallocZone(), GSUniStrict); + if (result == YES) + { + if (enc == NSISOLatin1StringEncoding) + { + unsigned i; + + /* + * If all the unicode characters are in the 0 to 255 range + * they are all latin1. + */ + for (i = 0; i < l; i++) + { + if (r[i] > 255) + { + result = NO; + break; + } + } + } + else if (enc == NSASCIIStringEncoding) + { + unsigned i; + + /* + * If all the unicode characters are in the 0 to 127 range + * they are all ascii. + */ + for (i = 0; i < l; i++) + { + if (r[i] > 127) + { + result = NO; + break; + } + } + } + else + { + unsigned dummy = 0; // Hold returned length. + + result = GSFromUnicode(0, &dummy, r, l, enc, 0, GSUniStrict); + } + + // Temporary unicode string no longer needed. + NSZoneFree(NSDefaultMallocZone(), r); + } } + return result; } static inline BOOL canBeConvertedToEncoding_u(GSStr self, NSStringEncoding enc) { - BOOL result = YES; + unsigned c = self->_count; + BOOL result = YES; - if (enc == NSISOLatin1StringEncoding) + if (c > 0 && enc != NSUTF8StringEncoding && enc != NSUnicodeStringEncoding) { - unsigned i; - - /* - * If all the unicode characters are in the 0 to 255 range - * they are all latin1. - */ - for (i = 0; i < self->_count; i++) + if (enc == NSISOLatin1StringEncoding) { - if (self->_contents.u[i] > 255) + unsigned i; + + /* + * If all the unicode characters are in the 0 to 255 range + * they are all latin1. + */ + for (i = 0; i < self->_count; i++) { - result = NO; - break; + if (self->_contents.u[i] > 255) + { + result = NO; + break; + } } } - } - else if (enc == NSASCIIStringEncoding) - { - unsigned i; - - /* - * If all the unicode characters are in the 0 to 127 range - * they are all ascii. - */ - for (i = 0; i < self->_count; i++) + else if (enc == NSASCIIStringEncoding) { - if (self->_contents.u[i] > 127) + unsigned i; + + /* + * If all the unicode characters are in the 0 to 127 range + * they are all ascii. + */ + for (i = 0; i < self->_count; i++) { - result = NO; - break; + if (self->_contents.u[i] > 127) + { + result = NO; + break; + } } } - } - else - { - result = (*convertImp)((id)self, convertSel, enc); + else + { + unsigned dummy = 0; // Hold returned length. + + result = GSFromUnicode(0, &dummy, self->_contents.u, c, enc, + 0, GSUniStrict); + } } return result; } diff --git a/Source/NSString.m b/Source/NSString.m index de1fcd550..0ac47f1e2 100644 --- a/Source/NSString.m +++ b/Source/NSString.m @@ -219,6 +219,42 @@ pathSepMember(unichar c) return NO; } +inline static unichar +pathSepChar() +{ +#if defined(__MINGW32__) + if (GSPathHandlingUnix() == YES) + { + return '/'; + } + return '\\'; +#else + if (GSPathHandlingWindows() == YES) + { + return '\\'; + } + return '/'; +#endif +} + +inline static NSString* +pathSepString() +{ +#if defined(__MINGW32__) + if (GSPathHandlingUnix() == YES) + { + return @"/"; + } + return @"\\"; +#else + if (GSPathHandlingWindows() == YES) + { + return @"\\"; + } + return @"/"; +#endif +} + /* * Find end of 'root' sequence in a string. Characters before this * point in the string cannot be split into path components/extensions. @@ -3674,7 +3710,7 @@ static NSFileManager *fm = nil; { length--; } - buf[length++] = '/'; + buf[length++] = pathSepChar(); } if ((aLength - root) > 0) @@ -3704,12 +3740,12 @@ static NSFileManager *fm = nil; { if (pathSepMember(buf[aLength]) == YES) { - buf[aLength] = '/'; // Standardise + buf[aLength] = pathSepChar(); if (pathSepMember(buf[aLength-1]) == YES) { unsigned pos; - buf[aLength-1] = '/'; // Standardise + buf[aLength-1] = pathSepChar(); for (pos = aLength+1; pos < length; pos++) { buf[pos-1] = buf[pos]; @@ -4347,7 +4383,26 @@ static NSFileManager *fm = nil; { s = AUTORELEASE([self mutableCopy]); } - [s replaceString: @"\\" withString: @"/"]; +#if defined(__MINGW32__) + if (GSPathHandlingUnix() == YES) + { + [s replaceString: @"\\" withString: @"/"]; + } + else + { + [s replaceString: @"/" withString: @"\\"]; + } +#else + if (GSPathHandlingWindows() == YES) + { + [s replaceString: @"/" withString: @"\\"]; + } + else + { + [s replaceString: @"\\" withString: @"/"]; + } +#endif + l = [s length]; root = rootOf(s, l); @@ -4375,11 +4430,14 @@ static NSFileManager *fm = nil; } // Condense ('/./') sequences. r = (NSRange){root, l-root}; - while ((r = [s rangeOfString: @"/." options: 0 range: r]).length == 2) + while ((r = [s rangeOfString: @"." options: 0 range: r]).length == 1) { - if (NSMaxRange(r) == l || - pathSepMember((*caiImp)(s, caiSel, NSMaxRange(r))) == YES) + if (r.location > 0 + && pathSepMember((*caiImp)(s, caiSel, r.location-1)) == YES + && (NSMaxRange(r) == l + || pathSepMember((*caiImp)(s, caiSel, NSMaxRange(r))) == YES)) { + r.length++; [s deleteCharactersInRange: r]; l -= r.length; } @@ -4391,7 +4449,7 @@ static NSFileManager *fm = nil; } // Strip trailing '/' if present. - if (l > root && [s hasSuffix: @"/"]) + if (l > root && pathSepMember([s characterAtIndex: l - 1]) == YES) { r.length = 1; r.location = l - r.length; @@ -4418,11 +4476,15 @@ static NSFileManager *fm = nil; #if defined(__MINGW32__) /* Condense `/../' */ r = (NSRange){root, l-root}; - while ((r = [s rangeOfString: @"/.." options: 0 range: r]).length == 3) + while ((r = [s rangeOfString: @".." options: 0 range: r]).length == 2) { - if (NSMaxRange(r) == l || - pathSepMember((*caiImp)(s, caiSel, NSMaxRange(r))) == YES) + if (r.location > 0 + && pathSepMember((*caiImp)(s, caiSel, r.location-1)) == YES + && (NSMaxRange(r) == l + || pathSepMember((*caiImp)(s, caiSel, NSMaxRange(r))) == YES)) { + r.location--; + r.lenght++; if (r.location > root) { NSRange r2 = {root, r.location-root}; @@ -4550,7 +4612,7 @@ static NSFileManager *fm = nil; s = [components objectAtIndex: 0]; if ([s length] == 0) { - s = @"/"; + s = pathSepString(); } for (i = 1; i < c; i++) { @@ -4579,7 +4641,7 @@ static NSFileManager *fm = nil; * Any string beginning with '/' is absolute ... except in windows mode * or on windows and not in unix mode. */ - if (c == '/') + if (c == pathSepChar()) { #if defined(__MINGW32__) if (GSPathHandlingUnix() == YES) @@ -4662,7 +4724,7 @@ static NSFileManager *fm = nil; */ if (l > root && pathSepMember([s characterAtIndex: l-1])) { - [a addObject: @"/"]; + [a addObject: pathSepString()]; } r = [a copy]; diff --git a/Source/NSURL.m b/Source/NSURL.m index 9e90a8926..3616f2e6d 100644 --- a/Source/NSURL.m +++ b/Source/NSURL.m @@ -825,7 +825,7 @@ static unsigned urlAlign; if (legal(buf->host, "-") == NO) { [NSException raise: NSGenericException format: - @"illegal character in user/password part"]; + @"illegal character in host part"]; } /* diff --git a/Tools/plget.m b/Tools/plget.m index c3ea8ce84..1cbe355e9 100644 --- a/Tools/plget.m +++ b/Tools/plget.m @@ -75,7 +75,6 @@ main(int argc, char** argv, char **env) NSFileHandle *fileHandle; NSData *inputData; NSString *inputString; - NSDictionary *dictionary; NSData *outputData; NS_DURING