Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ported over improvements to TEA.sugar's getWordAtIndex method (to pre…

…vent crashes in zero-length documents)
  • Loading branch information...
commit bacb41c8060a9b46f39d370a8d2e868c11fb4829 1 parent f6ef85f
@onecrayon authored
Showing with 11 additions and 12 deletions.
  1. +11 −12 ShellActions/Classes/NSObject+OCTextActionContextAdditions.m
View
23 ShellActions/Classes/NSObject+OCTextActionContextAdditions.m
@@ -33,12 +33,12 @@ - (NSString *)getWordAtIndex:(NSUInteger)cursor allowExtraCharacters:(NSCharacte
if (extraChars != nil) {
[legalChars formUnionWithCharacterSet:extraChars];
}
- NSString *lineText = nil;
+ NSString *lineText;
// Init non-pointer variables
- NSUInteger maxindex = [[self string] length] - 1;
+ NSInteger maxindex = [[self string] length] - 1;
unichar character;
- BOOL inword = NO;
+ BOOL inword;
NSInteger index = (NSInteger)cursor;
NSUInteger firstindex, lastindex;
// Vars for checking if the line ends with an HTML tag
@@ -47,12 +47,12 @@ - (NSString *)getWordAtIndex:(NSUInteger)cursor allowExtraCharacters:(NSCharacte
if (index < maxindex) {
// Check if index is mid-word
- character = [[self string] characterAtIndex:index+1];
+ character = [[self string] characterAtIndex:(NSUInteger) (index+1)];
if ([legalChars characterIsMember:character]) {
inword = TRUE;
// Parse forward until we hit the end of the word or document
while (inword) {
- character = [[self string] characterAtIndex:index];
+ character = [[self string] characterAtIndex:(NSUInteger) index];
// If word character, append and advance
if ([legalChars characterIsMember:character]) {
[word appendFormat:@"%C", character];
@@ -73,9 +73,9 @@ - (NSString *)getWordAtIndex:(NSUInteger)cursor allowExtraCharacters:(NSCharacte
}
// Set the last index of the word
if (index <= maxindex) {
- lastindex = index - 1;
+ lastindex = (NSUInteger) (index - 1);
} else {
- lastindex = index;
+ lastindex = (NSUInteger) index;
}
// Ready to go backward, so reset index to one less than cursor location
index = cursor - 1;
@@ -83,14 +83,14 @@ - (NSString *)getWordAtIndex:(NSUInteger)cursor allowExtraCharacters:(NSCharacte
if (index >= 0) {
inword = YES;
while (inword) {
- character = [[self string] characterAtIndex:index];
+ character = [[self string] characterAtIndex:(NSUInteger) index];
// Impossible for the line to the index to be an HTML tag if the character isn't a caret
if (character != '>') {
lineEndsWithTag = NO;
} else {
- linestart = [[self lineStorage] lineStartIndexLessThanIndex:index];
- lineText = [[self string] substringWithRange:NSMakeRange(linestart, index - linestart + 1)];
+ linestart = [[self lineStorage] lineStartIndexLessThanIndex:(NSUInteger) index];
+ lineText = [[self string] substringWithRange:NSMakeRange(linestart, (NSUInteger) (index - linestart + 1))];
// Not much point in loading up matches, so here's a regex approximation using NSPredicate
// NSPredicate regex test courtesy of: http://www.stiefels.net/2007/01/24/regular-expressions-for-nsstring/
// Using double backslashes to escape them
@@ -98,7 +98,6 @@ - (NSString *)getWordAtIndex:(NSUInteger)cursor allowExtraCharacters:(NSCharacte
NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
// Test it!
lineEndsWithTag = [regextest evaluateWithObject:lineText];
- lineText = nil;
}
if ([legalChars characterIsMember:character] && !lineEndsWithTag) {
@@ -113,7 +112,7 @@ - (NSString *)getWordAtIndex:(NSUInteger)cursor allowExtraCharacters:(NSCharacte
}
}
// Since index is left-aligned and we've overcompensated, need to increment +1
- firstindex = index + 1;
+ firstindex = (NSUInteger) (index + 1);
// Switch last index to length for use in range
lastindex = lastindex - firstindex;
Please sign in to comment.
Something went wrong with that request. Please try again.