Skip to content

[5.0] [String] UTF8View.withContiguousStorageIfAvailable #21194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions stdlib/public/core/StringUTF8View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,13 @@ extension String.Index {
return target._guts.isOnUnicodeScalarBoundary(self)
}
}

extension String.UTF8View {
@inlinable
public func withContiguousStorageIfAvailable<R>(
_ body: (UnsafeBufferPointer<Element>) throws -> R
) rethrows -> R? {
guard _guts.isFastUTF8 else { return nil }
return try _guts.withFastUTF8(body)
}
}
98 changes: 98 additions & 0 deletions validation-test/stdlib/StringUTF8.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// RUN: %empty-directory(%t)
// RUN: if [ %target-runtime == "objc" ]; \
// RUN: then \
// RUN: %target-clang -fobjc-arc %S/Inputs/NSSlowString/NSSlowString.m -c -o %t/NSSlowString.o && \
// RUN: %target-build-swift -I %S/Inputs/NSSlowString/ %t/NSSlowString.o %s -o %t/String; \
// RUN: else \
// RUN: %target-build-swift %s -o %t/String; \
// RUN: fi

// RUN: %target-codesign %t/String
// RUN: %target-run %t/String
// REQUIRES: executable_test
// XFAIL: interpret

import StdlibUnittest
import StdlibCollectionUnittest

#if _runtime(_ObjC)
import NSSlowString
import Foundation // For NSRange
#endif

extension String {
func withFastUTF8IfAvailable<R>(
_ f: (UnsafeBufferPointer<UInt8>) throws -> R
) rethrows -> R? {
return try utf8.withContiguousStorageIfAvailable(f)
}
var isFastUTF8: Bool {
return withFastUTF8IfAvailable({ _ in return 0 }) != nil
}
mutating func makeNative() { self += "" }

var isASCII: Bool { return utf8.allSatisfy { $0 < 0x7f } }
}

var UTF8Tests = TestSuite("StringUTF8Tests")

var strings: Array<String> = [
"abcd",
"abcdefghijklmnop",
"abcde\u{301}fghijk",
"a\u{301}",
"👻",
"Spooky long string. 👻",
"в чащах юга жил-был цитрус? да, но фальшивый экземпляр",
"日",
]

let kCFStringEncodingASCII: UInt32 = 0x0600

UTF8Tests.test("Contiguous Access") {
for string in strings {
print(string)

// Native strings are contiguous UTF-8
expectTrue(string.isFastUTF8)
expectEqualSequence(
Array(string.utf8), string.withFastUTF8IfAvailable(Array.init)!)

// FIXME: Bridge small non-ASCII as StringStorage
// expectTrue(((string as NSString) as String).isFastUTF8)

var copy = string
expectTrue(copy.isFastUTF8)
copy.makeNative()
expectTrue(copy.isFastUTF8)

// FIXME: Bridge small non-ASCII as StringStorage
// expectTrue(((copy as NSString) as String).isFastUTF8)

#if _runtime(_ObjC)
// Lazily bridged strings are not contiguous UTF-8
var slowString = NSSlowString(string: string) as String
expectFalse(slowString.isFastUTF8)
expectEqualSequence(string.utf8, slowString.utf8)

// They become fast when mutated
slowString.makeNative()
expectTrue(slowString.isFastUTF8)
expectEqualSequence(
string.utf8, slowString.withFastUTF8IfAvailable(Array.init)!)

// Contiguous ASCII CFStrings provide access, even if lazily bridged
if string.isASCII {
let cfString = string.withCString {
CFStringCreateWithCString(nil, $0, kCFStringEncodingASCII)!
} as String
expectTrue(cfString.isFastUTF8)
expectEqualSequence(
string.utf8, cfString.withFastUTF8IfAvailable(Array.init)!)
}
#endif
}
}

runAllTests()