getLineStart(_:end:contentsEnd:for:)
Returns by reference the beginning of the first line and the end of the last line touched by the given range.
Declaration
func getLineStart(_ startPtr: UnsafeMutablePointer<Int>?, end lineEndPtr: UnsafeMutablePointer<Int>?, contentsEnd contentsEndPtr: UnsafeMutablePointer<Int>?, for range: NSRange)Parameters
- startPtr:
Upon return, contains the index of the first character of the line containing the beginning of
aRange. PassNULLif you do not need this value (in which case the work to compute the value isn’t performed). - lineEndPtr:
Upon return, contains the index of the first character past the terminator of the line containing the end of
aRange. PassNULLif you do not need this value (in which case the work to compute the value isn’t performed). - contentsEndPtr:
Upon return, contains the index of the first character of the terminator of the line containing the end of
aRange. PassNULLif you do not need this value (in which case the work to compute the value isn’t performed). - range:
A range within the receiver. The value must not exceed the bounds of the receiver.
Raises an
NSRangeExceptionifaRangeis invalid.
Discussion
A line is delimited by any of these characters, the longest possible sequence being preferred to any shorter:
U+000AUnicode Character ‘LINE FEED (LF)’ (\n)U+000DUnicode Character ‘CARRIAGE RETURN (CR)’ (\r)U+0085Unicode Character ‘NEXT LINE (NEL)’U+2028Unicode Character ‘LINE SEPARATOR’U+2029Unicode Character ‘PARAGRAPH SEPARATOR’\r\n, in that order (also known asCRLF)
If aRange is contained with a single line, of course, the returned indexes all belong to that line. You can use the results of this method to construct ranges for lines by using the start index as the range’s location and the difference between the end index and the start index as the range’s length.
Special Considerations
This method detects all invalid ranges (including those with negative lengths). For applications linked against macOS 10.6 and later, this error causes an exception; for applications linked against earlier releases, this error causes a warning, which is displayed just once per application execution.