diff --git a/Sources/Base64/Base64.swift b/Sources/Base64/Base64.swift index 0ba105c..39c5ee7 100644 --- a/Sources/Base64/Base64.swift +++ b/Sources/Base64/Base64.swift @@ -67,6 +67,14 @@ enum Base64 /// Encodes a sequence of bytes to a base-64 string with padding if needed. @inlinable public static func encode(_ bytes:Bytes) -> String where Bytes:Sequence + { + self.encode(bytes, padding: true) + } + + /// Encodes a sequence of bytes to a base-64 string, padding the output with `=` characters + /// if `padding` is true. + @inlinable public static + func encode(_ bytes:Bytes, padding:Bool) -> String where Bytes:Sequence { var encoded:String = "" encoded.reserveCapacity(bytes.underestimatedCount * 4 / 3) @@ -79,9 +87,14 @@ enum Base64 else { encoded.append(Digits[first << 4]) - encoded.append("=") - encoded.append("=") - continue + + if padding + { + encoded.append("=") + encoded.append("=") + } + + break } encoded.append( Digits[first << 4 | second >> 4]) @@ -90,8 +103,12 @@ enum Base64 else { encoded.append(Digits[second << 2]) - encoded.append("=") - continue + + if padding + { + encoded.append("=") + } + break } encoded.append( Digits[second << 2 | third >> 6])