Skip to content

Commit de1fdb7

Browse files
authored
Create string with bytes instead of unsafe pointer. (#9)
As proposed by @weissi in #5
1 parent abcb9b0 commit de1fdb7

File tree

1 file changed

+3
-8
lines changed

1 file changed

+3
-8
lines changed

Sources/Base64Kit/Base64.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ extension Base64 {
2323
-> String where Buffer.Element == UInt8
2424
{
2525
// In Base64, 3 bytes become 4 output characters, and we pad to the
26-
// nearest multiple of four. We need an additional byte to create a
27-
// null-terminated UTF-8 String in the end.
28-
let newCapacity = ((bytes.count + 2) / 3) * 4 + 1
26+
// nearest multiple of four.
27+
let newCapacity = ((bytes.count + 2) / 3) * 4
2928
let alphabet = options.contains(.base64UrlAlphabet)
3029
? Base64.encodeBase64Url
3130
: Base64.encodeBase64
@@ -49,12 +48,8 @@ extension Base64 {
4948
outputBytes.append(thirdChar)
5049
outputBytes.append(forthChar)
5150
}
52-
53-
outputBytes.append(0)
5451

55-
return outputBytes.withUnsafeBufferPointer { (ptr) -> String in
56-
return String(cString: ptr.baseAddress!)
57-
}
52+
return String(decoding: outputBytes, as: Unicode.UTF8.self)
5853
}
5954

6055
// MARK: Internal

0 commit comments

Comments
 (0)