@@ -5,22 +5,22 @@ import StdlibUnittest
5
5
6
6
var StringIndexTests = TestSuite ( " StringIndexTests " )
7
7
8
- enum SimpleString : String {
9
- case smallASCII = " abcdefg "
10
- case smallUnicode = " abéÏ𓀀 "
11
- case largeASCII = " 012345678901234567890 "
12
- case largeUnicode = " abéÏ012345678901234567890𓀀 "
13
- case emoji = " 😀😃🤢🤮👩🏿🎤🧛🏻♂️🧛🏻♂️👩👩👦👦 "
14
- }
8
+ // enum SimpleString: String {
9
+ // case smallASCII = "abcdefg"
10
+ // case smallUnicode = "abéÏ𓀀"
11
+ // case largeASCII = "012345678901234567890"
12
+ // case largeUnicode = "abéÏ012345678901234567890𓀀"
13
+ // case emoji = "😀😃🤢🤮👩🏿🎤🧛🏻♂️🧛🏻♂️👩👩👦👦"
14
+ // }
15
15
16
- let simpleStrings : [ String ] = [
17
- SimpleString . smallASCII. rawValue,
18
- SimpleString . smallUnicode. rawValue,
19
- SimpleString . largeASCII. rawValue,
20
- SimpleString . largeUnicode. rawValue,
21
- SimpleString . emoji. rawValue,
22
- " " ,
23
- ]
16
+ // let simpleStrings: [String] = [
17
+ // SimpleString.smallASCII.rawValue,
18
+ // SimpleString.smallUnicode.rawValue,
19
+ // SimpleString.largeASCII.rawValue,
20
+ // SimpleString.largeUnicode.rawValue,
21
+ // SimpleString.emoji.rawValue,
22
+ // "",
23
+ // ]
24
24
25
25
// StringIndexTests.test("basic sanity checks") {
26
26
// for s in simpleStrings {
@@ -248,65 +248,137 @@ import Foundation
248
248
// }
249
249
// }
250
250
251
- StringIndexTests . test ( " Misaligned " ) {
252
- func doIt( _ str: String ) {
253
- let characterIndices = Array ( str. indices)
254
- let scalarIndices = Array ( str. unicodeScalars. indices) + [ str. endIndex]
255
- let utf8Indices = Array ( str. utf8. indices)
256
- let utf16Indices = Array ( str. utf16. indices)
257
-
258
- var lastScalarI = 0
259
- for i in 1 ..< utf8Indices. count {
260
- let idx = utf8Indices [ i]
261
-
262
- // Skip aligned indices
263
- guard idx < scalarIndices [ lastScalarI + 1 ] else {
264
- assert ( idx == scalarIndices [ lastScalarI + 1 ] )
265
- lastScalarI += 1
266
- continue
267
- }
268
- expectTrue ( UTF8 . isContinuation ( str. utf8 [ idx] ) )
251
+ // StringIndexTests.test("Misaligned") {
252
+ // func doIt(_ str: String) {
253
+ // let characterIndices = Array(str.indices)
254
+ // let scalarIndices = Array(str.unicodeScalars.indices) + [str.endIndex]
255
+ // let utf8Indices = Array(str.utf8.indices)
256
+ // let utf16Indices = Array(str.utf16.indices)
257
+
258
+ // var lastScalarI = 0
259
+ // for i in 1..<utf8Indices.count {
260
+ // let idx = utf8Indices[i]
261
+
262
+ // // Skip aligned indices
263
+ // guard idx < scalarIndices[lastScalarI + 1] else {
264
+ // assert(idx == scalarIndices[lastScalarI + 1])
265
+ // lastScalarI += 1
266
+ // continue
267
+ // }
268
+ // expectTrue(UTF8.isContinuation(str.utf8[idx]))
269
269
270
- let lastScalarIdx = scalarIndices [ lastScalarI]
270
+ // let lastScalarIdx = scalarIndices[lastScalarI]
271
271
272
- dump ( idx)
273
- dump ( lastScalarIdx)
274
- dump ( scalarIndices [ lastScalarI + 1 ] )
272
+ // dump(idx)
273
+ // dump(lastScalarIdx)
274
+ // dump(scalarIndices[lastScalarI + 1])
275
275
276
- // Check aligning-down
277
- expectEqual ( str [ lastScalarIdx] , str [ idx] )
278
- expectEqual ( str. utf16 [ lastScalarIdx] , str. utf16 [ idx] )
279
- expectEqual ( str. unicodeScalars [ lastScalarIdx] , str. unicodeScalars [ idx] )
276
+ // // Check aligning-down
277
+ // expectEqual(str[lastScalarIdx], str[idx])
278
+ // expectEqual(str.utf16[lastScalarIdx], str.utf16[idx])
279
+ // expectEqual(str.unicodeScalars[lastScalarIdx], str.unicodeScalars[idx])
280
+
281
+ // // Check distance
282
+ // let (start, end) = (str.startIndex, str.endIndex)
283
+ // if characterIndices.contains(lastScalarIdx) {
284
+ // expectEqual(0, str.distance(from: lastScalarIdx, to: idx))
285
+ // expectEqual(str[..<idx].count, str.distance(from: start, to: idx))
286
+ // expectEqual(str[idx...].count, str.distance(from: idx, to: end))
287
+ // }
288
+ // expectEqual(
289
+ // 0, str.unicodeScalars.distance(from: lastScalarIdx, to: idx))
290
+ // expectEqual(
291
+ // str.unicodeScalars[..<idx].count,
292
+ // str.unicodeScalars.distance(from: start, to: idx))
293
+ // expectEqual(
294
+ // str.unicodeScalars[idx...].count,
295
+ // str.unicodeScalars.distance(from: idx, to: end))
296
+
297
+ // expectEqual(0, str.utf16.distance(from: lastScalarIdx, to: idx))
298
+ // expectEqual(
299
+ // str.utf16[..<idx].count, str.utf16.distance(from: start, to: idx))
300
+ // expectEqual(
301
+ // str.utf16[idx...].count, str.utf16.distance(from: idx, to: end))
302
+ // }
303
+ // }
304
+
305
+ // let nsstring: NSString = "aодиde\u{301}日🧟♀️"
306
+ // doIt(nsstring as String)
307
+
308
+ // let string = "aодиde\u{301}日🧟♀️"
309
+ // doIt(string)
310
+ // }
311
+
312
+ StringIndexTests . test ( " Index interchange " ) {
313
+ // Exhaustively test aspects of string index interchange
314
+ func testInterchange(
315
+ _ str: String ,
316
+ stackTrace: SourceLocStack = SourceLocStack ( ) ,
317
+ showFrame: Bool = true ,
318
+ file: String = #file,
319
+ line: UInt = #line
320
+ ) {
321
+ let stackTrace = stackTrace. pushIf ( showFrame, file: file, line: line)
322
+ func expect(
323
+ _ condition: @autoclosure ( ) -> Bool ,
324
+ _ message: String = " " ,
325
+ file: String = #file,
326
+ line: UInt = #line
327
+ ) {
328
+ expectTrue ( condition ( ) , message,
329
+ stackTrace: stackTrace, showFrame: showFrame,
330
+ file: file, line: line)
331
+ }
280
332
281
- // Check distance
282
- let ( start, end) = ( str. startIndex, str. endIndex)
283
- if characterIndices. contains ( lastScalarIdx) {
284
- expectEqual ( 0 , str. distance ( from: lastScalarIdx, to: idx) )
285
- expectEqual ( str [ ..< idx] . count, str. distance ( from: start, to: idx) )
286
- expectEqual ( str [ idx... ] . count, str. distance ( from: idx, to: end) )
333
+ var curCharIdx = str. startIndex
334
+ var curScalarIdx = str. startIndex
335
+ var curUTF8Idx = str. startIndex
336
+ var curUTF16Idx = str. startIndex
337
+
338
+ while curCharIdx < str. endIndex {
339
+ let curChar = str [ curCharIdx]
340
+ expect ( curChar == str [ curScalarIdx] )
341
+ expect ( curChar == str [ curUTF8Idx] )
342
+ expect ( curChar == str [ curUTF16Idx] )
343
+
344
+ // Advance the character index once and have the scalar index catch up
345
+ str. formIndex ( after: & curCharIdx)
346
+
347
+ while curScalarIdx < curCharIdx {
348
+ let curScalar = str. unicodeScalars [ curScalarIdx]
349
+
350
+ print ( curScalar)
351
+ print ( curScalarIdx. encodedOffset)
352
+
353
+ let curSubChar = str [ curScalarIdx]
354
+
355
+ // Advance the scalar index once and have the code unit indices catch up
356
+ str. unicodeScalars. formIndex ( after: & curScalarIdx)
357
+
358
+ while curUTF8Idx < curScalarIdx {
359
+ expect ( curScalar == str. unicodeScalars [ curUTF8Idx] )
360
+ // expect(curSubChar == str[curUTF8Idx])
361
+ expect ( !UTF16. isTrailSurrogate ( str. utf16 [ curUTF8Idx] ) )
362
+ str. utf8. formIndex ( after: & curUTF8Idx)
363
+ }
364
+ expect ( curUTF8Idx == curScalarIdx)
365
+
366
+ while curUTF16Idx < curScalarIdx {
367
+ expect ( curScalar == str. unicodeScalars [ curUTF16Idx] )
368
+ // expect(curSubChar == str[curUTF16Idx])
369
+ expect ( !UTF8. isContinuation ( str. utf8 [ curUTF16Idx] ) )
370
+ str. utf16. formIndex ( after: & curUTF16Idx)
371
+ }
372
+ expect ( curUTF16Idx == curScalarIdx)
287
373
}
288
- expectEqual (
289
- 0 , str. unicodeScalars. distance ( from: lastScalarIdx, to: idx) )
290
- expectEqual (
291
- str. unicodeScalars [ ..< idx] . count,
292
- str. unicodeScalars. distance ( from: start, to: idx) )
293
- expectEqual (
294
- str. unicodeScalars [ idx... ] . count,
295
- str. unicodeScalars. distance ( from: idx, to: end) )
296
-
297
- expectEqual ( 0 , str. utf16. distance ( from: lastScalarIdx, to: idx) )
298
- expectEqual (
299
- str. utf16 [ ..< idx] . count, str. utf16. distance ( from: start, to: idx) )
300
- expectEqual (
301
- str. utf16 [ idx... ] . count, str. utf16. distance ( from: idx, to: end) )
302
374
}
303
375
}
304
376
305
- let nsstring : NSString = " aодиde \u{301} 日🧟♀️ "
306
- doIt ( nsstring as String )
377
+ testInterchange ( " abc \r \n defg " )
378
+ testInterchange ( ( " abc \r \n defg " as NSString ) as String )
307
379
308
- let string = " aодиde \ u{301}日🧟♀️ "
309
- doIt ( string )
380
+ testInterchange ( " ab \r \n cдиde \ u{301}日🧟♀️ " )
381
+ testInterchange ( ( " ab \r \n cдиde \u{301} 日🧟♀️ " as NSString ) as String )
310
382
}
311
383
312
384
#endif // _runtime(_ObjC)
0 commit comments