Skip to content

Commit f0a4cbf

Browse files
committed
Some tests
1 parent 950a344 commit f0a4cbf

File tree

2 files changed

+106
-17
lines changed

2 files changed

+106
-17
lines changed

stdlib/public/core/UnicodeScalar.swift

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -380,53 +380,89 @@ extension UInt64 {
380380
}
381381
}
382382

383-
extension FixedWidthInteger {
384-
/// Construct with value `v.value`.
385-
@inlinable @_alwaysEmitIntoClient
386-
public init(unicode v: Unicode.Scalar) {
387-
_precondition(v.value <= Self.max,
388-
"Code point value does not fit into type")
389-
self = Self(v.value)
390-
}
391-
}
392-
393-
/// Allows direct comparisons between UInt8 and double quoted literals.
383+
/// Extends `UInt8` to allow direct comparisons with double quoted literals.
394384
extension UInt8 {
395-
/// Basic equality operator
385+
/// Returns a Boolean indicating whether the `UInt8` is equal to the provided Unicode scalar.
386+
///
387+
/// - Parameters:
388+
/// - i: The `UInt8` value to compare.
389+
/// - s: The Unicode scalar to compare against.
390+
/// - Returns: `true` when the `UInt8` is equal to the provided Unicode scalar; otherwise, `false`.
396391
@_transparent @_alwaysEmitIntoClient
397392
public static func == (i: Self, s: Unicode.Scalar) -> Bool {
398393
return i == UInt8(ascii: s)
399394
}
400-
/// Basic inequality operator
395+
396+
/// Returns a Boolean indicating whether the `UInt8` is not equal to the provided Unicode scalar.
401397
@_transparent @_alwaysEmitIntoClient
402398
public static func != (i: Self, s: Unicode.Scalar) -> Bool {
403399
return i != UInt8(ascii: s)
404400
}
405-
/// Used in switch statements
401+
402+
/// Enables pattern matching of Unicode scalars in switch statements.
406403
@_transparent @_alwaysEmitIntoClient
407404
public static func ~= (s: Unicode.Scalar, i: Self) -> Bool {
408405
return i == UInt8(ascii: s)
409406
}
410407
}
411408

409+
/// Extends `Optional<UInt8>` to allow direct comparisons with double quoted literals.
412410
extension UInt8? {
413-
/// Optional equality operator
411+
/// Returns a Boolean value indicating whether the optional `UInt8` is equal to the provided Unicode scalar.
412+
///
413+
/// - Parameters:
414+
/// - i: The optional `UInt8` value to compare.
415+
/// - s: The Unicode scalar to compare against.
416+
/// - Returns: `true` if the optional `UInt8` is equal to the provided Unicode scalar; otherwise, `false`.
414417
@_transparent @_alwaysEmitIntoClient
415418
public static func == (i: Self, s: Unicode.Scalar) -> Bool {
416419
return i == UInt8(ascii: s)
417420
}
418-
/// Optional inequality operator
421+
422+
/// Returns a Boolean value indicating whether the optional `UInt8` is not equal to the provided Unicode scalar.
419423
@_transparent @_alwaysEmitIntoClient
420424
public static func != (i: Self, s: Unicode.Scalar) -> Bool {
421425
return i != UInt8(ascii: s)
422426
}
423-
/// Used in switch statements
427+
428+
/// Allows pattern matching of Unicode scalars in switch statements.
424429
@_transparent @_alwaysEmitIntoClient
425430
public static func ~= (s: Unicode.Scalar, i: Self) -> Bool {
426431
return i == UInt8(ascii: s)
427432
}
428433
}
429434

435+
/// Extends `Array` where Element is a FixedWidthInteger, providing initialization from a string of Unicode scalars.
436+
@_unavailableInEmbedded
437+
extension Array where Element: FixedWidthInteger {
438+
/// Initializes an array of Integers with Unicode scalars represented by the provided string.
439+
///
440+
/// - Parameter scalars: A string containing Unicode scalars.
441+
@inlinable @_alwaysEmitIntoClient @_unavailableInEmbedded
442+
public init(scalars: String) {
443+
#if os(Linux) || os(iOS) || os(tvOS)
444+
// How to avoid the function body being type checked for embedded?
445+
self.init(scalars.unicodeScalars.map { Element(unicode: $0) })
446+
#else
447+
self.init(scalars.utf16.map { Element($0) })
448+
#endif
449+
}
450+
}
451+
452+
/// Extends `FixedWidthInteger` providing initialization from a Unicode scalar.
453+
extension FixedWidthInteger {
454+
/// Initializes a FixedWidthInteger with the value of the provided Unicode scalar.
455+
///
456+
/// - Parameter unicode: The Unicode scalar to initialize from.
457+
/// - Note: Construct with value `v.value`.
458+
@inlinable @_alwaysEmitIntoClient
459+
public init(unicode v: Unicode.Scalar) {
460+
_precondition(v.value <= Self.max,
461+
"Code point value does not fit into type")
462+
self = Self(v.value)
463+
}
464+
}
465+
430466
extension Unicode.Scalar: Equatable {
431467
@inlinable
432468
public static func == (lhs: Unicode.Scalar, rhs: Unicode.Scalar) -> Bool {

test/Misc/character_ops.swift

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %target-run-simple-swift | %FileCheck %s
2+
// REQUIRES: executable_test
3+
4+
let c = UInt8(ascii: "a")
5+
6+
// CHECK: OK
7+
if c == "a" {
8+
print("OK")
9+
}
10+
11+
// CHECK: OK
12+
if c != "b" {
13+
print("OK")
14+
}
15+
16+
// CHECK: OK
17+
switch c {
18+
case "a":
19+
print("OK")
20+
case "b":
21+
fallthrough
22+
default:
23+
print("FAIL")
24+
}
25+
26+
let d: UInt8? = UInt8(ascii: "a")
27+
28+
// CHECK: OK
29+
if d == "a" {
30+
print("OK")
31+
}
32+
33+
// CHECK: OK
34+
if d != "b" {
35+
print("OK")
36+
}
37+
38+
// CHECK: OK
39+
switch d {
40+
case "a":
41+
print("OK")
42+
case "b":
43+
fallthrough
44+
default:
45+
print("FAIL")
46+
}
47+
48+
if UInt8(unicode: "a") == "a" {
49+
print("OK")
50+
}
51+
52+
// CHECK: 128512
53+
print(UInt64(unicode: "😀"))

0 commit comments

Comments
 (0)