Skip to content

Commit 085a53b

Browse files
author
John Holdsworth
committed
Short circuit instead of crash.
1 parent 34a5e41 commit 085a53b

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

stdlib/public/core/UnicodeScalar.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,18 +390,21 @@ extension UInt8 {
390390
/// - Returns: `true` when the `UInt8` is equal to the provided Unicode scalar; otherwise, `false`.
391391
@_transparent @_alwaysEmitIntoClient
392392
public static func == (i: Self, s: Unicode.Scalar) -> Bool {
393+
guard s.value < 128 else { return false }
393394
return i == UInt8(ascii: s)
394395
}
395396

396397
/// Returns a Boolean indicating whether the `UInt8` is not equal to the provided Unicode scalar.
397398
@_transparent @_alwaysEmitIntoClient
398399
public static func != (i: Self, s: Unicode.Scalar) -> Bool {
400+
guard s.value < 128 else { return true }
399401
return i != UInt8(ascii: s)
400402
}
401403

402404
/// Enables pattern matching of Unicode scalars in switch statements.
403405
@_transparent @_alwaysEmitIntoClient
404406
public static func ~= (s: Unicode.Scalar, i: Self) -> Bool {
407+
guard s.value < 128 else { return false }
405408
return i == UInt8(ascii: s)
406409
}
407410
}
@@ -416,18 +419,21 @@ extension UInt8? {
416419
/// - Returns: `true` if the optional `UInt8` is equal to the provided Unicode scalar; otherwise, `false`.
417420
@_transparent @_alwaysEmitIntoClient
418421
public static func == (i: Self, s: Unicode.Scalar) -> Bool {
422+
guard s.value < 128 else { return false }
419423
return i == UInt8(ascii: s)
420424
}
421425

422426
/// Returns a Boolean value indicating whether the optional `UInt8` is not equal to the provided Unicode scalar.
423427
@_transparent @_alwaysEmitIntoClient
424428
public static func != (i: Self, s: Unicode.Scalar) -> Bool {
429+
guard s.value < 128 else { return true }
425430
return i != UInt8(ascii: s)
426431
}
427432

428433
/// Allows pattern matching of Unicode scalars in switch statements.
429434
@_transparent @_alwaysEmitIntoClient
430435
public static func ~= (s: Unicode.Scalar, i: Self) -> Bool {
436+
guard s.value < 128 else { return false }
431437
return i == UInt8(ascii: s)
432438
}
433439
}

0 commit comments

Comments
 (0)