mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Introduce checking of ConcurrentValue conformances: - For structs, check that each stored property conforms to ConcurrentValue - For enums, check that each associated value conforms to ConcurrentValue - For classes, check that each stored property is immutable and conforms to ConcurrentValue Because all of the stored properties / associated values need to be visible for this check to work, limit ConcurrentValue conformances to be in the same source file as the type definition. This checking can be disabled by conforming to a new marker protocol, UnsafeConcurrentValue, that refines ConcurrentValue. UnsafeConcurrentValue otherwise his no specific meaning. This allows both "I know what I'm doing" for types that manage concurrent access themselves as well as enabling retroactive conformance, both of which are fundamentally unsafe but also quite necessary. The bulk of this change ended up being to the standard library, because all conformances of standard library types to the ConcurrentValue protocol needed to be sunk down into the standard library so they would benefit from the checking above. There were numerous little mistakes in the initial pass through the stsandard library types that have now been corrected.
90 lines
2.4 KiB
Swift
90 lines
2.4 KiB
Swift
//===--- UTF32.swift ------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
extension Unicode {
|
|
@frozen
|
|
public enum UTF32: ConcurrentValue {
|
|
case _swift3Codec
|
|
}
|
|
}
|
|
|
|
extension Unicode.UTF32: Unicode.Encoding {
|
|
public typealias CodeUnit = UInt32
|
|
public typealias EncodedScalar = CollectionOfOne<UInt32>
|
|
|
|
@inlinable
|
|
internal static var _replacementCodeUnit: CodeUnit {
|
|
@inline(__always) get { return 0xFFFD }
|
|
}
|
|
|
|
@inlinable
|
|
public static var encodedReplacementCharacter: EncodedScalar {
|
|
return EncodedScalar(_replacementCodeUnit)
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
public static func _isScalar(_ x: CodeUnit) -> Bool {
|
|
return true
|
|
}
|
|
|
|
/// Returns whether the given code unit represents an ASCII scalar
|
|
@_alwaysEmitIntoClient
|
|
public static func isASCII(_ x: CodeUnit) -> Bool {
|
|
return x <= 0x7F
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
public static func decode(_ source: EncodedScalar) -> Unicode.Scalar {
|
|
return Unicode.Scalar(_unchecked: source.first!)
|
|
}
|
|
|
|
@inlinable
|
|
@inline(__always)
|
|
public static func encode(
|
|
_ source: Unicode.Scalar
|
|
) -> EncodedScalar? {
|
|
return EncodedScalar(source.value)
|
|
}
|
|
|
|
@frozen
|
|
public struct Parser: ConcurrentValue {
|
|
@inlinable
|
|
public init() { }
|
|
}
|
|
|
|
public typealias ForwardParser = Parser
|
|
public typealias ReverseParser = Parser
|
|
}
|
|
|
|
extension UTF32.Parser: Unicode.Parser {
|
|
public typealias Encoding = Unicode.UTF32
|
|
|
|
/// Parses a single Unicode scalar value from `input`.
|
|
@inlinable
|
|
public mutating func parseScalar<I: IteratorProtocol>(
|
|
from input: inout I
|
|
) -> Unicode.ParseResult<Encoding.EncodedScalar>
|
|
where I.Element == Encoding.CodeUnit {
|
|
let n = input.next()
|
|
if _fastPath(n != nil), let x = n {
|
|
// Check code unit is valid: not surrogate-reserved and within range.
|
|
guard _fastPath((x &>> 11) != 0b1101_1 && x <= 0x10ffff)
|
|
else { return .error(length: 1) }
|
|
|
|
// x is a valid scalar.
|
|
return .valid(UTF32.EncodedScalar(x))
|
|
}
|
|
return .emptyInput
|
|
}
|
|
}
|