This is a very small pitch to gauge interest and figure out whether there are any sharp edges I can’t see. So, without further ado:
Expose whether Swift strings are ASCII
Introduction
This proposal introduces an API that exposes whether a Swift String or Substring is known to be ASCII.
Motivation
There are a number of contexts in which a String that is known to be ASCII can be handled in a more-optimal fashion than other kinds of strings. This is particularly common in network programming, where non-ASCII strings are frequently either forbidden or require special handling.
Swift strings already keep track of whether they are known to be ASCII, but this is not currently API. This means that to answer this question, programmers are forced to do a scan of the String to look at every byte. This is entirely redundant computation that could easily be elided.
As an example of the need, I will note that Apple itself has implemented String-is-ASCII checks at least 5 times in public repositories:
Presumably even more exist in the wider world.
Generally speaking it is considered good practice not to perform unnecessary computations. In this case, all of these computations are unnecessary. All Swift string objects know, statically, whether they are ASCII or not. This is determined by a simple static computation that relies on bitmasking a number of fields in the string.
This pitch proposes making this access public, and regaining the cycles spent checking for something we already know.
Detailed design
This pitch proposes to add a new public API on String and Substring:
public var isASCII: Bool { get }
This API will be implemented on top of the existing APIs on _StringGuts:
@inlinable @inline(__always)
internal var isASCII: Bool {
return _object.isASCII
}
This API will be public and @_alwaysEmitIntoClient. The reason for the latter is that it enables us to back-deploy this accessor. The APIs it relies upon are as available as String, so making this API AEIC ensures that we are able to offer this API without requiring an availability guard.
Alternatives considered
There are very few alternatives. The current status quo is one.
We could consider promoting the full testing API surface to public API, to enable more introspection of String representations. This is probably out of scope. String ‘s representational choices are mostly of no interest to users of Swift. Whether a String is small or not is rarely immediately relevant. Similarly, its capacity and count are already exposed in other places.
Source compatibility
This proposal has no impact on source compatibility.
Effect on ABI stability
This proposal has no impact on ABI stability, as no new symbols will be added.