CBHStringSplitter
is an NSEnumerator
subclass which splits strings using an NSInputStream
to process input with minimal memory use.
While CBHStringSplitter
is slightly slower than the equivalent componentsSeparatedByCharactersInSet
it does not load the entire contents of a file into memory. This makes it more appropriate for some use cases.
NSString *path = @"/path/to/file";
CBHStringSplitter *splitter = [CBHStringSplitter splitterWithFileAtPath:path andSeparators:[NSCharacterSet newlineCharacterSet]];
for (NSString *line in splitter)
{
// Do something with the line...
}
NSString *path = @"/path/to/file";
CBHStringSplitter *splitter = [CBHStringSplitter splitterWithFileAtPath:path andSeparators:[NSCharacterSet newlineCharacterSet]];
NSString *line;
while ( (line = [splitter nextObject]) )
{
// Do something with the line...
}
- Unlike
componentsSeparatedByCharactersInSet
if the input is suffixed with a separator character an empty string will not be given as the last entry. It will just be ignored.
- Performance Improvements
- Simplify buffering
- Block based enumeration
Pull requests are welcome.
CBHStringSplitter is available under the ISC license.