Monotone-Parent: 3049bbd9ddfe5886523c3f742888bf850719f4bc

Monotone-Revision: d9a86eb13b216d79e4554381c163dbca408f0d39

Monotone-Author: wsourdeau@inverse.ca
Monotone-Date: 2011-07-19T20:01:13
Monotone-Branch: ca.inverse.sogo
This commit is contained in:
Wolfgang Sourdeau
2011-07-19 20:01:13 +00:00
parent 5a1aad272e
commit 5f6f472b16
5 changed files with 87 additions and 5 deletions
+2
View File
@@ -74,6 +74,8 @@
- (NSString *) asSafeSQLString;
- (NSUInteger) countOccurrencesOfString: (NSString *) substring;
@end
#endif /* NSSTRING_URL_H */
+29
View File
@@ -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
+2 -1
View File
@@ -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)
+4 -4
View File
@@ -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 */
+50
View File
@@ -0,0 +1,50 @@
/* TestNSString+Utilities.m - this file is part of SOGo
*
* Copyright (C) 2011 Inverse inc
*
* Author: Wolfgang Sourdeau <wsourdeau@inverse.ca>
*
* 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 <SOGo/NSString+Utilities.h>
#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