diff --git a/stdlib/public/core/StringUnicodeScalarView.swift b/stdlib/public/core/StringUnicodeScalarView.swift index 7546bb4099b..ee0386fb256 100644 --- a/stdlib/public/core/StringUnicodeScalarView.swift +++ b/stdlib/public/core/StringUnicodeScalarView.swift @@ -112,7 +112,9 @@ internal func _utf8Count( result += _utf8Count(&ptr, base: base, total: count, type: SIMD8.self) result += _utf8Count(&ptr, base: base, total: count, type: SIMD4.self) result += _utf8Count(&ptr, base: base, total: count, type: SIMD2.self) - result += Int(~ptr.loadUnaligned(as: UInt8.self) >> 7) + if ptr < (base + count) { + result += Int(~ptr.loadUnaligned(as: UInt8.self) >> 7) + } return result } #endif