diff --git a/ChangeLog b/ChangeLog index 751fbfd32..13047d535 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2015-09-04 Niels Grewe + + * Source/GSString.m: Optimisation for formatting strings that are stored as + UTF-16 (use memcpy instead of looping over the characters). + * Tests/base/NSString/unichar_format.m: Test cases for formatting. + 2015-09-01 Niels Grewe * Source/NSRegularExpression.m: Fix handling of empty capture groups. diff --git a/Source/GSString.m b/Source/GSString.m index 379b2f9c8..30bec69de 100644 --- a/Source/GSString.m +++ b/Source/GSString.m @@ -6267,12 +6267,8 @@ GSPrivateStrAppendUnichars(GSStr s, const unichar *u, unsigned l) */ if (s->_flags.wide == 1) { - unsigned i; - - for (i = 0; i < l; i++) - { - s->_contents.u[s->_count++] = u[i]; - } + memcpy(s->_contents.u + s->_count, u, l * sizeof(unichar)); + s->_count += l; } else { diff --git a/Tests/base/NSString/unichar_format.m b/Tests/base/NSString/unichar_format.m new file mode 100644 index 000000000..7e3f516c5 --- /dev/null +++ b/Tests/base/NSString/unichar_format.m @@ -0,0 +1,24 @@ +#import "ObjectTesting.h" +#import +#import + +int main() +{ + NSAutoreleasePool *arp = [NSAutoreleasePool new]; + NSString *narrow = @"aaaa"; // fits in a single byte + NSString *wide = @"a\u20AC\u20ACa"; // Euro signs, requires UTF-16 storage + NSString *narrowNarrowFormat = [NSString stringWithFormat: @"a%@a", narrow]; + NSString *narrowWideFormat = [NSString stringWithFormat: @"a%@a", wide]; + NSString *wideNarrowFormat = [NSString stringWithFormat: @"\u20AC%@\u20ac", narrow]; + NSString *wideWideFormat = [NSString stringWithFormat: @"\u20AC%@\u20ac", wide]; + PASS_EQUAL(narrowNarrowFormat, @"aaaaaa", + "Formatting byte-width string into a byte-width string works."); + PASS_EQUAL(narrowWideFormat, @"aa\u20AC\u20ACaa", + "Formatting a 16 bit wide string into a byte-width string works."); + PASS_EQUAL(wideNarrowFormat, @"\u20ACaaaa\u20AC", + "Formatting a byte-width string into a 16 bit wide string works."); + PASS_EQUAL(wideWideFormat, @"\u20ACa\u20AC\u20ACa\u20AC", + "Formatting a 16 bit wide string into a 16 bit wide string works."); + [arp release]; arp = nil; + return 0; +}