CBHStringSplitter
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.
Examples
Process each line in a file using a for in loop:
NSString *path = @"/path/to/file";
CBHStringSplitter *splitter = [CBHStringSplitter splitterWithFileAtPath:path andSeparators:[NSCharacterSet newlineCharacterSet]];
for (NSString *line in splitter)
{
// Do something with the line...
}
Process each line in a file using a while loop:
NSString *path = @"/path/to/file";
CBHStringSplitter *splitter = [CBHStringSplitter splitterWithFileAtPath:path andSeparators:[NSCharacterSet newlineCharacterSet]];
NSString *line;
while ( (line = [splitter nextObject]) )
{
// Do something with the line...
}
Notes:
- 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.
TODO:
- Performance Improvements
- Simplify buffering
- Block based enumeration
Pull requests are welcome.
Licence
CBHStringSplitter is available under the ISC license.