Skip to content

Commit

Permalink
feat!: define Str as a wrapper type
Browse files Browse the repository at this point in the history
  • Loading branch information
kkebo committed Oct 12, 2024
1 parent 781cb71 commit da4c98f
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 11 deletions.
97 changes: 90 additions & 7 deletions Sources/Str/Str.swift
Original file line number Diff line number Diff line change
@@ -1,30 +1,113 @@
public typealias Str = [Char]
public typealias StrSlice = ArraySlice<Char>
public typealias Char = Unicode.Scalar

extension Str: @retroactive ExpressibleByStringLiteral {
public struct Str: Hashable, Sendable {
@usableFromInline
var storage: ContiguousArray<Char>

@inlinable
public init() {
self.storage = []
}

@inlinable
public init(_ char: Char) {
self.storage = [char]
}

@inlinable
public init(_ chars: ContiguousArray<Char>) {
self.storage = chars
}

@inlinable
public init(_ sequence: some Sequence<Self.Element>) {
self.storage = .init(sequence)
}

@inlinable
public mutating func removeAll(keepingCapacity keepCapacity: Bool = false) {
self.storage.removeAll(keepingCapacity: keepCapacity)
}

@inlinable
public mutating func append(_ newElement: Self.Element) {
self.storage.append(newElement)
}

@inlinable
public static func += (lhs: inout Self, rhs: Self) {
lhs.storage += rhs.storage
}

@inlinable
public static func += (lhs: inout Self, rhs: some Sequence<Self.Element>) {
lhs.storage += rhs
}
}

extension Str: Sequence {
public typealias Iterator = ContiguousArray<Char>.Iterator
public typealias Element = Char

@inlinable
public func makeIterator() -> Self.Iterator { self.storage.makeIterator() }
}

extension Str: BidirectionalCollection {
public typealias Index = ContiguousArray<Char>.Index
public typealias Indices = ContiguousArray<Char>.Indices
public typealias SubSequence = ContiguousArray<Char>.SubSequence

@inlinable
public var startIndex: Self.Index { self.storage.startIndex }
@inlinable
public var endIndex: Self.Index { self.storage.endIndex }
@inlinable
public var indices: Self.Indices { self.storage.indices }
@inlinable
public func index(before i: Self.Index) -> Self.Index { self.storage.index(before: i) }
@inlinable
public func index(after i: Self.Index) -> Self.Index { self.storage.index(after: i) }

@inlinable
public var count: Int { self.storage.count }
@inlinable
public var isEmpty: Bool { self.storage.isEmpty }

@inlinable
public subscript(position: Self.Index) -> Self.Element {
self.storage[position]
}
@inlinable
public subscript(bounds: Range<Self.Index>) -> Self.SubSequence {
self.storage[bounds]
}
}

extension Str: ExpressibleByStringLiteral {
@inlinable public init(stringLiteral value: consuming String) {
guard !value.isEmpty else {
self = []
self.init()
return
}
self.init(value.unicodeScalars)
}
}

extension Str: @retroactive ExpressibleByUnicodeScalarLiteral {
extension Str: ExpressibleByUnicodeScalarLiteral {
@inlinable public init(unicodeScalarLiteral value: consuming Unicode.Scalar) {
self = [value]
self.storage = [value]
}
}

extension Str: @retroactive ExpressibleByExtendedGraphemeClusterLiteral {
extension Str: ExpressibleByExtendedGraphemeClusterLiteral {
@inlinable public init(extendedGraphemeClusterLiteral value: consuming Character) {
self.init(value.unicodeScalars)
}
}

extension Str: @retroactive ExpressibleByStringInterpolation {}
extension Str: ExpressibleByStringInterpolation {}

extension StrSlice: @retroactive ExpressibleByStringLiteral {
@inlinable public init(stringLiteral value: consuming String) {
Expand Down
8 changes: 4 additions & 4 deletions Sources/Tokenizer/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1486,22 +1486,22 @@ public struct Tokenizer<Sink: ~Copyable & TokenSink>: ~Copyable {

@inline(__always)
private mutating func createDOCTYPE(with c: consuming Char) {
self.currentDOCTYPE = .init(name: [c])
self.currentDOCTYPE = .init(name: .init(c))
}

@inline(__always)
private mutating func appendDOCTYPEName(_ c: consuming Char) {
switch self.currentDOCTYPE.name {
case .some: self.currentDOCTYPE.name?.append(c)
case .none: self.currentDOCTYPE.name = [c]
case .none: self.currentDOCTYPE.name = .init(c)
}
}

@inline(__always)
private mutating func appendPublicID(_ c: consuming Char) {
switch self.currentDOCTYPE.publicID {
case .some: self.currentDOCTYPE.publicID?.append(c)
case .none: self.currentDOCTYPE.publicID = [c]
case .none: self.currentDOCTYPE.publicID = .init(c)
}
}

Expand All @@ -1517,7 +1517,7 @@ public struct Tokenizer<Sink: ~Copyable & TokenSink>: ~Copyable {
private mutating func appendSystemID(_ c: consuming Char) {
switch self.currentDOCTYPE.systemID {
case .some: self.currentDOCTYPE.systemID?.append(c)
case .none: self.currentDOCTYPE.systemID = [c]
case .none: self.currentDOCTYPE.systemID = .init(c)
}
}

Expand Down

0 comments on commit da4c98f

Please sign in to comment.