Skip to content

Commit a669c65

Browse files
committed
wip: basic tilde equals support and test
1 parent bb6d0ce commit a669c65

File tree

2 files changed

+154
-2
lines changed

2 files changed

+154
-2
lines changed

stdlib/public/core/UTF8Span.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,12 @@ extension RawSpan.Cursor {
261261

262262
@available(SwiftStdlib 6.1, *)
263263
extension UTF8Span {
264-
static func ~=(_ lhs: UTF8Span, _ rhs: StaticString) -> Bool {
265-
fatalError()
264+
public static func ~=(_ lhs: StaticString, _ rhs: UTF8Span) -> Bool {
265+
return lhs.withUTF8Buffer { str in
266+
rhs.withUnsafeBufferPointer { span in
267+
str.elementsEqual(span)
268+
}
269+
}
266270
}
267271

268272
// Not doing == between two UTFSpan, as pointerness
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// RUN: %target-run-stdlib-swift(-enable-experimental-feature Span) -enable-experimental-feature Span %S/Inputs/
2+
3+
// REQUIRES: executable_test
4+
5+
import Swift
6+
import StdlibUnittest
7+
8+
var suite = TestSuite("UTF8SpanQueriesComparisons")
9+
defer { runAllTests() }
10+
11+
@available(SwiftStdlib 6.1, *)
12+
extension Array where Element == UInt8 {
13+
func withSpan<R>(_ f: (Span<Element>) throws -> R) rethrows -> R {
14+
try self.withUnsafeBufferPointer {
15+
try f(Span(_unsafeElements: $0))
16+
}
17+
}
18+
func withUTF8Span<R>(_ f: (UTF8Span) throws -> R) rethrows -> R {
19+
try self.withSpan { span in
20+
try f(try! UTF8Span(_validating: span))
21+
}
22+
}
23+
}
24+
25+
if #available(SwiftStdlib 6.1, *) {
26+
suite.test("UTF8Span/tilde equals") {
27+
Array("abcdefg".utf8).withUTF8Span { utf8Span in
28+
switch utf8Span {
29+
case "def":
30+
expectationFailure(
31+
"unexpected pattern match",
32+
trace: "",
33+
stackTrace: SourceLocStack().withCurrentLoc())
34+
case "abcdef":
35+
expectationFailure(
36+
"unexpected pattern match",
37+
trace: "",
38+
stackTrace: SourceLocStack().withCurrentLoc())
39+
case "abcdefg ":
40+
expectationFailure(
41+
"unexpected pattern match",
42+
trace: "",
43+
stackTrace: SourceLocStack().withCurrentLoc())
44+
case "abcdefg\0":
45+
expectationFailure(
46+
"unexpected pattern match",
47+
trace: "",
48+
stackTrace: SourceLocStack().withCurrentLoc())
49+
case "abcdefg":
50+
break
51+
default:
52+
expectationFailure(
53+
"expected a pattern match",
54+
trace: "",
55+
stackTrace: SourceLocStack().withCurrentLoc())
56+
}
57+
}
58+
}
59+
}
60+
61+
// TODO: Rest of this file is in-progress TODOs
62+
63+
64+
/*
65+
66+
67+
isASCII
68+
isKnownNFC
69+
checkForNFC(quickCheck:)
70+
isKnownSingleScalarCharacters
71+
checkForSingleScalarCharacters(quickCheck:)
72+
73+
public func bytesEqual(to other: UTF8Span) -> Bool
74+
public func bytesEqual(to other: some Sequence<UInt8>) -> Bool
75+
76+
public func scalarsEqual(
77+
to other: some Sequence<Unicode.Scalar>
78+
) -> Bool
79+
80+
public func charactersEqual(
81+
to other: some Sequence<Character>
82+
) -> Bool
83+
84+
public func isCanonicallyEquivalent(
85+
to other: UTF8Span
86+
) -> Bool
87+
88+
public func isCanonicallyLessThan(
89+
_ other: UTF8Span
90+
) -> Bool
91+
92+
*/
93+
94+
// @available(SwiftStdlib 6.1, *)
95+
// private struct QueryTestCase {
96+
// var content: String
97+
98+
// var loc: SourceLocStack
99+
100+
// var isASCII: Bool
101+
102+
// // TODO: This might become API, or otherwise calculated at init time
103+
// var isLatinyNFC: Bool {
104+
// bytes.allSatisfy { $0 < 0xCC }
105+
// }
106+
107+
// var isQuickNFC: Bool
108+
// var isNFC: Bool
109+
110+
// var isQuickSSC: Bool
111+
// var isSSC: Bool
112+
// }
113+
114+
// if #available(SwiftStdlib 6.1, *) {
115+
// suite.test("UTF8Span/queries") {
116+
// }
117+
// }
118+
119+
// enum ComparisonResult {
120+
// binaryEqual
121+
// canonicallyEqual
122+
// canonicallyLess
123+
// inequal
124+
// }
125+
126+
// private struct ComparisonTestCase {
127+
// var content: String
128+
// var comparisons: [(String, ComparisonResult)]
129+
130+
// var loc: SourceLocStack
131+
// }
132+
133+
// if #available(SwiftStdlib 6.1, *) {
134+
// suite.test("UTF8Span/comparisons") {
135+
// func test()
136+
// }
137+
// }
138+
139+
140+
/*
141+
142+
input string, to check the bits and relevant info
143+
comparison string and expected comparison level
144+
145+
*/
146+
147+
148+
// }

0 commit comments

Comments
 (0)