From 6f548d1c8ddab9a206afea3b6e42f50efe9d96cd Mon Sep 17 00:00:00 2001 From: thebeing Date: Fri, 4 Sep 2015 09:48:07 +0000 Subject: [PATCH] Optimise appending UTF-16 strings for formatting If the string being appended to already uses unichar as the underlying character type, append the new characters using memcpy() instead of looping over them individually. Also includes test cases to verify that nothing breaks. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@38969 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 6 ++++++ Source/GSString.m | 8 ++------ Tests/base/NSString/unichar_format.m | 24 ++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 Tests/base/NSString/unichar_format.m 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; +}