Is String's subscript O(n) when given a lot of combining code points?

FWIW, I'm having a hard time reproducing this locally:

$ swift repl <<EOF
import Foundation

let problematic = "a" + String(repeating: "\u{0301}", count: 20_000_000) + "x"

let start = Date()
print("Hello, World!".contains(problematic))
let end = Date()

print(end.timeIntervalSince(start))
EOF
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'lldb'
problematic: String = "á́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́́\u{cc}"...
start: Foundation.Date = 2023-10-17 05:21:24 GMT
false
end: Foundation.Date = 2023-10-17 05:21:24 GMT
0.12429308891296387

That last line -- the 0.124... -- is the number of seconds that contains check took, plus all the additional processing of piping this through the REPL instead of having a precompiled binary.

1 Like