Implement -[NSString enumerateLinesUsingBlock:] (#407)

* Implement -[NSString enumerateLinesUsingBlock:]

* Fix formatting

* Use GNUstep CALL_BLOCK macro
This commit is contained in:
Hugo Melder 2024-06-04 22:19:45 +02:00 committed by GitHub
parent c498475110
commit 4d3926d250
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 133 additions and 1 deletions

View file

@ -6271,6 +6271,39 @@ static NSFileManager *fm = nil;
return [self rangeOfString: string].location != NSNotFound;
}
- (void) enumerateLinesUsingBlock: (GSNSStringLineEnumerationBlock)block
{
NSUInteger length;
NSUInteger lineStart, lineEnd, contentsEnd;
NSRange currentLocationRange;
BOOL stop;
length = [self length];
lineStart = lineEnd = contentsEnd = 0;
stop = NO;
// Enumerate through the string line by line
while (lineStart < length && !stop) {
NSString *line;
NSRange lineRange;
currentLocationRange = NSMakeRange(lineStart, 0);
[self getLineStart: &lineStart
end: &lineEnd
contentsEnd: &contentsEnd
forRange: currentLocationRange];
lineRange = NSMakeRange(lineStart, contentsEnd - lineStart);
line = [self substringWithRange: lineRange];
// Execute the block
CALL_BLOCK(block, line, &stop);
// Move to the next line
lineStart = lineEnd;
}
}
- (void) enumerateSubstringsInRange: (NSRange)range
options: (NSStringEnumerationOptions)opts
usingBlock: (GSNSStringEnumerationBlock)block