From 94ba18f76f1bfe3e82b7199820d6f521ef1a0ce9 Mon Sep 17 00:00:00 2001 From: Kenta Kubo <601636+kkebo@users.noreply.github.com> Date: Tue, 14 May 2024 03:30:59 +0900 Subject: [PATCH] feat: `Str` conforms to `ExpressibleByStringLiteral` --- Sources/Tokenizer/Str.swift | 24 +++++++++++++++++------- Sources/Tokenizer/Tokenizer.swift | 8 ++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Sources/Tokenizer/Str.swift b/Sources/Tokenizer/Str.swift index 25bfd95..38bfb0e 100644 --- a/Sources/Tokenizer/Str.swift +++ b/Sources/Tokenizer/Str.swift @@ -2,12 +2,22 @@ public typealias Str = [Char] public typealias StrSlice = ArraySlice 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 {} diff --git a/Sources/Tokenizer/Tokenizer.swift b/Sources/Tokenizer/Tokenizer.swift index 440fb5e..b7df52e 100644 --- a/Sources/Tokenizer/Tokenizer.swift +++ b/Sources/Tokenizer/Tokenizer.swift @@ -1270,12 +1270,12 @@ public struct Tokenizer: ~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] @@ -1296,12 +1296,12 @@ public struct Tokenizer: ~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]