From 009683ddd4011dc3a53711a25a2dcdff254ad36f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arkadiusz=20M=C5=82ynarczyk?= Date: Tue, 24 Aug 2010 20:00:27 +0200 Subject: [PATCH] - Exception raising in componentsSeparatedByCharactersInSet: when separator parameter is nil - Added support for edge cases (string is empty or string doesn't contain characters from separator set). --- Foundation/CPString.j | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/Foundation/CPString.j b/Foundation/CPString.j index 03ee0521b..7f7ea1df3 100644 --- a/Foundation/CPString.j +++ b/Foundation/CPString.j @@ -280,23 +280,36 @@ var CPStringRegexSpecialCharacters = [ @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 -{ - CPMutableArray components = [CPMutableArray array]; +/* + 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'"]; - if (separator && self.length) - { - CPRange componentRange = CPMakeRange(0, 0); + 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; + componentRange.location += componentRange.length + 1; } - }; - - } - return components; + } + if (componentRange.location < self.length ) { + componentRange.length = self.length - componentRange.location; + [components addObject:[self substringWithRange:componentRange]]; + } + } else { + [components addObject:[NSString stringWithString:self]]; + } + return components; } /*!