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
This commit is contained in:
Niels Grewe 2015-09-04 09:48:07 +00:00
parent 252bbf1ba2
commit 17d379a59a
3 changed files with 32 additions and 6 deletions

View file

@ -1,3 +1,9 @@
2015-09-04 Niels Grewe <niels.grewe@halbordnung.de>
* 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 <niels.grewe@halbordnung.de>
* Source/NSRegularExpression.m: Fix handling of empty capture groups.

View file

@ -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
{

View file

@ -0,0 +1,24 @@
#import "ObjectTesting.h"
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSString.h>
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;
}