locale method fixup with testcase

This commit is contained in:
rfm 2024-02-11 15:03:37 +00:00
parent 6d1fe8fadf
commit be12e91109
3 changed files with 27 additions and 30 deletions

View file

@ -1,9 +1,10 @@
2024-02-06 Richard Frith-Macdonald <rfm@gnu.org>
2024-02-11 Richard Frith-Macdonald <rfm@gnu.org>
* Source/NSLocale.m: (+canonicalLocaleIdentifierFromString:)
Fix for #368 ... standardise hyphen to underscore then check for
presence of two underscores and delete the script identifier between
them, as suggested by the existing comments. Avoids the exception.
Fix for #368 ... then check for presence of huphen...underscore script
identifier and delete it, as suggested by the existing comments.
Avoids the exception.
* Tests/base/NSLocale/general.m: Simple testcase
2024-02-02 Richard Frith-Macdonald <rfm@gnu.org>

View file

@ -259,9 +259,8 @@ static NSRecursiveLock *classLock = nil;
zh-Hant_TW as it's locale identifier (was zh_TW on 10.3.9 and below).
Since ICU doesn't use "-" as a separator it will modify that identifier
to zh_Hant_TW. */
NSString *result;
NSMutableString *mStr;
NSRange range;
NSString *result;
NSRange range;
if (string == nil)
return nil;
@ -273,38 +272,30 @@ static NSRecursiveLock *classLock = nil;
if (result == nil)
result = string;
/* Strip script info (if present) from locale.
* We try to cope with zh-Hant_TW or zh_Hant-TW
/* Strip script info (if present) from hyphenated form.
* eg. try to cope with zh-Hant_TW
*/
mStr = nil;
range = [result rangeOfString: @"-"];
if (range.length > 0)
{
mStr = [NSMutableString stringWithString: result];
[mStr replaceString: @"-" withString: @"_"];
result = mStr;
}
range = [result rangeOfString: @"_"];
if (range.location != NSNotFound)
{
NSUInteger start = range.location;
NSUInteger start = range.location;
NSUInteger length = [result length];
range = [result rangeOfString: @"_" options: NSBackwardsSearch];
if (range.location != start)
range = [result rangeOfString: @"_"
options: 0
range: NSMakeRange(start, length - start)];
if (range.length > 0)
{
NSUInteger length = range.location - start;
if (nil == mStr)
{
mStr = [NSMutableString stringWithString: result];
}
NSMutableString *mStr;
/* Found -..._ sequence, so delete the script part.
*/
mStr = [NSMutableString stringWithString: result];
length = range.location - start;
[mStr deleteCharactersInRange: NSMakeRange(start, length)];
result = [NSString stringWithString: mStr];
}
}
if (mStr)
{
result = [NSString stringWithString: mStr];
}
return result;
}

View file

@ -20,6 +20,11 @@ int main(void)
if (!NSLOCALE_SUPPORTED)
SKIP("NSLocale not supported\nThe ICU library was not available when GNUstep-base was built")
PASS_EQUAL([NSLocale canonicalLocaleIdentifierFromString: @"es_ES"],
@"es_ES", "canonical version of es_ES")
PASS_EQUAL([NSLocale canonicalLocaleIdentifierFromString: @"zh-Hant_TW"],
@"zh_TW", "canonical version of zh-Hant_TW")
// These tests don't really work all that well. I need to come up with
// something better. Most of the ones that fail are because nil is returned.
testHopeful = YES;