swift-atoms/swift-ascii
Use this package when you read or write bytes as ASCII text: naming characters by their ASCII names, classifying and case-converting bytes, normalising line endings, and converting integers and decimal floating-point numbers to and from ASCII digits. Start from ASCII.Code, one
Laws
- Classification works on byte values, not Unicode properties. Whitespace is exactly HT, LF, CR and SP: VT and FF do not count. Control is 0x00 to 0x1F plus DEL. Printable is 0x20 to 0x7E, and visible is the same range without SP. A byte at or above 0x80 satisfies no classification predicate.
ASCII.Classification.isAllLowercaseandASCII.Classification.isAllUppercasehold when no letter of the other case occurs, so input that is empty or holds only digits satisfies both. - Case conversion changes only A to Z and a to z. Every other byte, and every non-ASCII
Swift.Character, comes back unchanged. ASCII.normalized(_:to:)treats a CRLF pair, a lone CR and a lone LF each as one line ending and replaces each with the target ending. Input that contains neither CR nor LF comes back byte for byte.- The integer parsers read an optional sign, then digits of their radix, and stop before the first byte that is not such a digit. That byte stays in the input. Hexadecimal letters are accepted in either case. On any failure, the input is restored to where the parse began. A negative count is rejected before any byte is read.
- With
ASCII.Digits.Sign.none, a leading plus or minus sign is not consumed, so the parse fails for lack of digits. WithASCII.Digits.Sign.optional, a minus sign is accepted only when the output type is signed. Negative numbers accumulate towards the type's minimum, so that minimum parses without overflow. Overflow is checked after every digit, and it fails the whole number. ASCII.Digits.Count.exactlyfails unless exactly that many digits are present, and it never reads a further digit. A count of exactly zero always fails.ASCII.Digits.Count.atMoststops after that many digits and needs at least one.ASCII.Decimal.Float.Parserreads an optional sign, digits, an optional point followed by digits, and an optional exponent: e or E, an optional sign, then digits. It needs at least one digit before or after the point. An exponent marker that has no digits after it is left in the input. The result is the correctly roundedDouble, bit-identical to whatSwift.Doubleproduces from the same text, including negative zero and subnormal values. An exponent beyond the representable range gives zero or infinity with the parsed sign rather than a failure. There is no syntax for infinity, NaN or hexadecimal floating-point numbers.- The serializers write the canonical spelling: no plus sign, no leading zeros, a single 0 for zero, and a leading minus sign for negative values.
ASCII.Hexadecimal.Serializerwrites lowercase letters, butASCII.Hexadecimal.code(:case:)defaults to uppercase. Every serializer appends to the buffer and leaves its existing contents intact.ASCII.Decimal.serialize(:into:)handles integers up to 128 bits wide, including each type's minimum. ASCII.Decimal.Codercounts the digits of the magnitude and leaves out the sign.ASCII.Digits.Count.exactlypads with zeros between the sign and the digits, andASCII.Digits.Count.atMostnever pads. A value with more digits than the count fails. Every configuration failure is thrown before any byte is appended. WithASCII.Digits.Count.greedy, parsing and then serializing gives the canonical spelling rather than the original text: an explicit plus sign and leading zeros are dropped.
Package Metadata
Repository: swift-atoms/swift-ascii
Default branch: main
README: README.md