Created append ascii digit extension of Int
This commit is contained in:
@@ -82,6 +82,15 @@ extension Int {
|
||||
|
||||
}
|
||||
|
||||
extension Int {
|
||||
|
||||
func appendAsciiDigit(asciiDigit: Byte) throws -> Int {
|
||||
let digit = Int(try asciiDigit.fromAsciiValue())
|
||||
return self*10 + digit
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension Character {
|
||||
|
||||
func asciiValue() throws -> NSData {
|
||||
|
||||
@@ -73,6 +73,19 @@ class BitsAndByteTests: XCTestCase {
|
||||
XCTAssertEqual(string, "\(integer)")
|
||||
}
|
||||
|
||||
func testAppendAsciiDigit() {
|
||||
doTestAppendAsciiDigit(123, digit: 4, expectedResult: 1234)
|
||||
doTestAppendAsciiDigit(1, digit: 0, expectedResult: 10)
|
||||
doTestAppendAsciiDigit(567, digit: 0, expectedResult: 5670)
|
||||
doTestAppendAsciiDigit(0, digit: 4, expectedResult: 4)
|
||||
doTestAppendAsciiDigit(0, digit: 0, expectedResult: 0)
|
||||
}
|
||||
|
||||
func doTestAppendAsciiDigit(integer: Int, digit: UInt8, expectedResult: Int) {
|
||||
let result = try! integer.appendAsciiDigit(try! digit.asciiValue())
|
||||
XCTAssertEqual(result, expectedResult, "\(integer)\(digit) != \(result)")
|
||||
}
|
||||
|
||||
func testAsciiEncodeString() {
|
||||
let string = "abc"
|
||||
let data = try! string.asciiValue()
|
||||
|
||||
Reference in New Issue
Block a user