ASCII validation, case conversion, line-ending normalization, and decimal serialization for Swift strings, characters, and integers, built on the INCITS 4-1986 (US-ASCII) specification.
- Validated
Stringconstruction —String(ascii:)builds a string from bytes orASCII.Codevalues and returnsnilwhen any byte falls outside the 7-bit range (0x00–0x7F), instead of decoding it to a replacement character. - ASCII-only case conversion —
string.ascii.uppercased()/.lowercased()fold the lettersA–Z/a–zand leave every non-ASCII scalar untouched. - Line-ending normalization —
normalized(to:)rewrites mixed LF, CR, and CRLF endings to one consistent style. - 7-bit ASCII predicate —
string.ascii.isAllASCIIreports whether a string contains only US-ASCII characters. - Decimal integer serialization —
Int,Int64,UInt, andUInt64serialize to their ASCII decimal byte representation throughBinary.Serializable. - INCITS 4-1986 grounding — classification, case, and format-effector behavior follow the US-ASCII specification.
import ASCII
// Reject non-ASCII on construction instead of decoding it to replacement characters.
let greeting = String(ascii: [104, 101, 108, 108, 111]) // "hello"
let rejected = String(ascii: [0xFF]) // nil — 0xFF is not 7-bit ASCII
// Test whether a string is pure 7-bit ASCII.
let pure = "hello".ascii.isAllASCII // true
let mixed = "café".ascii.isAllASCII // false
// Fold only ASCII letters; non-ASCII scalars pass through unchanged.
let shout = "HELLO🌍".ascii.lowercased() // "hello🌍"
// Normalize mixed CR / LF / CRLF line endings to a single style.
let body = "line1\nline2\r\nline3".normalized(to: .crlf)
// "line1\r\nline2\r\nline3"dependencies: [
.package(url: "https://github.com/swift-foundations/swift-ascii.git", branch: "main")
].target(
name: "YourTarget",
dependencies: [
.product(name: "ASCII", package: "swift-ascii")
]
)Requires Swift 6.3.1. The ASCII product supports Windows as a host platform;
the repository's universal CI includes its required Windows build-and-test leg.
Package.swift declares minimum deployment versions for Apple platforms:
macOS 26, iOS 26, tvOS 26, watchOS 26, and visionOS 26.
Discussion thread will be created at first public release.
Apache 2.0. See LICENSE.