Skip to content

Commit 6e80e8f

Browse files
committed
Streamlining.
1 parent 5b085fe commit 6e80e8f

File tree

2 files changed

+25
-45
lines changed

2 files changed

+25
-45
lines changed

stdlib/public/core/UnicodeScalar.swift

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,15 @@ extension UInt8 {
364364
"Code point value does not fit into ASCII")
365365
self = UInt8(v.value)
366366
}
367+
/// Initializes a UInt8 with the ASCII value of the provided Unicode scalar if possible.
368+
///
369+
/// - Parameter unicode: The Unicode scalar to initialize from.
370+
/// - Note: Construct with value `v.value`.
371+
@inlinable @_alwaysEmitIntoClient
372+
public init?(ifASCII v: Unicode.Scalar) {
373+
guard v.value < 128 else { return nil }
374+
self = Self(v.value)
375+
}
367376
}
368377
extension UInt32 {
369378
/// Construct with value `v.value`.
@@ -380,51 +389,6 @@ extension UInt64 {
380389
}
381390
}
382391

383-
/// Extends `UInt8` providing initialization from a Unicode scalar.
384-
extension UInt8 {
385-
/// Converts a UInt8 ASCII value into a UnicodeScalar
386-
@_transparent @_alwaysEmitIntoClient
387-
var asciiScalar: Unicode.Scalar? {
388-
guard self < 0x80 else { return nil }
389-
return Unicode.Scalar(self)
390-
}
391-
/// Initializes a FixedWidthInteger with the value of the provided Unicode scalar.
392-
///
393-
/// - Parameter unicode: The Unicode scalar to initialize from.
394-
/// - Note: Construct with value `v.value`.
395-
@inlinable @_alwaysEmitIntoClient
396-
public init?(ifASCII v: Unicode.Scalar) {
397-
guard v.value < 0x80 else { return nil }
398-
self = Self(v.value)
399-
}
400-
}
401-
402-
/// Extends `UInt8` to allow direct comparisons with double quoted literals.
403-
extension UInt8 {
404-
/// Returns a Boolean indicating whether the `UInt8` is equal to the provided Unicode scalar.
405-
///
406-
/// - Parameters:
407-
/// - i: The `UInt8` value to compare.
408-
/// - s: The Unicode scalar to compare against.
409-
/// - Returns: `true` when the `UInt8` is equal to the provided Unicode scalar; otherwise, `false`.
410-
@_transparent @_alwaysEmitIntoClient
411-
public static func == (i: Self, s: Unicode.Scalar) -> Bool {
412-
return i == UInt8(ifASCII: s)
413-
}
414-
415-
/// Returns a Boolean indicating whether the `UInt8` is not equal to the provided Unicode scalar.
416-
@_transparent @_alwaysEmitIntoClient
417-
public static func != (i: Self, s: Unicode.Scalar) -> Bool {
418-
return i != UInt8(ifASCII: s)
419-
}
420-
421-
/// Enables pattern matching of Unicode scalars in switch statements.
422-
@_transparent @_alwaysEmitIntoClient
423-
public static func ~= (s: Unicode.Scalar, i: Self) -> Bool {
424-
return i == UInt8(ifASCII: s)
425-
}
426-
}
427-
428392
/// Extends `Optional<UInt8>` to allow direct comparisons with double quoted literals.
429393
extension UInt8? {
430394
/// Returns a Boolean value indicating whether the optional `UInt8` is equal to the provided Unicode scalar.
@@ -462,6 +426,12 @@ extension FixedWidthInteger {
462426
guard v.value <= Self.max else { return nil }
463427
self = Self(v.value)
464428
}
429+
/// Converts an ASCII value into a UnicodeScalar
430+
@inlinable @_alwaysEmitIntoClient
431+
public var asciiScalar: Unicode.Scalar? {
432+
guard self < 128 && self >= 0 else { return nil }
433+
return Unicode.Scalar(UInt8(self))
434+
}
465435
}
466436

467437
extension Unicode.Scalar: Equatable {

test/Misc/character_ops.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ default:
4545
print("FAIL")
4646
}
4747

48+
// CHECK: OK
49+
switch d?.asciiScalar {
50+
case "a":
51+
print("OK")
52+
case "b":
53+
fallthrough
54+
default:
55+
print("FAIL")
56+
}
57+
4858
// CHECK: OK
4959
if UInt8(unicode: "a") == "a" {
5060
print("OK")

0 commit comments

Comments
 (0)