зеркало из https://github.com/mozilla/gecko-dev.git
Patch from Max Horn to optimize 'stringByTruncatingTo'.
This commit is contained in:
Родитель
57378f7d93
Коммит
5b35359398
|
@ -50,7 +50,7 @@
|
||||||
if (!sEllipsisString)
|
if (!sEllipsisString)
|
||||||
{
|
{
|
||||||
unichar ellipsisChar = 0x2026;
|
unichar ellipsisChar = 0x2026;
|
||||||
sEllipsisString = [[[NSString alloc] initWithCharacters:&ellipsisChar length:1] retain];
|
sEllipsisString = [[NSString alloc] initWithCharacters:&ellipsisChar length:1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return sEllipsisString;
|
return sEllipsisString;
|
||||||
|
@ -131,34 +131,24 @@
|
||||||
|
|
||||||
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
|
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
|
||||||
{
|
{
|
||||||
NSString* ellipsisString = [NSString ellipsisString];
|
|
||||||
|
|
||||||
if ([self length] > maxCharacters)
|
if ([self length] > maxCharacters)
|
||||||
{
|
{
|
||||||
NSMutableString *croppedString = [NSMutableString stringWithCapacity:maxCharacters + [ellipsisString length]];
|
NSMutableString *croppedString = [self mutableCopy];
|
||||||
|
NSRange replaceRange;
|
||||||
|
replaceRange.length = [self length] - maxCharacters;
|
||||||
|
|
||||||
switch (truncationType)
|
switch (truncationType)
|
||||||
{
|
{
|
||||||
case kTruncateAtStart:
|
case kTruncateAtStart:
|
||||||
[croppedString appendString:ellipsisString];
|
replaceRange.location = 0;
|
||||||
[croppedString appendString:[self substringWithRange:NSMakeRange([self length] - maxCharacters, maxCharacters)]];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kTruncateAtMiddle:
|
case kTruncateAtMiddle:
|
||||||
{
|
replaceRange.location = maxCharacters / 2;
|
||||||
int len1 = maxCharacters / 2;
|
|
||||||
int len2 = maxCharacters - len1;
|
|
||||||
NSString *part1 = [self substringWithRange:NSMakeRange(0, len1)];
|
|
||||||
NSString *part2 = [self substringWithRange:NSMakeRange([self length] - len2, len2)];
|
|
||||||
[croppedString appendString:part1];
|
|
||||||
[croppedString appendString:ellipsisString];
|
|
||||||
[croppedString appendString:part2];
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kTruncateAtEnd:
|
case kTruncateAtEnd:
|
||||||
[croppedString appendString:[self substringWithRange:NSMakeRange(0, maxCharacters)]];
|
replaceRange.location = maxCharacters;
|
||||||
[croppedString appendString:ellipsisString];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -168,6 +158,7 @@
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[croppedString replaceCharactersInRange:replaceRange withString:[NSString ellipsisString]];
|
||||||
return croppedString;
|
return croppedString;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -50,7 +50,7 @@
|
||||||
if (!sEllipsisString)
|
if (!sEllipsisString)
|
||||||
{
|
{
|
||||||
unichar ellipsisChar = 0x2026;
|
unichar ellipsisChar = 0x2026;
|
||||||
sEllipsisString = [[[NSString alloc] initWithCharacters:&ellipsisChar length:1] retain];
|
sEllipsisString = [[NSString alloc] initWithCharacters:&ellipsisChar length:1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return sEllipsisString;
|
return sEllipsisString;
|
||||||
|
@ -131,34 +131,24 @@
|
||||||
|
|
||||||
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
|
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
|
||||||
{
|
{
|
||||||
NSString* ellipsisString = [NSString ellipsisString];
|
|
||||||
|
|
||||||
if ([self length] > maxCharacters)
|
if ([self length] > maxCharacters)
|
||||||
{
|
{
|
||||||
NSMutableString *croppedString = [NSMutableString stringWithCapacity:maxCharacters + [ellipsisString length]];
|
NSMutableString *croppedString = [self mutableCopy];
|
||||||
|
NSRange replaceRange;
|
||||||
|
replaceRange.length = [self length] - maxCharacters;
|
||||||
|
|
||||||
switch (truncationType)
|
switch (truncationType)
|
||||||
{
|
{
|
||||||
case kTruncateAtStart:
|
case kTruncateAtStart:
|
||||||
[croppedString appendString:ellipsisString];
|
replaceRange.location = 0;
|
||||||
[croppedString appendString:[self substringWithRange:NSMakeRange([self length] - maxCharacters, maxCharacters)]];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kTruncateAtMiddle:
|
case kTruncateAtMiddle:
|
||||||
{
|
replaceRange.location = maxCharacters / 2;
|
||||||
int len1 = maxCharacters / 2;
|
|
||||||
int len2 = maxCharacters - len1;
|
|
||||||
NSString *part1 = [self substringWithRange:NSMakeRange(0, len1)];
|
|
||||||
NSString *part2 = [self substringWithRange:NSMakeRange([self length] - len2, len2)];
|
|
||||||
[croppedString appendString:part1];
|
|
||||||
[croppedString appendString:ellipsisString];
|
|
||||||
[croppedString appendString:part2];
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kTruncateAtEnd:
|
case kTruncateAtEnd:
|
||||||
[croppedString appendString:[self substringWithRange:NSMakeRange(0, maxCharacters)]];
|
replaceRange.location = maxCharacters;
|
||||||
[croppedString appendString:ellipsisString];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -168,6 +158,7 @@
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[croppedString replaceCharactersInRange:replaceRange withString:[NSString ellipsisString]];
|
||||||
return croppedString;
|
return croppedString;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -50,7 +50,7 @@
|
||||||
if (!sEllipsisString)
|
if (!sEllipsisString)
|
||||||
{
|
{
|
||||||
unichar ellipsisChar = 0x2026;
|
unichar ellipsisChar = 0x2026;
|
||||||
sEllipsisString = [[[NSString alloc] initWithCharacters:&ellipsisChar length:1] retain];
|
sEllipsisString = [[NSString alloc] initWithCharacters:&ellipsisChar length:1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return sEllipsisString;
|
return sEllipsisString;
|
||||||
|
@ -131,34 +131,24 @@
|
||||||
|
|
||||||
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
|
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
|
||||||
{
|
{
|
||||||
NSString* ellipsisString = [NSString ellipsisString];
|
|
||||||
|
|
||||||
if ([self length] > maxCharacters)
|
if ([self length] > maxCharacters)
|
||||||
{
|
{
|
||||||
NSMutableString *croppedString = [NSMutableString stringWithCapacity:maxCharacters + [ellipsisString length]];
|
NSMutableString *croppedString = [self mutableCopy];
|
||||||
|
NSRange replaceRange;
|
||||||
|
replaceRange.length = [self length] - maxCharacters;
|
||||||
|
|
||||||
switch (truncationType)
|
switch (truncationType)
|
||||||
{
|
{
|
||||||
case kTruncateAtStart:
|
case kTruncateAtStart:
|
||||||
[croppedString appendString:ellipsisString];
|
replaceRange.location = 0;
|
||||||
[croppedString appendString:[self substringWithRange:NSMakeRange([self length] - maxCharacters, maxCharacters)]];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kTruncateAtMiddle:
|
case kTruncateAtMiddle:
|
||||||
{
|
replaceRange.location = maxCharacters / 2;
|
||||||
int len1 = maxCharacters / 2;
|
|
||||||
int len2 = maxCharacters - len1;
|
|
||||||
NSString *part1 = [self substringWithRange:NSMakeRange(0, len1)];
|
|
||||||
NSString *part2 = [self substringWithRange:NSMakeRange([self length] - len2, len2)];
|
|
||||||
[croppedString appendString:part1];
|
|
||||||
[croppedString appendString:ellipsisString];
|
|
||||||
[croppedString appendString:part2];
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kTruncateAtEnd:
|
case kTruncateAtEnd:
|
||||||
[croppedString appendString:[self substringWithRange:NSMakeRange(0, maxCharacters)]];
|
replaceRange.location = maxCharacters;
|
||||||
[croppedString appendString:ellipsisString];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -168,6 +158,7 @@
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[croppedString replaceCharactersInRange:replaceRange withString:[NSString ellipsisString]];
|
||||||
return croppedString;
|
return croppedString;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
|
@ -50,7 +50,7 @@
|
||||||
if (!sEllipsisString)
|
if (!sEllipsisString)
|
||||||
{
|
{
|
||||||
unichar ellipsisChar = 0x2026;
|
unichar ellipsisChar = 0x2026;
|
||||||
sEllipsisString = [[[NSString alloc] initWithCharacters:&ellipsisChar length:1] retain];
|
sEllipsisString = [[NSString alloc] initWithCharacters:&ellipsisChar length:1];
|
||||||
}
|
}
|
||||||
|
|
||||||
return sEllipsisString;
|
return sEllipsisString;
|
||||||
|
@ -131,34 +131,24 @@
|
||||||
|
|
||||||
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
|
- (NSString*)stringByTruncatingTo:(int)maxCharacters at:(ETruncationType)truncationType
|
||||||
{
|
{
|
||||||
NSString* ellipsisString = [NSString ellipsisString];
|
|
||||||
|
|
||||||
if ([self length] > maxCharacters)
|
if ([self length] > maxCharacters)
|
||||||
{
|
{
|
||||||
NSMutableString *croppedString = [NSMutableString stringWithCapacity:maxCharacters + [ellipsisString length]];
|
NSMutableString *croppedString = [self mutableCopy];
|
||||||
|
NSRange replaceRange;
|
||||||
|
replaceRange.length = [self length] - maxCharacters;
|
||||||
|
|
||||||
switch (truncationType)
|
switch (truncationType)
|
||||||
{
|
{
|
||||||
case kTruncateAtStart:
|
case kTruncateAtStart:
|
||||||
[croppedString appendString:ellipsisString];
|
replaceRange.location = 0;
|
||||||
[croppedString appendString:[self substringWithRange:NSMakeRange([self length] - maxCharacters, maxCharacters)]];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kTruncateAtMiddle:
|
case kTruncateAtMiddle:
|
||||||
{
|
replaceRange.location = maxCharacters / 2;
|
||||||
int len1 = maxCharacters / 2;
|
|
||||||
int len2 = maxCharacters - len1;
|
|
||||||
NSString *part1 = [self substringWithRange:NSMakeRange(0, len1)];
|
|
||||||
NSString *part2 = [self substringWithRange:NSMakeRange([self length] - len2, len2)];
|
|
||||||
[croppedString appendString:part1];
|
|
||||||
[croppedString appendString:ellipsisString];
|
|
||||||
[croppedString appendString:part2];
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case kTruncateAtEnd:
|
case kTruncateAtEnd:
|
||||||
[croppedString appendString:[self substringWithRange:NSMakeRange(0, maxCharacters)]];
|
replaceRange.location = maxCharacters;
|
||||||
[croppedString appendString:ellipsisString];
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -168,6 +158,7 @@
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[croppedString replaceCharactersInRange:replaceRange withString:[NSString ellipsisString]];
|
||||||
return croppedString;
|
return croppedString;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
Загрузка…
Ссылка в новой задаче