Skip to content
A convenient utility for splitting stings with a minimal memory footprint.
Objective-C
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
CBHStringSplitter.xcodeproj
CBHStringSplitter
CBHStringSplitterTests
.gitattributes
.gitignore
CBHStringSplitter.podspec
LICENSE
README.md

README.md

CBHStringSplitter

release pod licence coverage

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.

You can’t perform that action at this time.