Skip to content

Commit 6f508a5

Browse files
John Holdsworthjohnno1962
authored andcommitted
Character comparison operators.
1 parent 79fcb5e commit 6f508a5

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

stdlib/private/StdlibUnicodeUnittest/StdlibUnicodeUnittest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ public struct UTFTest {
142142
public let loc: SourceLoc
143143

144144
public var utf32: [UInt32] {
145-
return unicodeScalars.map(UInt32.init)
145+
return unicodeScalars.map({ s in UInt32(s) })
146146
}
147147

148148
public var utf32RepairedTail: [UInt32] {
149-
return unicodeScalarsRepairedTail.map(UInt32.init)
149+
return unicodeScalarsRepairedTail.map({ s in UInt32(s) })
150150
}
151151

152152
public init(

stdlib/public/core/IntegerTypes.swift.gyb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,6 +1786,14 @@ ${operatorComment(x.operator, True)}
17861786
}
17871787
}
17881788

1789+
extension ${Self} {
1790+
/// Construct with value `v.value`.
1791+
@inlinable @_alwaysEmitIntoClient
1792+
public init(unicode v: Unicode.Scalar) {
1793+
self = ${Self}(v.value)
1794+
}
1795+
}
1796+
17891797

17901798
extension ${Self}: Sendable { }
17911799

stdlib/public/core/UnicodeScalar.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,62 @@ extension UInt64 {
380380
}
381381
}
382382

383+
/// Allows direct comparisons between UInt8 and double quoted literals.
384+
extension UInt8 {
385+
/// Basic comparison operators
386+
@_transparent @_alwaysEmitIntoClient
387+
public static func == (i: Self, s: Unicode.Scalar) -> Bool {
388+
return i == UInt8(ascii: s)
389+
}
390+
@_transparent @_alwaysEmitIntoClient
391+
public static func != (i: Self, s: Unicode.Scalar) -> Bool {
392+
return i != UInt8(ascii: s)
393+
}
394+
@_transparent @_alwaysEmitIntoClient
395+
public static func <= (i: Self, s: Unicode.Scalar) -> Bool {
396+
return i <= UInt8(ascii: s)
397+
}
398+
@_transparent @_alwaysEmitIntoClient
399+
public static func >= (i: Self, s: Unicode.Scalar) -> Bool {
400+
return i >= UInt8(ascii: s)
401+
}
402+
@_transparent @_alwaysEmitIntoClient
403+
public static func < (i: Self, s: Unicode.Scalar) -> Bool {
404+
return i < UInt8(ascii: s)
405+
}
406+
@_transparent @_alwaysEmitIntoClient
407+
public static func > (i: Self, s: Unicode.Scalar) -> Bool {
408+
return i > UInt8(ascii: s)
409+
}
410+
/// Used in switch statements
411+
@_transparent @_alwaysEmitIntoClient
412+
public static func ~= (s: Unicode.Scalar, i: Self) -> Bool {
413+
return i == UInt8(ascii: s)
414+
}
415+
/// Useful now and then
416+
@_transparent @_alwaysEmitIntoClient
417+
public static func - (i: Self, s: Unicode.Scalar) -> Self {
418+
return i - UInt8(ascii: s)
419+
}
420+
}
421+
422+
extension UInt8? {
423+
/// Basic equality operators
424+
@_transparent @_alwaysEmitIntoClient
425+
public static func == (i: Self, s: Unicode.Scalar) -> Bool {
426+
return i == UInt8(ascii: s)
427+
}
428+
@_transparent @_alwaysEmitIntoClient
429+
public static func != (i: Self, s: Unicode.Scalar) -> Bool {
430+
return i != UInt8(ascii: s)
431+
}
432+
/// Used in switch statements
433+
@_transparent @_alwaysEmitIntoClient
434+
public static func ~= (s: Unicode.Scalar, i: Self) -> Bool {
435+
return i == UInt8(ascii: s)
436+
}
437+
}
438+
383439
extension Unicode.Scalar: Equatable {
384440
@inlinable
385441
public static func == (lhs: Unicode.Scalar, rhs: Unicode.Scalar) -> Bool {

0 commit comments

Comments
 (0)