CFStringGetLineBounds(_:_:_:_:_:)
Given a range of characters in a string, obtains the line bounds—that is, the indexes of the first character and the final characters of the lines containing the range.
Declaration
func CFStringGetLineBounds(_ theString: CFString!, _ range: CFRange, _ lineBeginIndex: UnsafeMutablePointer<CFIndex>!, _ lineEndIndex: UnsafeMutablePointer<CFIndex>!, _ contentsEndIndex: UnsafeMutablePointer<CFIndex>!)Parameters
- theString:
The string containing the specified range of characters.
- range:
The range of characters to consider. The specified range must not exceed the length of the string.
- lineBeginIndex:
On return, the index of the first character of the containing line. Pass
NULLif you do not want this result. - lineEndIndex:
On return, the index of the first character of the line after the specified range. Pass
NULLif you do not want this result. - contentsEndIndex:
On return, the index of the last character of the containing line, excluding any line-separator characters. Pass
NULLif you are not interested in this result.
Discussion
This function is a convenience function for determining the beginning and ending indexes of one or more lines in the given range of a string. It is useful, for example, when each line represents a “record” of some sort; you might search for some substring, but want to extract the record of which the substring is a part.
To determine line separation, the function looks for the standard line-separator characters: carriage returns (CR and CRLF), linefeeds (LF), and Unicode line and paragraph separators. The three final parameters of the function indirectly return, in order, the index of the first character that starts the line, the index of the first character of the next line (including end-of-line characters), and the index of the last character of the line (excluding end-of-line characters). Pass NULL for any of these parameters if you aren’t interested in the result.
To determine the number of characters in the line:
Subtract
lineBeginIndexfromlineEndIndexto find the number of characters in the line, including the line separators.Subtract
lineBeginIndexfromcontentsEndIndexto find the number of characters in the line, excluding the line separators.