[stdlib] Fix indentation in Unicode.swift

This commit is contained in:
Patrick Pijnappel
2016-03-17 06:41:59 +11:00
parent 7e0945e7ac
commit 771a81594f

View File

@@ -125,62 +125,62 @@ public struct UTF8 : UnicodeCodec {
I : IteratorProtocol where I.Element == CodeUnit I : IteratorProtocol where I.Element == CodeUnit
>(next: inout I) -> UnicodeDecodingResult { >(next: inout I) -> UnicodeDecodingResult {
refillBuffer: if !_didExhaustIterator { refillBuffer: if !_didExhaustIterator {
// Bufferless ASCII fastpath. // Bufferless ASCII fastpath.
if _fastPath(_bitsInBuffer == 0) { if _fastPath(_bitsInBuffer == 0) {
if let codeUnit = next.next() { if let codeUnit = next.next() {
if codeUnit & 0x80 == 0 { if codeUnit & 0x80 == 0 {
return .scalarValue(UnicodeScalar(_unchecked: UInt32(codeUnit))) return .scalarValue(UnicodeScalar(_unchecked: UInt32(codeUnit)))
}
// Non-ASCII, proceed to buffering mode.
_decodeBuffer = UInt32(codeUnit)
_bitsInBuffer = 8
} else {
_didExhaustIterator = true
return .emptyInput
} }
} else if(_decodeBuffer & 0x80 == 0) { // Non-ASCII, proceed to buffering mode.
// ASCII in buffer. We don't refill the buffer so we can return _decodeBuffer = UInt32(codeUnit)
// to bufferless mode once we've exhausted it. _bitsInBuffer = 8
break refillBuffer } else {
_didExhaustIterator = true
return .emptyInput
} }
// Buffering mode. } else if(_decodeBuffer & 0x80 == 0) {
// Fill buffer back to 4 bytes (or as many as are left in the iterator). // ASCII in buffer. We don't refill the buffer so we can return
_sanityCheck(_bitsInBuffer < 32) // to bufferless mode once we've exhausted it.
repeat { break refillBuffer
if let codeUnit = next.next() {
// We use & 0x1f to make the compiler omit a bounds check branch.
_decodeBuffer |= (UInt32(codeUnit) << UInt32(_bitsInBuffer & 0x1f))
_bitsInBuffer = _bitsInBuffer &+ 8
} else {
_didExhaustIterator = true
if _bitsInBuffer == 0 { return .emptyInput }
break // We still have some bytes left in our buffer.
}
} while _bitsInBuffer < 32
} else if _bitsInBuffer == 0 {
return .emptyInput
} }
// Buffering mode.
// Fill buffer back to 4 bytes (or as many as are left in the iterator).
_sanityCheck(_bitsInBuffer < 32)
repeat {
if let codeUnit = next.next() {
// We use & 0x1f to make the compiler omit a bounds check branch.
_decodeBuffer |= (UInt32(codeUnit) << UInt32(_bitsInBuffer & 0x1f))
_bitsInBuffer = _bitsInBuffer &+ 8
} else {
_didExhaustIterator = true
if _bitsInBuffer == 0 { return .emptyInput }
break // We still have some bytes left in our buffer.
}
} while _bitsInBuffer < 32
} else if _bitsInBuffer == 0 {
return .emptyInput
}
// Decode one unicode scalar. // Decode one unicode scalar.
// Note our empty bytes are always 0x00, which is required for this call. // Note our empty bytes are always 0x00, which is required for this call.
let (result, length) = UTF8._decodeOne(_decodeBuffer) let (result, length) = UTF8._decodeOne(_decodeBuffer)
// Consume the decoded bytes (or maximal subpart of ill-formed sequence). // Consume the decoded bytes (or maximal subpart of ill-formed sequence).
let bitsConsumed = 8 &* length let bitsConsumed = 8 &* length
_sanityCheck(1...4 ~= length && bitsConsumed <= _bitsInBuffer) _sanityCheck(1...4 ~= length && bitsConsumed <= _bitsInBuffer)
// Swift doesn't allow shifts greater than or equal to the type width. // Swift doesn't allow shifts greater than or equal to the type width.
// _decodeBuffer >>= UInt32(bitsConsumed) // >>= 32 crashes. // _decodeBuffer >>= UInt32(bitsConsumed) // >>= 32 crashes.
// Mask with 0x3f to let the compiler omit the '>= 64' bounds check. // Mask with 0x3f to let the compiler omit the '>= 64' bounds check.
_decodeBuffer = UInt32(truncatingBitPattern: _decodeBuffer = UInt32(truncatingBitPattern:
UInt64(_decodeBuffer) >> (UInt64(bitsConsumed) & 0x3f)) UInt64(_decodeBuffer) >> (UInt64(bitsConsumed) & 0x3f))
_bitsInBuffer = _bitsInBuffer &- bitsConsumed _bitsInBuffer = _bitsInBuffer &- bitsConsumed
if _fastPath(result != nil) { if _fastPath(result != nil) {
return .scalarValue(UnicodeScalar(_unchecked: result!)) return .scalarValue(UnicodeScalar(_unchecked: result!))
} else { } else {
return .error // Ill-formed UTF-8 code unit sequence. return .error // Ill-formed UTF-8 code unit sequence.
} }
} }
/// Attempts to decode a single UTF-8 code unit sequence starting at the LSB /// Attempts to decode a single UTF-8 code unit sequence starting at the LSB
@@ -698,6 +698,7 @@ extension UTF8.CodeUnit : _StringElement {
return UTF8.CodeUnit(utf16) return UTF8.CodeUnit(utf16)
} }
} }
extension UTF16 { extension UTF16 {
/// Returns the number of code units required to encode `x`. /// Returns the number of code units required to encode `x`.
@warn_unused_result @warn_unused_result
@@ -724,7 +725,7 @@ extension UTF16 {
_precondition(width(x) == 2) _precondition(width(x) == 2)
return UTF16.CodeUnit( return UTF16.CodeUnit(
(x.value - 0x1_0000) & (((1 as UInt32) << 10) - 1) (x.value - 0x1_0000) & (((1 as UInt32) << 10) - 1)
) + 0xDC00 ) + 0xDC00
} }
@warn_unused_result @warn_unused_result
@@ -742,19 +743,19 @@ extension UTF16 {
source source: UnsafeMutablePointer<T>, source source: UnsafeMutablePointer<T>,
destination: UnsafeMutablePointer<U>, destination: UnsafeMutablePointer<U>,
count: Int count: Int
) { ) {
if strideof(T.self) == strideof(U.self) { if strideof(T.self) == strideof(U.self) {
_memcpy( _memcpy(
dest: UnsafeMutablePointer(destination), dest: UnsafeMutablePointer(destination),
src: UnsafeMutablePointer(source), src: UnsafeMutablePointer(source),
size: UInt(count) * UInt(strideof(U.self))) size: UInt(count) * UInt(strideof(U.self)))
} }
else { else {
for i in 0..<count { for i in 0..<count {
let u16 = T._toUTF16CodeUnit((source + i).pointee) let u16 = T._toUTF16CodeUnit((source + i).pointee)
(destination + i).pointee = U._fromUTF16CodeUnit(u16) (destination + i).pointee = U._fromUTF16CodeUnit(u16)
}
} }
}
} }
/// Returns the number of UTF-16 code units required for the given code unit /// Returns the number of UTF-16 code units required for the given code unit
@@ -768,35 +769,35 @@ extension UTF16 {
public static func transcodedLength< public static func transcodedLength<
Encoding : UnicodeCodec, Input : IteratorProtocol Encoding : UnicodeCodec, Input : IteratorProtocol
where Encoding.CodeUnit == Input.Element where Encoding.CodeUnit == Input.Element
>( >(
of input: Input, of input: Input,
decodedAs sourceEncoding: Encoding.Type, decodedAs sourceEncoding: Encoding.Type,
repairingIllFormedSequences: Bool repairingIllFormedSequences: Bool
) -> (count: Int, isASCII: Bool)? { ) -> (count: Int, isASCII: Bool)? {
var input = input var input = input
var count = 0 var count = 0
var isAscii = true var isAscii = true
var inputDecoder = Encoding() var inputDecoder = Encoding()
loop: loop:
while true { while true {
switch inputDecoder.decode(&input) { switch inputDecoder.decode(&input) {
case .scalarValue(let us): case .scalarValue(let us):
if us.value > 0x7f { if us.value > 0x7f {
isAscii = false isAscii = false
} }
count += width(us) count += width(us)
case .emptyInput: case .emptyInput:
break loop break loop
case .error: case .error:
if !repairingIllFormedSequences { if !repairingIllFormedSequences {
return nil return nil
} }
isAscii = false isAscii = false
count += width(UnicodeScalar(0xfffd)) count += width(UnicodeScalar(0xfffd))
}
} }
return (count, isAscii) }
return (count, isAscii)
} }
} }
@@ -823,12 +824,12 @@ public func transcode<
InputEncoding : UnicodeCodec, InputEncoding : UnicodeCodec,
OutputEncoding : UnicodeCodec OutputEncoding : UnicodeCodec
where InputEncoding.CodeUnit == Input.Element where InputEncoding.CodeUnit == Input.Element
>( >(
inputEncoding: InputEncoding.Type, _ outputEncoding: OutputEncoding.Type, inputEncoding: InputEncoding.Type, _ outputEncoding: OutputEncoding.Type,
_ input: Input, _ output: (OutputEncoding.CodeUnit) -> Void, _ input: Input, _ output: (OutputEncoding.CodeUnit) -> Void,
stoppingOnError stopOnError: Bool stoppingOnError stopOnError: Bool
) -> Bool { ) -> Bool {
fatalError("unavailable function can't be called") fatalError("unavailable function can't be called")
} }
extension UTF16 { extension UTF16 {
@@ -836,9 +837,9 @@ extension UTF16 {
public static func measure< public static func measure<
Encoding : UnicodeCodec, Input : IteratorProtocol Encoding : UnicodeCodec, Input : IteratorProtocol
where Encoding.CodeUnit == Input.Element where Encoding.CodeUnit == Input.Element
>( >(
_: Encoding.Type, input: Input, repairIllFormedSequences: Bool _: Encoding.Type, input: Input, repairIllFormedSequences: Bool
) -> (Int, Bool)? { ) -> (Int, Bool)? {
fatalError("unavailable function can't be called") fatalError("unavailable function can't be called")
} }
} }