@@ -1035,47 +1035,43 @@ fileprivate extension StringProtocol {
1035
1035
}
1036
1036
1037
1037
func removingURLPercentEncoding( utf8Buffer: some Collection < UInt8 > , excluding: Set < UInt8 > ) -> String ? {
1038
- let result : String ? = withUnsafeTemporaryAllocation ( of: CChar . self, capacity: utf8 . count + 1 ) { _buffer in
1039
- var buffer = OutputBuffer ( initializing : _buffer . baseAddress! , capacity : _buffer . count )
1038
+ let result : String ? = withUnsafeTemporaryAllocation ( of: UInt8 . self, capacity: utf8Buffer . count) { buffer in
1039
+ var i = 0
1040
1040
var byte : UInt8 = 0
1041
1041
var hexDigitsRequired = 0
1042
1042
for v in utf8Buffer {
1043
1043
if v == UInt8 ( ascii: " % " ) {
1044
1044
guard hexDigitsRequired == 0 else {
1045
- _ = buffer. relinquishBorrowedMemory ( )
1046
1045
return nil
1047
1046
}
1048
1047
hexDigitsRequired = 2
1049
1048
} else if hexDigitsRequired > 0 {
1050
1049
guard let hex = asciiToHex ( v) else {
1051
- _ = buffer. relinquishBorrowedMemory ( )
1052
1050
return nil
1053
1051
}
1054
1052
if hexDigitsRequired == 2 {
1055
1053
byte = hex << 4
1056
1054
} else if hexDigitsRequired == 1 {
1057
1055
byte += hex
1058
1056
if excluding. contains ( byte) {
1059
- buffer. appendElement ( CChar ( bitPattern: UInt8 ( ascii: " % " ) ) )
1060
- buffer. appendElement ( CChar ( bitPattern: hexToAscii ( byte >> 4 ) ) )
1061
- buffer. appendElement ( CChar ( bitPattern: v) )
1057
+ // Keep the original percent-encoding for this byte
1058
+ i = buffer [ i... i+ 2 ] . initialize ( fromContentsOf: [ UInt8 ( ascii: " % " ) , hexToAscii ( byte >> 4 ) , v] )
1062
1059
} else {
1063
- buffer. appendElement ( CChar ( bitPattern: byte) )
1060
+ buffer [ i] = byte
1061
+ i += 1
1064
1062
byte = 0
1065
1063
}
1066
1064
}
1067
1065
hexDigitsRequired -= 1
1068
1066
} else {
1069
- buffer. appendElement ( CChar ( bitPattern: v) )
1067
+ buffer [ i] = v
1068
+ i += 1
1070
1069
}
1071
1070
}
1072
1071
guard hexDigitsRequired == 0 else {
1073
- _ = buffer. relinquishBorrowedMemory ( )
1074
1072
return nil
1075
1073
}
1076
- buffer. appendElement ( 0 ) // NULL-terminated
1077
- let initialized = buffer. relinquishBorrowedMemory ( )
1078
- return String ( validatingUTF8: initialized. baseAddress!)
1074
+ return String ( _validating: buffer [ ..< i] , as: UTF8 . self)
1079
1075
}
1080
1076
return result
1081
1077
}
0 commit comments