diff --git a/Base32.xcodeproj/project.pbxproj b/Base32.xcodeproj/project.pbxproj index b3c22ea..5bb98a2 100644 --- a/Base32.xcodeproj/project.pbxproj +++ b/Base32.xcodeproj/project.pbxproj @@ -9,6 +9,8 @@ /* Begin PBXBuildFile section */ 6C10E90F1A753A6C006EED90 /* Base32.h in Headers */ = {isa = PBXBuildFile; fileRef = 6CA0A79D1A74E80600AC539F /* Base32.h */; settings = {ATTRIBUTES = (Public, ); }; }; 6C10E91A1A754346006EED90 /* TTTDataTransformer.m in Sources */ = {isa = PBXBuildFile; fileRef = 6C10E9141A7540B5006EED90 /* TTTDataTransformer.m */; }; + 6C122F7D1A85E52C004FD458 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C122F7C1A85E52C004FD458 /* StringExtension.swift */; }; + 6C122F7E1A85E52C004FD458 /* StringExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6C122F7C1A85E52C004FD458 /* StringExtension.swift */; }; 6CA0A79E1A74E80600AC539F /* Base32.h in Headers */ = {isa = PBXBuildFile; fileRef = 6CA0A79D1A74E80600AC539F /* Base32.h */; settings = {ATTRIBUTES = (Public, ); }; }; 6CA0A7A41A74E80600AC539F /* Base32.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6CA0A7981A74E80600AC539F /* Base32.framework */; }; 6CA0A7AB1A74E80600AC539F /* Base32Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6CA0A7AA1A74E80600AC539F /* Base32Tests.swift */; }; @@ -42,6 +44,7 @@ 6C10E9121A7540B4006EED90 /* SecEncodeTransformTests-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "SecEncodeTransformTests-Bridging-Header.h"; sourceTree = ""; }; 6C10E9131A7540B5006EED90 /* TTTDataTransformer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TTTDataTransformer.h; sourceTree = ""; }; 6C10E9141A7540B5006EED90 /* TTTDataTransformer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TTTDataTransformer.m; sourceTree = ""; }; + 6C122F7C1A85E52C004FD458 /* StringExtension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringExtension.swift; sourceTree = ""; }; 6CA0A7981A74E80600AC539F /* Base32.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Base32.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 6CA0A79C1A74E80600AC539F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 6CA0A79D1A74E80600AC539F /* Base32.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Base32.h; sourceTree = ""; }; @@ -152,6 +155,7 @@ children = ( 6CA0A79D1A74E80600AC539F /* Base32.h */, 6CA0A7EF1A74ECEE00AC539F /* Base32.swift */, + 6C122F7C1A85E52C004FD458 /* StringExtension.swift */, 6CA0A79B1A74E80600AC539F /* Supporting Files */, ); path = Base32; @@ -479,6 +483,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 6C122F7D1A85E52C004FD458 /* StringExtension.swift in Sources */, 6CA0A7F01A74ECEE00AC539F /* Base32.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -495,6 +500,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 6C122F7E1A85E52C004FD458 /* StringExtension.swift in Sources */, 6CA0A7F11A74ECEE00AC539F /* Base32.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/Base32/Base32.swift b/Base32/Base32.swift index cab2b73..1aa4447 100644 --- a/Base32/Base32.swift +++ b/Base32/Base32.swift @@ -106,8 +106,8 @@ extension NSData { return base32Encode(self) } - public var base32EncodedData: NSData? { - return base32EncodedString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) + public var base32EncodedData: NSData { + return base32EncodedString.dataUsingUTF8StringEncoding } public var base32DecodedData: NSData? { @@ -123,8 +123,8 @@ extension NSData { return base32HexEncode(self) } - public var base32HexEncodedData: NSData? { - return base32HexEncodedString.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) + public var base32HexEncodedData: NSData { + return base32HexEncodedString.dataUsingUTF8StringEncoding } public var base32HexDecodedData: NSData? { diff --git a/Base32/StringExtension.swift b/Base32/StringExtension.swift new file mode 100644 index 0000000..02e6632 --- /dev/null +++ b/Base32/StringExtension.swift @@ -0,0 +1,29 @@ +// +// StringExtension.swift +// Base32 +// +// Created by 野村 憲男 on 2/7/15. +// Copyright (c) 2015 Norio Nomura. All rights reserved. +// + +import Foundation + +// MARK: - private + +extension String { + /// NSData never nil + internal var dataUsingUTF8StringEncoding: NSData { + let length = nulTerminatedUTF8.count - 1 + return nulTerminatedUTF8.withUnsafeBufferPointer { + return NSData(bytes: $0.baseAddress, length: length) + } + } + + /// Array + internal var arrayUsingUTF8StringEncoding: [UInt8] { + let length = nulTerminatedUTF8.count - 1 + return nulTerminatedUTF8.withUnsafeBufferPointer { + return Array(UnsafeBufferPointer(start: $0.baseAddress, count: length)) + } + } +} diff --git a/Base32Tests/Base32Tests.swift b/Base32Tests/Base32Tests.swift index b0085d4..c47553a 100644 --- a/Base32Tests/Base32Tests.swift +++ b/Base32Tests/Base32Tests.swift @@ -114,10 +114,10 @@ class Base32Tests: XCTestCase { for (test, expect, expectHex) in dataVectors { let result = test.base32EncodedData let resultHex = test.base32HexEncodedData - XCTAssertEqual(result!, expect, "\(test).base32EncodedData") - XCTAssertEqual(resultHex!, expectHex, "\(test).base32HexEncodedData") - let decoded = result!.base32DecodedData - let decodedHex = resultHex!.base32HexDecodedData + XCTAssertEqual(result, expect, "\(test).base32EncodedData") + XCTAssertEqual(resultHex, expectHex, "\(test).base32HexEncodedData") + let decoded = result.base32DecodedData + let decodedHex = resultHex.base32HexDecodedData XCTAssertEqual(decoded!, test, "\(result).base32DecodedData") XCTAssertEqual(decodedHex!, test, "\(resultHex).base32HexDecodedData") }