File tree Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Expand file tree Collapse file tree 1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change @@ -492,8 +492,29 @@ extension String.UTF8View.Iterator : IteratorProtocol {
492
492
extension String . UTF8View {
493
493
public var count : Int {
494
494
if _fastPath ( _core. isASCII) { return _core. count }
495
+ let b = _core. _unmanagedUTF16
496
+ if _fastPath ( b != nil ) {
497
+ defer { _fixLifetime ( _core) }
498
+ return _count ( fromUTF16: b!)
499
+ }
500
+ return _count ( fromUTF16: self . _core)
501
+ }
502
+
503
+ internal func _count< Source: Sequence > ( fromUTF16 source: Source ) -> Int
504
+ where Source. Element == Unicode . UTF16 . CodeUnit
505
+ {
495
506
var result = 0
496
- for _ in self { result += 1 }
507
+ var prev : Unicode . UTF16 . CodeUnit = 0
508
+ for u in source {
509
+ switch u {
510
+ case 0 ..< 0x80 : result += 1
511
+ case 0x80 ..< 0x800 : result += 2
512
+ case 0x800 ..< 0xDC00 : result += 3
513
+ case 0xDC00 ..< 0xE000 : result += UTF16 . isLeadSurrogate ( prev) ? 1 : 3
514
+ default : result += 3
515
+ }
516
+ prev = u
517
+ }
497
518
return result
498
519
}
499
520
}
You can’t perform that action at this time.
0 commit comments