Re: Extracting lines from a buffer
Re: Extracting lines from a buffer
- Subject: Re: Extracting lines from a buffer
- From: Pandaa <email@hidden>
- Date: Thu, 29 Jul 2004 15:51:50 +0200
2004-07-29 kl. 04.12 skrev Bruce Truax:
What is the easiest way to extract lines of text from a buffer. I
thought
that was what
[stringBuffer getLineStart:&start
end:&end
contentsEnd:&contentsEnd
forRange:entireBufferRange];
Was supposed to do, but it always returns the entire range of buffer.
getLineStart:end:contentsEnd:forRange: returns the lines a character
range occupy. So passing the entire buffer range returns the range of
all lines. From the documentation:
"Returns by reference the indexes of the smallest range of lines
containing aRange. ...
<cut>
When this method returns, startIndex contains the index of the first
character of the line, which is at or before the location of aRange;
lineEndIndex contains the index of the first character past the line
terminator; and contentsEndIndex contains the index of the first
character of the line terminator itself. ...
"
So this would be useful if you wanted to get the lines enclosing a
range of characters.
What
I plan to do is use rangeOfString:options:range: to locate the "\n"
first \n
character, create a range which goes from the first character of the
buffer
to the value returned by rangeOfString:options:range: and extract the
range
using substringWithRange: I am sure this will work, but I would think
that
there is a simpler way.
You could use NSScanner's scanUpToCharactersFromSet:intoString: to
iterate over the lines.
NSScanner *scanner = [NSScanner scannerWithString:inputString];
NSString *line = nil;
NSCharacterSet newlineCharacterSet = [NSCharacterSet
characterSetWithCharactersInString:@"\r\n"];
while ( [scanner scanUpToCharactersFromSet:newlineCharacterSet
intoString:&line] )
{
// do something with line here
[scanner scanCharactersFromSet:newlineCharacterSet intoString:nil]; //
scan past any empty lines or \r\n sequence
}
. . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . .
. email@hidden . . www.synapticpulse.net .
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.