@@ -27,27 +27,46 @@ extension Base64 {
27
27
? Base64 . encodeBase64Url
28
28
: Base64 . encodeBase64
29
29
30
- var outputBytes = [ UInt8] ( )
31
- outputBytes. reserveCapacity ( newCapacity)
32
-
33
30
var input = bytes. makeIterator ( )
34
31
35
- while let firstByte = input. next ( ) {
36
- let secondByte = input. next ( )
37
- let thirdByte = input. next ( )
32
+ #if swift(>=5.3) && os(Linux)
33
+ return String ( unsafeUninitializedCapacity: newCapacity) { buffer in
34
+ var i = 0
35
+ while let firstByte = input. next ( ) {
36
+ let secondByte = input. next ( )
37
+ let thirdByte = input. next ( )
38
38
39
- let firstChar = Base64 . encode ( alphabet: alphabet, firstByte: firstByte)
40
- let secondChar = Base64 . encode ( alphabet: alphabet, firstByte: firstByte, secondByte: secondByte)
41
- let thirdChar = Base64 . encode ( alphabet: alphabet, secondByte: secondByte, thirdByte: thirdByte)
42
- let forthChar = Base64 . encode ( alphabet: alphabet, thirdByte: thirdByte)
39
+ buffer [ i ] = Base64 . encode ( alphabet: alphabet, firstByte: firstByte)
40
+ buffer [ i + 1 ] = Base64 . encode ( alphabet: alphabet, firstByte: firstByte, secondByte: secondByte)
41
+ buffer [ i + 2 ] = Base64 . encode ( alphabet: alphabet, secondByte: secondByte, thirdByte: thirdByte)
42
+ buffer [ i + 3 ] = Base64 . encode ( alphabet: alphabet, thirdByte: thirdByte)
43
43
44
- outputBytes. append ( firstChar)
45
- outputBytes. append ( secondChar)
46
- outputBytes. append ( thirdChar)
47
- outputBytes. append ( forthChar)
48
- }
44
+ i += 4
45
+ }
46
+
47
+ return newCapacity
48
+ }
49
+ #else
50
+ var outputBytes = [ UInt8] ( )
51
+ outputBytes. reserveCapacity ( newCapacity)
52
+
53
+ while let firstByte = input. next ( ) {
54
+ let secondByte = input. next ( )
55
+ let thirdByte = input. next ( )
56
+
57
+ let firstChar = Base64 . encode ( alphabet: alphabet, firstByte: firstByte)
58
+ let secondChar = Base64 . encode ( alphabet: alphabet, firstByte: firstByte, secondByte: secondByte)
59
+ let thirdChar = Base64 . encode ( alphabet: alphabet, secondByte: secondByte, thirdByte: thirdByte)
60
+ let forthChar = Base64 . encode ( alphabet: alphabet, thirdByte: thirdByte)
61
+
62
+ outputBytes. append ( firstChar)
63
+ outputBytes. append ( secondChar)
64
+ outputBytes. append ( thirdChar)
65
+ outputBytes. append ( forthChar)
66
+ }
49
67
50
- return String ( decoding: outputBytes, as: Unicode . UTF8. self)
68
+ return String ( decoding: outputBytes, as: Unicode . UTF8. self)
69
+ #endif
51
70
}
52
71
53
72
// MARK: Internal
0 commit comments