|
13 | 13 | import SwiftShims
|
14 | 14 |
|
15 | 15 | internal enum _Normalization {
|
16 |
| - |
17 | 16 | // ICU's NFC unorm2 instance
|
18 | 17 | //
|
19 | 18 | // TODO(String performance): Should we cache one on TLS? Is this an expensive
|
@@ -102,3 +101,258 @@ internal func _tryNormalize(
|
102 | 101 | }
|
103 | 102 | return numericCast(count)
|
104 | 103 | }
|
| 104 | + |
| 105 | +internal enum NormalizationResult { |
| 106 | + case success(SuccessResult) |
| 107 | + case bufferTooSmall(BufferResizeRequest) // The size needed to normalize the rest of the string |
| 108 | + |
| 109 | + struct SuccessResult { |
| 110 | + var amountFilled: Int |
| 111 | + var nextReadPosition: String.Index |
| 112 | + } |
| 113 | + struct BufferResizeRequest { |
| 114 | + var newOutputBufferSize: Int |
| 115 | + var newPreNormalScratchBufferSize: Int |
| 116 | + var newPostNormalScratchBufferSize: Int |
| 117 | + } |
| 118 | + |
| 119 | + static func bufferTooSmall(count: Int) -> NormalizationResult { |
| 120 | + let outputBufferSize = count * 9 |
| 121 | + let preNormalBufferSize = count |
| 122 | + let postNormalBufferSize = count * 3 |
| 123 | + let resizeRequest = BufferResizeRequest( |
| 124 | + newOutputBufferSize: outputBufferSize, |
| 125 | + newPreNormalScratchBufferSize: preNormalBufferSize, |
| 126 | + newPostNormalScratchBufferSize: postNormalBufferSize |
| 127 | + ) |
| 128 | + return .bufferTooSmall(resizeRequest) |
| 129 | + } |
| 130 | + |
| 131 | + static func success( |
| 132 | + amountFilled filled: Int, nextReadPosition index: String.Index |
| 133 | + ) -> NormalizationResult { |
| 134 | + let successResult = SuccessResult(amountFilled: filled, nextReadPosition: index) |
| 135 | + return .success(successResult) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func unimplemented() -> Never { fatalError("Unimplemented function called") } |
| 140 | + |
| 141 | +internal func fastFill( |
| 142 | + _ sourceBuffer: UnsafeBufferPointer<UInt8>, |
| 143 | + _ outputBuffer: UnsafeMutableBufferPointer<UInt8> |
| 144 | +) -> (Int, Int) { |
| 145 | + // Quick check if a scalar is NFC and a segment starter |
| 146 | + @inline(__always) func isNFCStarter(_ scalar: Unicode.Scalar) -> Bool { |
| 147 | + // Fast-path: All scalars up through U+02FF are NFC and have boundaries |
| 148 | + // before them |
| 149 | + if scalar.value < 0x300 { return true } |
| 150 | + |
| 151 | + // Otherwise, consult the properties |
| 152 | + return scalar._hasNormalizationBoundaryBefore && scalar._isNFCQCYes |
| 153 | + } |
| 154 | + |
| 155 | + var outputBufferThreshold: Int { |
| 156 | + return outputBuffer.count - 4 |
| 157 | + } |
| 158 | + |
| 159 | + // TODO: Additional fast-path: All CCC-ascending NFC_QC segments are NFC |
| 160 | + // TODO: Just freakin do normalization and don't bother with ICU |
| 161 | + var outputCount = 0 |
| 162 | + let outputEnd = outputBufferThreshold |
| 163 | + var inputCount = 0 |
| 164 | + let inputEnd = sourceBuffer.count |
| 165 | + while inputCount < inputEnd && outputCount < outputEnd { |
| 166 | + // TODO: Slightly faster code-unit scan for latiny (<0xCC) |
| 167 | + |
| 168 | + // Check scalar-based fast-paths |
| 169 | + let (scalar, len) = _decodeScalar(sourceBuffer, startingAt: inputCount) |
| 170 | + _internalInvariant(inputCount &+ len <= inputEnd) |
| 171 | + |
| 172 | + if _slowPath( |
| 173 | + !sourceBuffer.hasNormalizationBoundary(before: inputCount &+ len) |
| 174 | + || !isNFCStarter(scalar) |
| 175 | + ) { |
| 176 | + break |
| 177 | + } |
| 178 | + inputCount &+= len |
| 179 | + |
| 180 | + for cu in UTF8.encode(scalar)._unsafelyUnwrappedUnchecked { |
| 181 | + outputBuffer[outputCount] = cu |
| 182 | + outputCount &+= 1 |
| 183 | + } |
| 184 | + |
| 185 | + _internalInvariant(inputCount == outputCount, |
| 186 | + "non-normalizing UTF-8 fast path should be 1-to-1 in code units") |
| 187 | + } |
| 188 | + return (inputCount, outputCount) |
| 189 | +} |
| 190 | + |
| 191 | +internal func transcodeToUTF16( |
| 192 | + _ sourceBuffer: UnsafeBufferPointer<UInt8>, |
| 193 | + into outputBuffer: UnsafeMutableBufferPointer<UInt16> |
| 194 | +) -> (Int, Int)? { |
| 195 | + var readIndex = 0 |
| 196 | + var writeIndex = 0 |
| 197 | + let outputCount = outputBuffer.count |
| 198 | + let sourceCount = sourceBuffer.count |
| 199 | + |
| 200 | + while readIndex < sourceCount { |
| 201 | + let (scalar, length) = _decodeScalar(sourceBuffer, startingAt: readIndex) |
| 202 | + |
| 203 | + if scalar._hasNormalizationBoundaryBefore && readIndex != 0 { |
| 204 | + break |
| 205 | + } |
| 206 | + |
| 207 | + readIndex += length |
| 208 | + |
| 209 | + for cu in scalar.utf16 { |
| 210 | + if writeIndex < outputCount { |
| 211 | + outputBuffer[writeIndex] = cu |
| 212 | + writeIndex += 1 |
| 213 | + } else { |
| 214 | + return nil |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return (readIndex, writeIndex) |
| 220 | +} |
| 221 | + |
| 222 | +internal func transcodeToUTF8( |
| 223 | + _ sourceBuffer: UnsafeBufferPointer<UInt16>, |
| 224 | + into outputBuffer: UnsafeMutableBufferPointer<UInt8> |
| 225 | +) -> (Int, Int)? { |
| 226 | + var readIndex = 0 |
| 227 | + var writeIndex = 0 |
| 228 | + let outputCount = outputBuffer.count |
| 229 | + let sourceCount = sourceBuffer.count |
| 230 | + |
| 231 | + while readIndex < sourceCount { |
| 232 | + let (scalar, length) = _decodeScalar(sourceBuffer, startingAt: readIndex) |
| 233 | + //we don't need to check for normalization boundaries here because we are only transcoding |
| 234 | + //a single segment at this point |
| 235 | + |
| 236 | + readIndex += length |
| 237 | + guard scalar.withUTF8CodeUnits({ utf8 in |
| 238 | + for cu in utf8 { |
| 239 | + if writeIndex < outputCount { |
| 240 | + outputBuffer[writeIndex] = cu |
| 241 | + writeIndex += 1 |
| 242 | + } else { |
| 243 | + return false |
| 244 | + } |
| 245 | + } |
| 246 | + return true |
| 247 | + }) else { |
| 248 | + return nil |
| 249 | + } |
| 250 | + } |
| 251 | + return (readIndex, writeIndex) |
| 252 | +} |
| 253 | + |
| 254 | +internal func fastNormalize( |
| 255 | + readIndex: String.Index, |
| 256 | + guts: _StringGuts, |
| 257 | + outputBuffer: UnsafeMutableBufferPointer<UInt8>, |
| 258 | + icuInputBuffer: UnsafeMutableBufferPointer<UInt16>, |
| 259 | + icuOutputBuffer: UnsafeMutableBufferPointer<UInt16> |
| 260 | +) -> NormalizationResult { |
| 261 | + _internalInvariant(guts.isFastUTF8) |
| 262 | + return guts.withFastUTF8 { sourceBuffer in |
| 263 | + let sourceCount = sourceBuffer.count - readIndex.encodedOffset |
| 264 | + let start = readIndex.encodedOffset |
| 265 | + let rebasedSourceBuffer = UnsafeBufferPointer(rebasing: sourceBuffer[start...]) |
| 266 | + do { |
| 267 | + let (read, filled) = fastFill(rebasedSourceBuffer, outputBuffer) |
| 268 | + if filled > 0 { |
| 269 | + let nextIndex = readIndex.encoded(offsetBy: read) |
| 270 | + _internalInvariant(guts.isOnUnicodeScalarBoundary(nextIndex)) |
| 271 | + |
| 272 | + return .success(amountFilled: filled, nextReadPosition: nextIndex) |
| 273 | + } |
| 274 | + } |
| 275 | + guard let (read, filled) = transcodeToUTF16(rebasedSourceBuffer, into: icuInputBuffer) else { |
| 276 | + return .bufferTooSmall(count: sourceBuffer.count) |
| 277 | + } |
| 278 | + |
| 279 | + let nextIndex = readIndex.encoded(offsetBy: read) |
| 280 | + _internalInvariant(guts.isOnUnicodeScalarBoundary(nextIndex)) |
| 281 | + |
| 282 | + let rebasedICUInputBuffer = UnsafeBufferPointer(rebasing: icuInputBuffer[..<filled]) |
| 283 | + return sharedNormalize( |
| 284 | + sourceCount, nextIndex, outputBuffer, rebasedICUInputBuffer, icuOutputBuffer |
| 285 | + ) |
| 286 | + } |
| 287 | +} |
| 288 | + |
| 289 | +internal func foreignNormalize( |
| 290 | + readIndex: String.Index, |
| 291 | + guts: _StringGuts, |
| 292 | + outputBuffer: UnsafeMutableBufferPointer<UInt8>, |
| 293 | + icuInputBuffer: UnsafeMutableBufferPointer<UInt16>, |
| 294 | + icuOutputBuffer: UnsafeMutableBufferPointer<UInt16> |
| 295 | +) -> NormalizationResult { |
| 296 | + let sourceCount = guts.count - readIndex.encodedOffset |
| 297 | + guard let (read, filled) = foreignFill(readIndex, guts, into: icuInputBuffer) else { |
| 298 | + return .bufferTooSmall(count: guts.count) |
| 299 | + } |
| 300 | + |
| 301 | + let nextIndex = readIndex.encoded(offsetBy: read) |
| 302 | + _internalInvariant(guts.isOnUnicodeScalarBoundary(nextIndex)) |
| 303 | + |
| 304 | + let rebasedICUInputBuffer = UnsafeBufferPointer(rebasing: icuInputBuffer[..<filled]) |
| 305 | + return sharedNormalize( |
| 306 | + sourceCount, nextIndex, outputBuffer, rebasedICUInputBuffer, icuOutputBuffer |
| 307 | + ) |
| 308 | +} |
| 309 | + |
| 310 | +func foreignFill( |
| 311 | + _ readIndex: String.Index, |
| 312 | + _ guts: _StringGuts, |
| 313 | + into outputBuffer: UnsafeMutableBufferPointer<UInt16> |
| 314 | +) -> (Int, Int)? { |
| 315 | + var index = readIndex |
| 316 | + var writeIndex = 0 |
| 317 | + let outputCount = outputBuffer.count |
| 318 | + let cachedEndIndex = guts.endIndex |
| 319 | + while index != cachedEndIndex { |
| 320 | + let (scalar, length) = guts.foreignErrorCorrectedScalar(startingAt: index) |
| 321 | + if scalar._hasNormalizationBoundaryBefore && index != readIndex { |
| 322 | + break |
| 323 | + } |
| 324 | + |
| 325 | + index = index.encoded(offsetBy: length) |
| 326 | + |
| 327 | + for cu in scalar.utf16 { |
| 328 | + if writeIndex < outputCount { |
| 329 | + outputBuffer[writeIndex] = cu |
| 330 | + writeIndex += 1 |
| 331 | + } else { |
| 332 | + return nil |
| 333 | + } |
| 334 | + } |
| 335 | + } |
| 336 | + return (index.encodedOffset - readIndex.encodedOffset, writeIndex) |
| 337 | +} |
| 338 | + |
| 339 | +private func sharedNormalize( |
| 340 | + _ sourceCount: Int, |
| 341 | + _ nextIndex: String.Index, |
| 342 | + _ outputBuffer: UnsafeMutableBufferPointer<UInt8>, |
| 343 | + _ icuInputBuffer: UnsafeBufferPointer<UInt16>, |
| 344 | + _ icuOutputBuffer: UnsafeMutableBufferPointer<UInt16> |
| 345 | +) -> NormalizationResult { |
| 346 | + guard let normalized = _tryNormalize(icuInputBuffer, into: icuOutputBuffer) else { |
| 347 | + return .bufferTooSmall(count: sourceCount) |
| 348 | + } |
| 349 | + |
| 350 | + guard let (_, transcoded) = transcodeToUTF8( |
| 351 | + UnsafeBufferPointer<UInt16>(rebasing: icuOutputBuffer[..<normalized]), |
| 352 | + into: outputBuffer |
| 353 | + ) else { |
| 354 | + return .bufferTooSmall(count: sourceCount) |
| 355 | + } |
| 356 | + |
| 357 | + return .success(amountFilled: transcoded, nextReadPosition: nextIndex) |
| 358 | +} |
0 commit comments