Skip to content

Commit

Permalink
feat: Str conforms to ExpressibleByStringLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
kkebo committed May 16, 2024
1 parent 0638b6b commit 94ba18f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
24 changes: 17 additions & 7 deletions Sources/Tokenizer/Str.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@ public typealias Str = [Char]
public typealias StrSlice = ArraySlice<Char>
public typealias Char = Unicode.Scalar

extension Str {
static func == (lhs: Self, rhs: String) -> Bool {
guard lhs.count == rhs.count else { return false }
for (l, r) in zip(lhs, rhs.unicodeScalars) {
guard l == r else { return false }
}
return true
extension Str: @retroactive ExpressibleByStringLiteral {
@inlinable public init(stringLiteral value: StringLiteralType) {
self = .init(value.unicodeScalars)
}
}

extension Str: @retroactive ExpressibleByUnicodeScalarLiteral {
@inlinable public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
self = .init(value.unicodeScalars)
}
}

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

extension Str: @retroactive ExpressibleByStringInterpolation {}
8 changes: 4 additions & 4 deletions Sources/Tokenizer/Tokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1270,12 +1270,12 @@ public struct Tokenizer<Sink: ~Copyable & TokenSink>: ~Copyable {
@inline(__always)
private mutating func startsExact(
_ input: inout BufferQueue,
with pattern: consuming some StringProtocol
with pattern: consuming Str
) -> Bool? {
guard !input.buffers.isEmpty else { return nil }
var bufIndex = 0
var i = 0
for pc in pattern.unicodeScalars {
for pc in pattern {
guard bufIndex < input.buffers.count else { return nil }
let buf = input.buffers[bufIndex]
let c = buf[buf.startIndex + i]
Expand All @@ -1296,12 +1296,12 @@ public struct Tokenizer<Sink: ~Copyable & TokenSink>: ~Copyable {
@inline(__always)
private mutating func starts(
_ input: inout BufferQueue,
with pattern: consuming some StringProtocol
with pattern: consuming Str
) -> Bool? {
guard !input.buffers.isEmpty else { return nil }
var bufIndex = 0
var i = 0
for pc in pattern.unicodeScalars {
for pc in pattern {
guard bufIndex < input.buffers.count else { return nil }
let buf = input.buffers[bufIndex]
let c = buf[buf.startIndex + i]
Expand Down

0 comments on commit 94ba18f

Please sign in to comment.