From ddfac8d7171f736ea389f01d710b6072a1864c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arkadiusz=20M=C5=82ynarczyk?= Date: Wed, 25 Aug 2010 10:42:15 +0200 Subject: [PATCH] - moved componentsSeparatedByCharactersInSet: into CPCharacterSetAdditions CPString category - added tests for componentsSeparatedByCharactersInSet: - ensured Cocoa compatibility of componentsSeparatedByCharactersInSet: - added new test for CPString boolValue - fixed CPString boolValue (" ++01" should return NO as in Cocoa, previously it returned YES) --- Foundation/CPCharacterSet.j | 37 ++++++++++++++++++++++++++ Foundation/CPString.j | 47 +-------------------------------- Tests/Foundation/CPStringTest.j | 41 +++++++++++++++++++++++++++- 3 files changed, 78 insertions(+), 47 deletions(-) diff --git a/Foundation/CPCharacterSet.j b/Foundation/CPCharacterSet.j index a087a39c8..8a4f1338b 100644 --- a/Foundation/CPCharacterSet.j +++ b/Foundation/CPCharacterSet.j @@ -322,6 +322,43 @@ _CPCharacterSetTrimAtEnd = 1 << 2; @implementation CPString (CPCharacterSetAdditions) +/*! + Tokenizes the receiver string using the charactes + in a given set. For example, if the receiver is: + \c "Baku baku to jest skład." + and the set is [CPCharacterSet whitespaceCharacterSet] + the returned array would contain: +
 ["Baku", "baku", "to", "jest", "", "skład."] 
+ Adjacent occurences of the separator characters produce empty strings in the result. + @author Arkadiusz Młynarczyk + @param A character set containing the characters to use to split the receiver. Must not be nil. + @return An CPArray object containing substrings from the receiver that have been divided by characters in separator. +*/ +- (CPArray)componentsSeparatedByCharactersInSet:(CPCharacterSet)separator +{ + if (!separator) + [CPException raise:CPInvalidArgumentException + reason:"componentsSeparatedByCharactersInSet: the separator can't be 'nil'"]; + + var components = [CPMutableArray array]; + var componentRange = CPMakeRange(0, 0); + + for (var i=0; i < self.length; i++) + { + if ([separator characterIsMember:self.charAt(i)]) + { + componentRange.length = i - componentRange.location; + [components addObject:[self substringWithRange:componentRange]]; + componentRange.location += componentRange.length + 1; + } + } + + componentRange.length = self.length - componentRange.location; + [components addObject:[self substringWithRange:componentRange]]; + + return components; +} + // As per the Cocoa method. - (id)stringByTrimmingCharactersInSet:(CPCharacterSet)set { diff --git a/Foundation/CPString.j b/Foundation/CPString.j index 7f7ea1df3..3b6858ab2 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -268,50 +268,6 @@ var CPStringRegexSpecialCharacters = [ return split(aString); } -/*! - Tokenizes the receiver string using the charactes - in a given set. For example, if the receiver is: - \c "Baku baku to jest skład." - and the set is [CPCharacterSet whitespaceCharacterSet] - the returned array would contain: -
 ["Baku", "baku", "to", "jest", "", "skład."] 
- Adjacent occurences of the separator characters produce empty strings in the result. - @author Arkadiusz Młynarczyk - @param A character set containing the characters to use to split the receiver. Must not be nil. - @return An CPArray object containing substrings from the receiver that have been divided by characters in separator. -*/ -/* - TODO Write some tests for cases with [CPCharacterSet whitespaceCharacterSet] - "Baku baku to jest skład." ["Baku", "baku", "to", "jest", "", "skład."] - "Abradab" - ["Abradab"] - "" - [""] - " Test " - check what is returned by NSString here, probably ["", "Test"]; -*/ -- (CPArray)componentsSeparatedByCharactersInSet:(CPCharacterSet)separator { - if (!separator) - [CPException raise:CPInvalidArgumentException - reason:"componentsSeparatedByCharactersInSet: the separator can't be 'nil'"]; - - CPMutableArray components = [CPMutableArray array]; - CPRange componentRange = CPMakeRange(0, 0); - if (self.length) { - for (var i=0; i < self.length; i++) { - if ([separator characterIsMember:self.charAt(i)]) { - componentRange.length = i - componentRange.location; - [components addObject:[self substringWithRange:componentRange]]; - componentRange.location += componentRange.length + 1; - } - } - if (componentRange.location < self.length ) { - componentRange.length = self.length - componentRange.location; - [components addObject:[self substringWithRange:componentRange]]; - } - } else { - [components addObject:[NSString stringWithString:self]]; - } - return components; -} - /*! Returns a substring starting from the specified index to the end of the receiver. @param anIndex the starting string (inclusive) @@ -689,10 +645,9 @@ var CPStringRegexSpecialCharacters = [ a digit 1-9. Returns \c NO otherwise. This method skips the initial whitespace characters, +,- followed by Zeroes. */ - - (BOOL)boolValue { - var replaceRegExp = new RegExp("^\\s*[\\+,\\-]*0*"); + var replaceRegExp = new RegExp("^\\s*[\\+,\\-]?0*"); return RegExp("^[Y,y,t,T,1-9]").test(self.replace(replaceRegExp, '')); } diff --git a/Tests/Foundation/CPStringTest.j b/Tests/Foundation/CPStringTest.j index 90a7fb77f..34b6eeca6 100644 --- a/Tests/Foundation/CPStringTest.j +++ b/Tests/Foundation/CPStringTest.j @@ -1,4 +1,5 @@ @import +@import @implementation CPStringTest : OJTestCase @@ -144,7 +145,8 @@ [" NO", NO], [" -N00", NO], [" 00", NO], - [" -00", NO] + [" -00", NO], + [" -+001", NO], ]; for (var i = 0; i < testStrings.length; i++) @@ -312,4 +314,41 @@ [self assertFalse:["abc" hasSuffix:""]]; } +- (void)testComponentsSeparatedByCharactersInSetEmptyString +{ + [self assert:[""] + equals:["" componentsSeparatedByCharactersInSet:[CPCharacterSet whitespaceCharacterSet]]]; +} + +- (void)testComponentsSeparatedByCharactersInSetStringWithoutCharactersFromSet +{ + [self assert:["Abradab"] + equals:["Abradab" componentsSeparatedByCharactersInSet:[CPCharacterSet whitespaceCharacterSet]]]; +} + +- (void)testComponentsSeparatedByCharactersInSet +{ + [self assert:["Baku", "baku", "to", "jest", "", "skład."] + equals:["Baku baku to jest skład." componentsSeparatedByCharactersInSet:[CPCharacterSet whitespaceCharacterSet]]]; +} + +- (void)testComponentsSeparatedByCharactersInSetLeadingAndTrailingCharacterFromSet +{ + [self assert:["", "Test", ""] + equals:[" Test " componentsSeparatedByCharactersInSet:[CPCharacterSet whitespaceCharacterSet]]]; +} + +- (void)testComponentsSeparatedByCharactersExceptionRaiseOnNilSeparator +{ + try + { + [[CPString string] componentsSeparatedByCharactersInSet:nil]; + [self assert:false]; + } + catch (anException) + { + [self assert:[anException name] equals:CPInvalidArgumentException]; + [self assert:[anException reason] equals:@"componentsSeparatedByCharactersInSet: the separator can't be 'nil'"]; + } +} @end