https://github.com/orchetect/swiftascii
Type-safe ASCIIString and ASCIICharacter types for Swift. (ASCII string, character)
https://github.com/orchetect/swiftascii
ascii character characters ios macos string strings swift swift5 swift5-3 tvos watchos
Last synced: 6 months ago
JSON representation
Type-safe ASCIIString and ASCIICharacter types for Swift. (ASCII string, character)
- Host: GitHub
- URL: https://github.com/orchetect/swiftascii
- Owner: orchetect
- License: mit
- Created: 2021-01-30T03:58:09.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-29T22:26:21.000Z (about 1 year ago)
- Last Synced: 2024-10-20T08:09:07.222Z (12 months ago)
- Topics: ascii, character, characters, ios, macos, string, strings, swift, swift5, swift5-3, tvos, watchos
- Language: Swift
- Homepage:
- Size: 96.7 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# SwiftASCII
[](https://swiftpackageindex.com/orchetect/SwiftASCII) [](https://swiftpackageindex.com/orchetect/SwiftASCII) [](https://github.com/orchetect/SwiftASCII/blob/main/LICENSE)
`ASCIIString` and `ASCIICharacter` types for Swift offering validation and lossy conversion from `String`.
Complete unit test coverage.
## Getting Started
1. SwiftASCII is available as a Swift Package that can be added as a dependency in your app or package.
- In an Xcode app project or framework add the package dependency to your target using this URL:
`https://github.com/orchetect/SwiftASCII`
- In a Swift Package, add it to the Package.swift dependencies:
```swift
.package(url: "https://github.com/orchetect/SwiftASCII", from: "1.1.5")
```
2. Import the library:```swift
import SwiftASCII
```### ASCIIString
```swift
// failable init
ASCIIString(exactly: "An ASCII String.") // succeeds
ASCIIString(exactly: "Ãñ ÂŚÇÏÎ Strïńg.") // nil// lossy string conversion making ASCII-compatible substitutions
ASCIIString("An ASCII String.") // "An ASCII String." (unchanged)
ASCIIString("Ãñ ÂŚÇÏÎ Strïńg.") // "An ASCII String." (substituted)// lossy string conversion through String literal type inference
let str: ASCIIString = "Ãñ ÂŚÇÏÎ Strïńg."
print(str) // "An ASCII String." (substituted)
``````swift
let asciiString = ASCIIString("ÂŚÇÏÎ")// returns typed as String
asciiString.stringValue // "ASCII"// returns Data representation of string
asciiString.rawData // Data([0x41, 0x53, 0x43, 0x49, 0x49])
```### ASCIICharacter
```swift
// failable init
ASCIICharacter(exactly: "A") // succeeds
ASCIICharacter(exactly: "Ω") // nil// lossy string conversion making ASCII-compatible substitutions
ASCIICharacter("A") // "A" (unchanged)
ASCIICharacter("Ã") // "A" (substituted)// lossy character conversion through Character literal type inference
let char: ASCIICharacter = "Ä"
print(char) // "A" (substituted)// failable ASCII integer literal init
ASCIICharacter(65) // "A"
ASCIICharacter(300) // nil
``````swift
let asciiString = ASCIICharacter("Ä")// returns typed as Character
asciiString.characterValue // Character("A")// returns ASCII integer literal
asciiString.asciiValue // 65// returns Data representation of character
asciiString.rawData // Data([0x41])
```## Author
Coded by a bunch of 🐹 hamsters in a trenchcoat that calls itself [@orchetect](https://github.com/orchetect).
## License
Licensed under the MIT license. See [LICENSE](https://github.com/orchetect/SwiftASCII/blob/master/LICENSE) for details.
## Contributions
Contributions are welcome. Feel free to post an Issue to discuss.