here’s a weird bug:
var description:String
{
.init(unsafeUninitializedCapacity: self.rawValue.utf8.count)
{
for (i, codeunit):(Int, UInt8) in zip($0.indices, self.rawValue.utf8)
{
switch codeunit
{
case 0x09, 0x20: $0[i] = 0x2E // '.'
case let codeunit: $0[i] = codeunit
}
}
return $0.count
}
}
this produces an empty string if the rawValue
has less than 16 UTF-8 bytes, because the buffer count ($0.count
) is always at least 16.
the fix, as it turns out, is:
@inlinable public
var description:String
{
.init(unsafeUninitializedCapacity: self.rawValue.utf8.count)
{
var i:Int = $0.startIndex
for codeunit:UInt8 in self.rawValue.utf8
{
switch codeunit
{
case 0x09, 0x20: $0[i] = 0x2E // '.'
case let codeunit: $0[i] = codeunit
}
i = $0.index(after: i)
}
return i
}
}
why on earth does String.init(unsafeUninitializedCapacity:initializingUTF8With:)
even pass a buffer with a count that doesn’t match the specified capacity?