Skip to content

orchetect/swift-ascii

Repository files navigation

swift-ascii

License: MIT

Introduces ASCIIString and ASCIICharacter types for Swift offering validation and lossy conversion from String.

Complete unit test coverage.

Installation

Swift Package Manager (SPM)

To add this package to an Xcode app project, use:

https://github.com/orchetect/swift-ascii as the URL.

To add this package to a Swift package, add the dependency to your package and target in Package.swift:

let package = Package(
    dependencies: [
        .package(url: "https://github.com/orchetect/swift-ascii", from: "1.3.0")
    ],
    targets: [
        .target(
            dependencies: [
                .product(name: "SwiftASCII", package: "swift-ascii")
            ]
        )
    ]
)

ASCIIString

// 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)
let asciiString = ASCIIString("ÂŚÇÏÎ")

// returns typed as String
asciiString.stringValue // "ASCII"

// returns Data representation of string
asciiString.rawData // Data([0x41, 0x53, 0x43, 0x49, 0x49])

ASCIICharacter

// 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
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.

License

Licensed under the MIT license. See LICENSE for details.

Community & Support

Please do not email maintainers for technical support. Several options are available for issues and questions:

  • Questions and feature ideas can be posted to Discussions.
  • If an issue is a verifiable bug with reproducible steps it may be posted in Issues.

Contributions

Contributions are welcome. Posting in Discussions first prior to new submitting PRs for features or modifications is encouraged.

Legacy

This repository was formerly known as SwiftASCII.

Sponsor this project

 

Contributors 2

  •  
  •  

Languages