A zero-cost, type-safe Swift representation of ASCII characters with comprehensive formatting options and convenient utilities.
- 🚀 Zero runtime overhead with compile-time safety
- 💪 Complete ASCII character set (0-127) as type-safe enum
- 🛠 Multiple representation formats
- ⚡️ Direct integer comparisons
- 🧰 Convenient character properties
- 📚 Extensive documentation
- ✨ Pure Swift implementation
Add the following to your Package.swift
file:
let package = Package(
// name, platforms, products, etc.
dependencies: [
// other dependencies
.package(url: "https://github.com/besya/ascii.git", from: "1.0.1"),
],
targets: [
.target(name: "<library>", dependencies: [
// other dependencies
.product(name: "ASCII", package: "ASCII"),
]),
// other targets
]
)
import ASCII
let bracket = ASCII.leftBracket
ASCII(91) == ASCII.leftBracket // true
ASCII(UInt8(91)) == ASCII.leftBracket // true
ASCII("[") == ASCII.leftBracket // true
let CSI: [ASCII] = [.escape, .leftBracket]
let sgrBoldSequence: [ASCII] = CSI + [.digit1, .m]
let sgrItalicSequence: [ASCII] = CSI + [.digit3, .m]
let sgrResetSequence: [ASCII] = CSI + [.digit0, .m]
let boldOn: String = sgrBoldSequence.map(String.init).joined()
let italicOn: String = sgrItalicSequence.map(String.init).joined()
let reset: String = sgrResetSequence.map(String.init).joined()
print(boldOn + "Hello," + reset + italicOn + " World!")
// Prints: **Hello,** _World!_
bracket // ASCII
bracket.int // Int(91)
bracket.decimal // UInt8(91)
bracket.binary // StaticString("01011011")
bracket.hexadecimal // StaticString("5B")
bracket.octal // StaticString("133")
bracket.unicode // StaticString("\u{005B}")
bracket.escaped // StaticString("\\u{005B}")
bracket.htmlEntity // StaticString("[")
ASCII.leftBracket == 91 // true
91 == ASCII.leftBracket // true
UInt8(90) < ASCII.leftBracket // true
ASCII.leftBracket <= UInt8(92) // true
ASCII.A.isDigit // false
ASCII.A.isLetter // true
ASCII.A.isControl // false
ASCII.A.isLowercase // false
ASCII.A.isUppercase // true
ASCII.A.isPrintable // true
ASCII.A.isWhitespace // false
ASCII.A.isAlphanumeric // true
- Type Safety: Prevent invalid ASCII values at compile time
- Performance: Zero-cost abstractions with no runtime overhead
- Convenience: Rich set of utility methods and properties
- Clarity: Descriptive enum cases instead of raw values
- Versatility: Multiple representation formats for different use cases
- Swift 5.0+
- iOS 13.0+ / macOS 10.15+ / tvOS 13.0+ / watchOS 6.0+
This project is available under the MIT license. See the LICENSE file for more info.
Contributions are welcome! Please feel free to submit a Pull Request.