Index: src/OFMutableString.m ================================================================== --- src/OFMutableString.m +++ src/OFMutableString.m @@ -293,9 +293,94 @@ tmp[tmp_len] = 0; [self freeMemory: string]; string = tmp; length = tmp_len; + + return self; +} + +- removeLeadingWhitespaces +{ + size_t i; + + for (i = 0; i < length; i++) + if (string[i] != ' ' && string[i] != '\t') + break; + + length -= i; + memmove(string, string + i, length); + string[length] = '\0'; + + @try { + string = [self resizeMemory: string + toSize: length + 1]; + } @catch (OFOutOfMemoryException *e) { + /* We don't really care, as we only made it smaller */ + [e dealloc]; + } + + return self; +} + +- removeTrailingWhitespaces +{ + size_t d; + char *p; + + d = 0; + for (p = string + length - 1; p >= string; p--) { + if (*p != ' ' && *p != '\t') + break; + + *p = '\0'; + d++; + } + + length -= d; + + @try { + string = [self resizeMemory: string + toSize: length + 1]; + } @catch (OFOutOfMemoryException *e) { + /* We don't really care, as we only made it smaller */ + [e dealloc]; + } + + return self; +} + +- removeLeadingAndTrailingWhitespaces +{ + size_t d, i; + char *p; + + d = 0; + for (p = string + length - 1; p >= string; p--) { + if (*p != ' ' && *p != '\t') + break; + + *p = '\0'; + d++; + } + + length -= d; + + for (i = 0; i < length; i++) + if (string[i] != ' ' && string[i] != '\t') + break; + + length -= i; + memmove(string, string + i, length); + string[length] = '\0'; + + @try { + string = [self resizeMemory: string + toSize: length + 1]; + } @catch (OFOutOfMemoryException *e) { + /* We don't really care, as we only made it smaller */ + [e dealloc]; + } return self; } @end Index: src/OFString.h ================================================================== --- src/OFString.h +++ src/OFString.h @@ -187,10 +187,25 @@ * \param repl The string with which it should be replaced */ - replaceOccurrencesOfString: (OFString*)str withString: (OFString*)repl; +/** + * Removes all whitespaces at the beginning of a string. + */ +- removeLeadingWhitespaces; + +/** + * Removes all whitespaces at the end of a string. + */ +- removeTrailingWhitespaces; + +/** + * Removes all whitespaces at the beginning and the end of a string. + */ +- removeLeadingAndTrailingWhitespaces; + /** * Splits an OFString into an OFArray of OFStrings. * * \param delimiter The delimiter for splitting * \return An autoreleased OFArray with the splitted string Index: src/OFString.m ================================================================== --- src/OFString.m +++ src/OFString.m @@ -358,10 +358,28 @@ andSelector: _cmd]; } - replaceOccurrencesOfString: (OFString*)str withString: (OFString*)repl +{ + @throw [OFNotImplementedException newWithClass: isa + andSelector: _cmd]; +} + +- removeLeadingWhitespaces +{ + @throw [OFNotImplementedException newWithClass: isa + andSelector: _cmd]; +} + +- removeTrailingWhitespaces +{ + @throw [OFNotImplementedException newWithClass: isa + andSelector: _cmd]; +} + +- removeLeadingAndTrailingWhitespaces { @throw [OFNotImplementedException newWithClass: isa andSelector: _cmd]; } Index: tests/OFString/OFString.m ================================================================== --- tests/OFString/OFString.m +++ tests/OFString/OFString.m @@ -23,11 +23,11 @@ #define ZD "%zd" #else #define ZD "%u" #endif -#define NUM_TESTS 27 +#define NUM_TESTS 33 #define SUCCESS \ printf("\r\033[1;%dmTests successful: " ZD "/%d\033[0m", \ (i == NUM_TESTS - 1 ? 32 : 33), i + 1, NUM_TESTS); \ fflush(stdout); #define FAIL \ @@ -122,9 +122,23 @@ s1 = [@"XX" mutableCopy]; [s1 replaceOccurrencesOfString: @"X" withString: @"XX"]; CHECK([s1 isEqual: @"XXXX"]) + s1 = [@" \t\t \tasd \t \t\t" mutableCopy]; + s2 = [s1 mutableCopy]; + s3 = [s1 mutableCopy]; + CHECK([[s1 removeLeadingWhitespaces] isEqual: @"asd \t \t\t"]) + CHECK([[s2 removeTrailingWhitespaces] isEqual: @" \t\t \tasd"]) + CHECK([[s3 removeLeadingAndTrailingWhitespaces] isEqual: @"asd"]) + + s1 = [@" \t\t \t\t \t \t" mutableCopy]; + s2 = [s1 mutableCopy]; + s3 = [s1 mutableCopy]; + CHECK([[s1 removeLeadingWhitespaces] isEqual: @""]) + CHECK([[s2 removeTrailingWhitespaces] isEqual: @""]) + CHECK([[s3 removeLeadingAndTrailingWhitespaces] isEqual: @""]) + puts(""); return 0; }