@@ -19,6 +19,8 @@ class TestNSRange : XCTestCase {
19
19
( " test_NSUnionRange " , test_NSUnionRange) ,
20
20
( " test_NSIntersectionRange " , test_NSIntersectionRange) ,
21
21
( " test_NSStringFromRange " , test_NSStringFromRange) ,
22
+ ( " test_init_region_in_ascii_string " , test_init_region_in_ascii_string) ,
23
+ ( " test_init_region_in_unicode_string " , test_init_region_in_unicode_string) ,
22
24
]
23
25
}
24
26
@@ -126,4 +128,52 @@ class TestNSRange : XCTestCase {
126
128
XCTAssertEqual ( NSStringFromRange ( range) , string)
127
129
}
128
130
}
131
+
132
+ private func _assert< S: StringProtocol , R: RangeExpression > (
133
+ _ region: R , in target: S , is rangeString: String
134
+ ) where R. Bound == S . Index {
135
+ XCTAssertEqual ( NSStringFromRange ( NSRange ( region, in: target) ) , rangeString)
136
+ }
137
+
138
+ func test_init_region_in_ascii_string( ) {
139
+ // all count = 18
140
+ let normalString = " 1;DROP TABLE users "
141
+
142
+ _assert ( normalString. index ( normalString. startIndex, offsetBy: 2 ) ..< normalString. index ( normalString. endIndex, offsetBy: - 6 ) , in: normalString, is: " {2, 10} " )
143
+ _assert ( normalString. index ( after: normalString. startIndex) ... normalString. index ( before: normalString. endIndex) , in: normalString, is: " {1, 17} " )
144
+ _assert ( normalString. startIndex... , in: normalString, is: " {0, 18} " )
145
+ _assert ( ... normalString. firstIndex ( of: " " ) !, in: normalString, is: " {0, 7} " )
146
+ _assert ( ..< normalString. lastIndex ( of: " " ) !, in: normalString, is: " {0, 12} " )
147
+
148
+ let normalSubstring : Substring = normalString. split ( separator: " ; " ) [ 1 ]
149
+
150
+ _assert ( normalSubstring. range ( of: " TABLE " ) !, in: normalSubstring, is: " {5, 5} " )
151
+ _assert ( normalSubstring. index ( after: normalSubstring. firstIndex ( of: " " ) !) ..< normalSubstring. lastIndex ( of: " " ) !, in: normalString, is: " {7, 5} " )
152
+ _assert ( normalSubstring. firstIndex ( of: " u " ) !... normalSubstring. lastIndex ( of: " u " ) !, in: normalSubstring, is: " {11, 1} " )
153
+ _assert ( normalSubstring. startIndex... , in: normalSubstring, is: " {0, 16} " )
154
+ _assert ( normalSubstring. startIndex... , in: normalString, is: " {2, 16} " )
155
+ _assert ( ... normalSubstring. lastIndex ( of: " " ) !, in: normalSubstring, is: " {0, 11} " )
156
+ _assert ( ..< normalSubstring. lastIndex ( of: " " ) !, in: normalString, is: " {0, 12} " )
157
+ }
158
+
159
+ func test_init_region_in_unicode_string( ) {
160
+ // count: 46, utf8: 90, utf16: 54
161
+ let unicodeString = " This is a #naughty👻 string (╯°□°)╯︵ ┻━┻👨👩👧👦) "
162
+
163
+ _assert ( unicodeString. index ( unicodeString. startIndex, offsetBy: 10 ) ..< unicodeString. index ( unicodeString. startIndex, offsetBy: 28 ) , in: unicodeString, is: " {10, 19} " )
164
+ _assert ( unicodeString. index ( after: unicodeString. startIndex) ... unicodeString. index ( before: unicodeString. endIndex) , in: unicodeString, is: " {1, 53} " )
165
+ _assert ( unicodeString. startIndex... , in: unicodeString, is: " {0, 54} " )
166
+ _assert ( ... unicodeString. firstIndex ( of: " 👻 " ) !, in: unicodeString, is: " {0, 22} " )
167
+ _assert ( ..< unicodeString. range ( of: " 👨👩👧👦 " ) !. lowerBound, in: unicodeString, is: " {0, 42} " )
168
+
169
+ let unicodeSubstring : Substring = unicodeString [ unicodeString. firstIndex ( of: " 👻 " ) !... ]
170
+
171
+ _assert ( unicodeSubstring. range ( of: " 👨👩👧👦 " ) !, in: unicodeSubstring, is: " {22, 11} " )
172
+ _assert ( unicodeSubstring. range ( of: " 👨 " ) !. lowerBound..< unicodeSubstring. range ( of: " 👦 " ) !. upperBound, in: unicodeString, is: " {42, 11} " )
173
+ _assert ( unicodeSubstring. index ( after: unicodeSubstring. startIndex) ... unicodeSubstring. index ( before: unicodeSubstring. endIndex) , in: unicodeSubstring, is: " {2, 32} " )
174
+ _assert ( unicodeSubstring. startIndex... , in: unicodeSubstring, is: " {0, 34} " )
175
+ _assert ( unicodeSubstring. startIndex... , in: unicodeString, is: " {20, 34} " )
176
+ _assert ( ... unicodeSubstring. firstIndex ( of: " ╯ " ) !, in: unicodeSubstring, is: " {0, 12} " )
177
+ _assert ( ..< unicodeSubstring. firstIndex ( of: " ╯ " ) !, in: unicodeString, is: " {0, 31} " )
178
+ }
129
179
}
0 commit comments