Rewritten moving a word forward or backward

git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@12428 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
nico 2002-02-08 01:53:18 +00:00
parent f33d80bb1e
commit ddb3a8b91b

View file

@ -455,97 +455,79 @@ static Class converter_class(NSString *format, BOOL producer)
format: @"RangeError in method -nextWordFromIndex:forward:"]; format: @"RangeError in method -nextWordFromIndex:forward:"];
} }
/* Please note that we consider ' a valid word separator. This is
what Emacs does and is perfectly correct. If you want to change
the word separators, the right approach is to use a different
character set for word separators - the following code should be
unchanged whatever characters you use to separate words. */
cache_init (); cache_init ();
if (isForward) if (isForward)
{ {
/* What we want to do is: move forward to the next chunk of word
separator characters, skip them all, and return the location
just after them. */
if (location == length)
{
return length;
}
/* Move forward to the next word-separator. */
range = NSMakeRange (location, length - location); range = NSMakeRange (location, length - location);
range = [str rangeOfCharacterFromSet: wordBreakCSet range = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSLiteralSearch options: NSLiteralSearch
range: range]; range: range];
/* if (range.location == NSNotFound)
* If we found an apostrophe in the middle of a word,
* we have to skip forward.
*/
if (location > 0)
{
while (range.length > 0
&& range.location > 0 && range.location < length - 1
&& [str characterAtIndex: range.location] == '\''
&& [wordCSet characterIsMember:
[str characterAtIndex: range.location - 1]]
&& [wordCSet characterIsMember:
[str characterAtIndex: range.location + 1]])
{
location = range.location + 1;
range = NSMakeRange (location, length - location);
range = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSLiteralSearch range: range];
}
}
if (range.length == 0)
{ {
return length; return length;
} }
/* rangeOfCharacterFromSet:options:range: only returns the range
of the first word-separator character ... we want to skip
them all! So we need to search again, this time for the
first non-word-separator character, and return the first such
character. */
range = NSMakeRange (range.location, length - range.location); range = NSMakeRange (range.location, length - range.location);
range = [str rangeOfCharacterFromSet: wordCSet range = [str rangeOfCharacterFromSet: wordCSet
options: NSLiteralSearch options: NSLiteralSearch
range: range]; range: range];
if (range.length == 0) if (range.location == NSNotFound)
{ {
return length; return length;
} }
return range.location; return range.location;
} }
else else
{ {
BOOL inWord; /* What we want to do is: move backward to the next chunk of
non-word separator characters, skip them all, and return the
location just at the beginning of the chunk. */
inWord = [wordCSet characterIsMember: [str characterAtIndex: location]]; if (location == 0)
if (inWord == NO && location > 0
&& location > 0 && location < length - 1
&& [str characterAtIndex: location] == '\''
&& [wordCSet characterIsMember:
[str characterAtIndex: location - 1]]
&& [wordCSet characterIsMember:
[str characterAtIndex: location + 1]])
{ {
inWord = YES; return 0;
} }
/* Move backward to the next non-word separator. */
range = NSMakeRange (0, location); range = NSMakeRange (0, location);
range = [str rangeOfCharacterFromSet: wordCSet
options: NSBackwardsSearch | NSLiteralSearch
range: range];
if (range.location == NSNotFound)
{
return 0;
}
if (inWord == NO) /* rangeOfCharacterFromSet:options:range: only returns the range
{ of the first non-word-separator character ... we want to skip
range = [str rangeOfCharacterFromSet: wordCSet them all! So we need to search again, this time for the
options: NSBackwardsSearch | NSLiteralSearch first word-separator character. */
range: range]; range = NSMakeRange (0, range.location);
if (range.length == 0)
{
return 0;
}
range = NSMakeRange (0, range.location);
}
range = [str rangeOfCharacterFromSet: wordBreakCSet range = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSBackwardsSearch | NSLiteralSearch options: NSBackwardsSearch | NSLiteralSearch
range: range]; range: range];
while (range.length > 0 if (range.location == NSNotFound)
&& range.location > 0 && range.location < length - 1
&& [str characterAtIndex: range.location] == '\''
&& [wordCSet characterIsMember:
[str characterAtIndex: range.location - 1]]
&& [wordCSet characterIsMember:
[str characterAtIndex: range.location + 1]])
{
range = NSMakeRange (0, range.location - 1);
range = [str rangeOfCharacterFromSet: wordBreakCSet
options: NSBackwardsSearch|NSLiteralSearch range: range];
}
if (range.length == 0)
{ {
return 0; return 0;
} }