diff --git a/SoObjects/SOGo/NSString+Utilities.h b/SoObjects/SOGo/NSString+Utilities.h index 8b9d0dc81..78af47b7b 100644 --- a/SoObjects/SOGo/NSString+Utilities.h +++ b/SoObjects/SOGo/NSString+Utilities.h @@ -74,6 +74,8 @@ - (NSString *) asSafeSQLString; +- (NSUInteger) countOccurrencesOfString: (NSString *) substring; + @end #endif /* NSSTRING_URL_H */ diff --git a/SoObjects/SOGo/NSString+Utilities.m b/SoObjects/SOGo/NSString+Utilities.m index afc9d553a..ceac40666 100644 --- a/SoObjects/SOGo/NSString+Utilities.m +++ b/SoObjects/SOGo/NSString+Utilities.m @@ -597,4 +597,33 @@ static NSMutableCharacterSet *safeLDIFStartChars = nil; stringByReplacingString: @"'" withString: @"\\'"]; } +- (NSUInteger) countOccurrencesOfString: (NSString *) substring +{ + NSRange matchRange, substrRange; + BOOL done = NO; + NSUInteger selfLen, substrLen, count = 0; + + selfLen = [self length]; + substrLen = [substring length]; + + matchRange = NSMakeRange (0, selfLen); + while (!done) + { + substrRange = [self rangeOfString: substring options: 0 range: matchRange]; + if (substrRange.location == NSNotFound) + done = YES; + else + { + count++; + matchRange.location = substrRange.location + 1; + if (matchRange.location + substrLen > selfLen) + done = YES; + else + matchRange.length = selfLen - matchRange.location; + } + } + + return count; +} + @end diff --git a/Tests/Unit/GNUmakefile b/Tests/Unit/GNUmakefile index 5f1b3b80a..4e54ac68a 100644 --- a/Tests/Unit/GNUmakefile +++ b/Tests/Unit/GNUmakefile @@ -20,7 +20,8 @@ $(TEST_TOOL)_OBJC_FILES += \ TestSBJsonParser.m \ \ TestNGMimeAddressHeaderFieldGenerator.m \ - TestNSString+URLEscaping.m + TestNSString+URLEscaping.m \ + TestNSString+Utilities.m TEST_TOOL_NAME = $(TEST_TOOL) diff --git a/Tests/Unit/SOGoTest.h b/Tests/Unit/SOGoTest.h index bbc192ed3..04267181f 100644 --- a/Tests/Unit/SOGoTest.h +++ b/Tests/Unit/SOGoTest.h @@ -54,7 +54,7 @@ @end #define test(c) { \ - [self test: (c) message: @"assertion failure" \ + [self test: (c) message: @"assertion failure" \ file: __FILE__ line: __LINE__]; \ } @@ -63,13 +63,13 @@ file: __FILE__ line: __LINE__]; \ } -#define failIf(c) test(!c) +#define failIf(c) test(!(c)) #define testEquals(a,b) \ - testWithMessage(((a == b) || ([a isEqual: b])), \ + testWithMessage((((a) == (b)) || ([(a) isEqual: (b)])), \ ([NSString stringWithFormat: @"objects '%@' and '%@' differs", (a), (b)])) #define testEqualsWithMessage(a,b,m) \ - testWithMessage(([a isEqual: b]), (m)) + testWithMessage(([(a) isEqual: (b)]), (m)) #endif /* SOGOTEST_H */ diff --git a/Tests/Unit/TestNSString+Utilities.m b/Tests/Unit/TestNSString+Utilities.m new file mode 100644 index 000000000..7f9ef5b18 --- /dev/null +++ b/Tests/Unit/TestNSString+Utilities.m @@ -0,0 +1,50 @@ +/* TestNSString+Utilities.m - this file is part of SOGo + * + * Copyright (C) 2011 Inverse inc + * + * Author: Wolfgang Sourdeau + * + * This file is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2, or (at your option) + * any later version. + * + * This file is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; see the file COPYING. If not, write to + * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +/* This file is encoded in utf-8. */ + +#import + +#import "SOGoTest.h" + +@interface TestNSString_plus_Utilities : SOGoTest +@end + +@implementation TestNSString_plus_Utilities + +- (void) test_countOccurrencesOfString +{ + NSUInteger count; + + count = [@"abcdefa" countOccurrencesOfString: @"a"]; + failIf(count != 2); + count = [@"abcdefa" countOccurrencesOfString: @"b"]; + failIf(count != 1); + count = [@"" countOccurrencesOfString: @""]; + failIf(count != 0); + count = [@"" countOccurrencesOfString: @"b"]; + failIf(count != 0); + count = [@"roge" countOccurrencesOfString: @"roger"]; + failIf(count != 0); +} + +@end