Created
June 27, 2015 19:26
-
-
Save soffes/b6bd2a8d3e9b8317efd2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
extension String { | |
var isHomogeneous: Bool { | |
if lengthOfBytesUsingEncoding(NSUTF8StringEncoding) == 0 { | |
return true | |
} | |
var homogeneous = true | |
var character: NSString? | |
let range = Range(start: startIndex, end: endIndex) | |
enumerateSubstringsInRange(range, options: [.ByComposedCharacterSequences]) { substring, _, _, stop in | |
if let character = character { | |
if character != substring { | |
homogeneous = false | |
stop = true | |
} | |
} else { | |
character = substring | |
} | |
} | |
return homogeneous | |
} | |
} | |
import XCTest | |
class StringExtensionTests: XCTestCase { | |
func testHomogeneous() { | |
XCTAssertTrue("~~~".isHomogeneous) | |
XCTAssertTrue("aaa".isHomogeneous) | |
XCTAssertTrue("ππ".isHomogeneous) | |
XCTAssertTrue("π".isHomogeneous) | |
XCTAssertTrue("".isHomogeneous) | |
XCTAssertFalse("as".isHomogeneous) | |
XCTAssertFalse(" ~~~".isHomogeneous) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@marklarr Yes, it works!