Index: src/OFConstantString.m ================================================================== --- src/OFConstantString.m +++ src/OFConstantString.m @@ -642,6 +642,16 @@ if (initialized != SIZE_MAX) [self finishInitialization]; return [super writeToFile: path]; } + +#ifdef OF_HAVE_BLOCKS +- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block +{ + if (initialized != SIZE_MAX) + [self finishInitialization]; + + return [super enumerateLinesUsingBlock: block]; +} +#endif @end Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -39,10 +39,14 @@ OF_STRING_ENCODING_AUTODETECT = 0xFF } of_string_encoding_t; /* FIXME */ #define OF_STRING_ENCODING_NATIVE OF_STRING_ENCODING_UTF_8 + +#ifdef OF_HAVE_BLOCKS +typedef void (^of_string_line_enumeration_block_t)(OFString *line, BOOL *stop); +#endif #ifdef __cplusplus extern "C" { #endif extern int of_string_check_utf8(const char*, size_t, size_t*); @@ -797,14 +801,23 @@ * \brief Writes the string into the specified file using UTF-8 encoding. * * \param path The path of the file to write to */ - (void)writeToFile: (OFString*)path; + +#ifdef OF_HAVE_BLOCKS +/** + * Enumerates all lines in the receiver using the specified block. + * + * \brief block The block to call for each line + */ +- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block; +#endif @end #import "OFConstantString.h" #import "OFMutableString.h" #import "OFString+Hashing.h" #import "OFString+Serialization.h" #import "OFString+URLEncoding.h" #import "OFString+XMLEscaping.h" #import "OFString+XMLUnescaping.h" Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -1925,6 +1925,45 @@ mode: @"wb"]; [file writeString: self]; [pool release]; } + +#ifdef OF_HAVE_BLOCKS +- (void)enumerateLinesUsingBlock: (of_string_line_enumeration_block_t)block +{ + OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init]; + const char *cString = s->cString; + const char *last = cString; + BOOL stop = NO, lastCarriageReturn = NO; + + while (!stop && *cString != 0) { + if (lastCarriageReturn && *cString == '\n') { + lastCarriageReturn = NO; + + cString++; + last++; + + continue; + } + + if (*cString == '\n' || *cString == '\r') { + block([OFString + stringWithUTF8String: last + length: cString - last], &stop); + last = cString + 1; + + [pool releaseObjects]; + } + + lastCarriageReturn = (*cString == '\r'); + cString++; + } + + if (!stop) + block([OFString stringWithUTF8String: last + length: cString - last], &stop); + + [pool release]; +} +#endif @end