nostrdb: add initial swift integration
This commit is contained in:
35
nostrdb/AsciiCharacter.swift
Normal file
35
nostrdb/AsciiCharacter.swift
Normal file
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// AsciiCharacter.swift
|
||||
// damus
|
||||
//
|
||||
// Created by William Casarin on 2023-07-21.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct AsciiCharacter: ExpressibleByStringLiteral {
|
||||
private let value: UInt8
|
||||
|
||||
var cchar: CChar {
|
||||
return CChar(bitPattern: value)
|
||||
}
|
||||
|
||||
init?(_ character: Character) {
|
||||
guard let asciiValue = character.asciiValue, asciiValue < 128 else {
|
||||
return nil
|
||||
}
|
||||
self.value = asciiValue
|
||||
}
|
||||
|
||||
// MARK: - ExpressibleByStringLiteral conformance
|
||||
init(stringLiteral value: StringLiteralType) {
|
||||
guard value.count == 1, let character = value.first, let ascii = AsciiCharacter(character) else {
|
||||
fatalError("Invalid ASCII character initialization.")
|
||||
}
|
||||
self = ascii
|
||||
}
|
||||
|
||||
var character: Character {
|
||||
return Character(UnicodeScalar(value))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user