Skip to content

Instantly share code, notes, and snippets.

@karwa
Created May 3, 2020
Embed
What would you like to do?
ascii prototype
public struct ASCII: Equatable, Comparable {
public var codePoint: UInt8
@inlinable public init(_ v: UInt8) { self.codePoint = v }
}
extension ASCII {
// Homogenous comparisons.
@inlinable public static func < (lhs: ASCII, rhs: ASCII) -> Bool {
lhs.codePoint < rhs.codePoint
}
// Heterogeneous equality.
// - UInt8.
@inlinable public static func == (lhs: UInt8, rhs: ASCII) -> Bool {
lhs == rhs.codePoint
}
@inlinable public static func != (lhs: UInt8, rhs: ASCII) -> Bool {
!(lhs == rhs)
}
@inlinable public static func ~= (pattern: UInt8, value: ASCII) -> Bool {
pattern == value.codePoint
}
@inlinable public static func ~= (pattern: ASCII, value: UInt8) -> Bool {
value == pattern.codePoint
}
// - Character.
@inlinable public static func == (lhs: Character, rhs: ASCII) -> Bool {
lhs.asciiValue.map { $0 == rhs } ?? false
}
@inlinable public static func != (lhs: Character, rhs: ASCII) -> Bool {
!(lhs == rhs)
}
@inlinable public static func ~= (pattern: Character, value: ASCII) -> Bool {
pattern == value
}
@inlinable public static func ~= (pattern: ASCII, value: Character) -> Bool {
value == pattern
}
// - Character?
@inlinable public static func == (lhs: Character?, rhs: ASCII) -> Bool {
lhs?.asciiValue.map { $0 == rhs } ?? false
}
@inlinable public static func != (lhs: Character?, rhs: ASCII) -> Bool {
!(lhs == rhs)
}
@inlinable public static func ~= (pattern: Character?, value: ASCII) -> Bool {
pattern == value
}
@inlinable public static func ~= (pattern: ASCII, value: Character?) -> Bool {
value == pattern
}
}
extension Character {
@inlinable public init(_ ascii: ASCII) {
self.init(Unicode.Scalar(ascii.codePoint))
}
}
public extension ASCII {
// Control Characters.
@inlinable static var null : ASCII { ASCII(0x00) }
@inlinable static var startOfHeading : ASCII { ASCII(0x01) }
@inlinable static var startOfText : ASCII { ASCII(0x02) }
@inlinable static var endOfText : ASCII { ASCII(0x03) }
@inlinable static var endOfTransmission : ASCII { ASCII(0x04) }
@inlinable static var enquiry : ASCII { ASCII(0x05) }
@inlinable static var acknowledge : ASCII { ASCII(0x06) }
@inlinable static var bell : ASCII { ASCII(0x07) }
@inlinable static var backspace : ASCII { ASCII(0x08) }
@inlinable static var horizontalTab : ASCII { ASCII(0x09) }
@inlinable static var lineFeed : ASCII { ASCII(0x0A) }
@inlinable static var verticalTab : ASCII { ASCII(0x0B) }
@inlinable static var formFeed : ASCII { ASCII(0x0C) }
@inlinable static var carriageReturn : ASCII { ASCII(0x0D) }
@inlinable static var shiftOut : ASCII { ASCII(0x0E) }
@inlinable static var shiftIn : ASCII { ASCII(0x0F) }
@inlinable static var dataLinkEscape : ASCII { ASCII(0x10) }
@inlinable static var deviceControl1 : ASCII { ASCII(0x11) }
@inlinable static var deviceControl2 : ASCII { ASCII(0x12) }
@inlinable static var deviceControl3 : ASCII { ASCII(0x13) }
@inlinable static var deviceControl4 : ASCII { ASCII(0x14) }
@inlinable static var negativeAcknowledge : ASCII { ASCII(0x15) }
@inlinable static var synchronousIdle : ASCII { ASCII(0x16) }
@inlinable static var endOfTransmissionBlock: ASCII { ASCII(0x17) }
@inlinable static var cancel : ASCII { ASCII(0x18) }
@inlinable static var endOfMedium : ASCII { ASCII(0x19) }
@inlinable static var substitute : ASCII { ASCII(0x1A) }
@inlinable static var escape : ASCII { ASCII(0x1B) }
@inlinable static var fileSeparator : ASCII { ASCII(0x1C) }
@inlinable static var groupSeparator : ASCII { ASCII(0x1D) }
@inlinable static var recordSeparator : ASCII { ASCII(0x1E) }
@inlinable static var unitSeparator : ASCII { ASCII(0x1F) }
// Special Characters.
@inlinable static var space : ASCII { ASCII(0x20) }
@inlinable static var exclamationMark : ASCII { ASCII(0x21) }
@inlinable static var doubleQuotationMark: ASCII { ASCII(0x22) }
@inlinable static var numberSign : ASCII { ASCII(0x23) }
@inlinable static var dollarSign : ASCII { ASCII(0x24) }
@inlinable static var percentSign : ASCII { ASCII(0x25) }
@inlinable static var ampersand : ASCII { ASCII(0x26) }
@inlinable static var apostrophe : ASCII { ASCII(0x27) }
@inlinable static var leftParenthesis : ASCII { ASCII(0x28) }
@inlinable static var rightParenthesis : ASCII { ASCII(0x29) }
@inlinable static var asterisk : ASCII { ASCII(0x2A) }
@inlinable static var plus : ASCII { ASCII(0x2B) }
@inlinable static var comma : ASCII { ASCII(0x2C) }
@inlinable static var minus : ASCII { ASCII(0x2D) }
@inlinable static var period : ASCII { ASCII(0x2E) }
@inlinable static var forwardSlash : ASCII { ASCII(0x2F) }
// Numbers.
@inlinable static var n0: ASCII { ASCII(0x30) }
@inlinable static var n1: ASCII { ASCII(0x31) }
@inlinable static var n2: ASCII { ASCII(0x32) }
@inlinable static var n3: ASCII { ASCII(0x33) }
@inlinable static var n4: ASCII { ASCII(0x34) }
@inlinable static var n5: ASCII { ASCII(0x35) }
@inlinable static var n6: ASCII { ASCII(0x36) }
@inlinable static var n7: ASCII { ASCII(0x37) }
@inlinable static var n8: ASCII { ASCII(0x38) }
@inlinable static var n9: ASCII { ASCII(0x39) }
// Some punctuation.
@inlinable static var colon : ASCII { ASCII(0x3A) }
@inlinable static var semicolon : ASCII { ASCII(0x3B) }
@inlinable static var lessThanSign : ASCII { ASCII(0x3C) }
@inlinable static var equalSign : ASCII { ASCII(0x3D) }
@inlinable static var greaterThanSign: ASCII { ASCII(0x3E) }
@inlinable static var questionMark : ASCII { ASCII(0x3F) }
@inlinable static var commercialAt : ASCII { ASCII(0x40) }
// Upper-case letters.
@inlinable static var A: ASCII { ASCII(0x41) }
@inlinable static var B: ASCII { ASCII(0x42) }
@inlinable static var C: ASCII { ASCII(0x43) }
@inlinable static var D: ASCII { ASCII(0x44) }
@inlinable static var E: ASCII { ASCII(0x45) }
@inlinable static var F: ASCII { ASCII(0x46) }
@inlinable static var G: ASCII { ASCII(0x47) }
@inlinable static var H: ASCII { ASCII(0x48) }
@inlinable static var I: ASCII { ASCII(0x49) }
@inlinable static var J: ASCII { ASCII(0x4A) }
@inlinable static var K: ASCII { ASCII(0x4B) }
@inlinable static var L: ASCII { ASCII(0x4C) }
@inlinable static var M: ASCII { ASCII(0x4D) }
@inlinable static var N: ASCII { ASCII(0x4E) }
@inlinable static var O: ASCII { ASCII(0x4F) }
@inlinable static var P: ASCII { ASCII(0x50) }
@inlinable static var Q: ASCII { ASCII(0x51) }
@inlinable static var R: ASCII { ASCII(0x52) }
@inlinable static var S: ASCII { ASCII(0x53) }
@inlinable static var T: ASCII { ASCII(0x54) }
@inlinable static var U: ASCII { ASCII(0x55) }
@inlinable static var V: ASCII { ASCII(0x56) }
@inlinable static var W: ASCII { ASCII(0x57) }
@inlinable static var X: ASCII { ASCII(0x58) }
@inlinable static var Y: ASCII { ASCII(0x59) }
@inlinable static var Z: ASCII { ASCII(0x5A) }
// More special characters.
@inlinable static var leftSquareBracket : ASCII { ASCII(0x5B) }
@inlinable static var backslash : ASCII { ASCII(0x5C) }
@inlinable static var rightSquareBracket: ASCII { ASCII(0x5D) }
@inlinable static var circumflexAccent : ASCII { ASCII(0x5E) }
@inlinable static var underscore : ASCII { ASCII(0x5F) }
@inlinable static var backtick : ASCII { ASCII(0x60) }
// Lower-case letters.
@inlinable static var a: ASCII { ASCII(0x61) }
@inlinable static var b: ASCII { ASCII(0x62) }
@inlinable static var c: ASCII { ASCII(0x63) }
@inlinable static var d: ASCII { ASCII(0x64) }
@inlinable static var e: ASCII { ASCII(0x65) }
@inlinable static var f: ASCII { ASCII(0x66) }
@inlinable static var g: ASCII { ASCII(0x67) }
@inlinable static var h: ASCII { ASCII(0x68) }
@inlinable static var i: ASCII { ASCII(0x69) }
@inlinable static var j: ASCII { ASCII(0x6A) }
@inlinable static var k: ASCII { ASCII(0x6B) }
@inlinable static var l: ASCII { ASCII(0x6C) }
@inlinable static var m: ASCII { ASCII(0x6D) }
@inlinable static var n: ASCII { ASCII(0x6E) }
@inlinable static var o: ASCII { ASCII(0x6F) }
@inlinable static var p: ASCII { ASCII(0x70) }
@inlinable static var q: ASCII { ASCII(0x71) }
@inlinable static var r: ASCII { ASCII(0x72) }
@inlinable static var s: ASCII { ASCII(0x73) }
@inlinable static var t: ASCII { ASCII(0x74) }
@inlinable static var u: ASCII { ASCII(0x75) }
@inlinable static var v: ASCII { ASCII(0x76) }
@inlinable static var w: ASCII { ASCII(0x77) }
@inlinable static var x: ASCII { ASCII(0x78) }
@inlinable static var y: ASCII { ASCII(0x79) }
@inlinable static var z: ASCII { ASCII(0x7A) }
// More special characters.
@inlinable static var leftCurlyBracket : ASCII { ASCII(0x7B) }
@inlinable static var verticalBar : ASCII { ASCII(0x7C) }
@inlinable static var rightCurlyBracket: ASCII { ASCII(0x7D) }
@inlinable static var tilde : ASCII { ASCII(0x7E) }
@inlinable static var delete : ASCII { ASCII(0x7F) }
}
extension ASCII {
struct Ranges {
var controlCharacters: Range<ASCII> { ASCII(0x00)..<ASCII(0x20) }
var specialCharacters: Range<ASCII> { ASCII(0x20)..<ASCII(0x30) }
var digits : Range<ASCII> { ASCII(0x30)..<ASCII(0x3A) }
var uppercaseAlpha : Range<ASCII> { ASCII(0x41)..<ASCII(0x5B) }
var lowercaseAlpha : Range<ASCII> { ASCII(0x61)..<ASCII(0x7B) }
func isAlpha(_ char: Character) -> Bool {
uppercaseAlpha.contains(char) || lowercaseAlpha.contains(char)
}
func isAlphaNumeric(_ char: Character) -> Bool {
isAlpha(char) || digits.contains(char)
}
}
static var ranges: Ranges { Ranges() }
}
extension Range where Bound == ASCII {
public func contains(_ char: Character) -> Bool {
char.asciiValue.map { self.contains(ASCII($0)) } ?? false
}
}
extension ClosedRange where Bound == ASCII {
public func contains(_ char: Character) -> Bool {
char.asciiValue.map { self.contains(ASCII($0)) } ?? false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment