• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: getLineStart:end:contentsEnd:forRange:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: getLineStart:end:contentsEnd:forRange:


  • Subject: Re: getLineStart:end:contentsEnd:forRange:
  • From: Art Isbell <email@hidden>
  • Date: Tue, 10 Jul 2001 10:13:11 -1000

On Tuesday, July 10, 2001, at 04:14 AM, Michael Boulade wrote:

I take the output of an NSTask in a NSString. Now I'd like to divide it
into lines. Id'l like to use the getLineStart:end:contentsEnd:forRange:.
Can anyone explain me how the getLineStart:end:contentsEnd:forRange:
works and specially what is the aRange parameter (and the other
parameters too)?

aRange defines the range of characters that must be contained within the line returned by getLineStart:end:contentsEnd:forRange:. So if you have a string 100 characters in length, and you want to find the line that contains characters 49 - 51 (0-based):

unsigned startIndex;
unsigned lineEndIndex;
unsigned contentsEndIndex;
NSRange range = NSMakeRange(49, 3);

[string getLineStart:&startIndex end:&lineEndIndex contentsEnd:&contentsEndIndex forRange:range];

startIndex will now contain the 0-based index of the first character of the line *or lines* containing characters 49 - 51, lineEndIndex will contain the index of the first character past the line terminator, and contentsEndIndex will contain the index of the first character of the line terminator.

To split an entire string into lines:

unsigned stringLength = [string length];
unsigned startIndex;
unsigned lineEndIndex = 0;
unsigned contentsEndIndex;
NSRange range;
NSMutableArray *lines = [NSMutableArray array];

// There is more than one way to terminate this loop. Beware of an
// invalid termination test which might exist in this untested example :-)
while (lineEndIndex < stringLength)
{
// Include only a single character in range. Not sure whether
// this will work with empty lines, but if not, try a length of 0.
range = NSMakeRange(lineEndIndex, 1);
[string getLineStart:&startIndex end:&lineEndIndex contentsEnd:&contentsEndIndex forRange:range];

// If you want to exclude line terminators...
[lines addObject:[string substringWithRange:NSMakeRange(startIndex, contentsEndIndex - startIndex)]];
}

Note that this should correctly handle empty lines (i.e., more than one consecutive line terminator).

Also note that the above example isn't tested and probably contains errors, but it should get you started :-)

Art Isbell
Apple iServices Technical Support
http://www.apple.com/iservices/webobjectssupport/
+1-808-591-0836


References: 
 >getLineStart:end:contentsEnd:forRange: (From: Michael Boulade <email@hidden>)

  • Prev by Date: Re: NSThread question
  • Next by Date: Re: NSTask - grep
  • Previous by thread: getLineStart:end:contentsEnd:forRange:
  • Next by thread: Re: getLineStart:end:contentsEnd:forRange:
  • Index(es):
    • Date
    • Thread