@@ -34,19 +34,11 @@ class RegexConsumerTests: XCTestCase {
34
34
) {
35
35
let regex = try ! Regex ( compiling: regex)
36
36
37
- let actualSeq : [ Range < Int > ] = string [ ... ] . ranges ( of: regex) . map {
38
- let start = string. offset ( ofIndex: $0. lowerBound)
39
- let end = string. offset ( ofIndex: $0. upperBound)
40
- return start..< end
41
- }
37
+ let actualSeq : [ Range < Int > ] = string [ ... ] . ranges ( of: regex) . map ( string. offsets ( of: ) )
42
38
XCTAssertEqual ( actualSeq, expected, file: file, line: line)
43
39
44
40
// `IndexingIterator` tests the collection conformance
45
- let actualCol : [ Range < Int > ] = string [ ... ] . ranges ( of: regex) [ ... ] . map {
46
- let start = string. offset ( ofIndex: $0. lowerBound)
47
- let end = string. offset ( ofIndex: $0. upperBound)
48
- return start..< end
49
- }
41
+ let actualCol : [ Range < Int > ] = string [ ... ] . ranges ( of: regex) [ ... ] . map ( string. offsets ( of: ) )
50
42
XCTAssertEqual ( actualCol, expected, file: file, line: line)
51
43
}
52
44
@@ -145,4 +137,39 @@ class RegexConsumerTests: XCTestCase {
145
137
XCTAssertEqual ( " x " , " axb " . trimming ( r) )
146
138
XCTAssertEqual ( " x " , " axbb " . trimming ( r) )
147
139
}
140
+
141
+ func testSubstring( ) throws {
142
+ let s = " aaa | aaaaaa | aaaaaaaaaa "
143
+ let s1 = s. dropFirst ( 6 ) // "aaaaaa | aaaaaaaaaa"
144
+ let s2 = s1. dropLast ( 17 ) // "aa"
145
+ let regex = try ! Regex ( compiling: " a+ " )
146
+
147
+ XCTAssertEqual ( s. firstMatch ( of: regex) ? . 0 , " aaa " )
148
+ XCTAssertEqual ( s1. firstMatch ( of: regex) ? . 0 , " aaaaaa " )
149
+ XCTAssertEqual ( s2. firstMatch ( of: regex) ? . 0 , " aa " )
150
+
151
+ XCTAssertEqual (
152
+ s. ranges ( of: regex) . map ( s. offsets ( of: ) ) ,
153
+ [ 0 ..< 3 , 6 ..< 12 , 15 ..< 25 ] )
154
+ XCTAssertEqual (
155
+ s1. ranges ( of: regex) . map ( s. offsets ( of: ) ) ,
156
+ [ 6 ..< 12 , 15 ..< 25 ] )
157
+ XCTAssertEqual (
158
+ s2. ranges ( of: regex) . map ( s. offsets ( of: ) ) ,
159
+ [ 6 ..< 8 ] )
160
+
161
+ XCTAssertEqual ( s. replacing ( regex, with: " " ) , " | | " )
162
+ XCTAssertEqual ( s1. replacing ( regex, with: " " ) , " | " )
163
+ XCTAssertEqual ( s2. replacing ( regex, with: " " ) , " " )
164
+
165
+ XCTAssertEqual (
166
+ s. _matches ( of: regex) . map ( \. 0 ) ,
167
+ [ " aaa " , " aaaaaa " , " aaaaaaaaaa " ] )
168
+ XCTAssertEqual (
169
+ s1. _matches ( of: regex) . map ( \. 0 ) ,
170
+ [ " aaaaaa " , " aaaaaaaaaa " ] )
171
+ XCTAssertEqual (
172
+ s2. _matches ( of: regex) . map ( \. 0 ) ,
173
+ [ " aa " ] )
174
+ }
148
175
}
0 commit comments