From c115d8cc945e51d0091202a135aecd983e59d03a Mon Sep 17 00:00:00 2001 From: Egor Zhdan Date: Mon, 17 Oct 2022 13:43:41 +0100 Subject: [PATCH] [LLVM] Add string equality operator This allows checking strings for equality, e.g. `llvmString == "some swift string literal"` without an explicit string conversion. --- Sources/LLVM/LLVM_Utils.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/LLVM/LLVM_Utils.swift b/Sources/LLVM/LLVM_Utils.swift index f536319..d479f6c 100644 --- a/Sources/LLVM/LLVM_Utils.swift +++ b/Sources/LLVM/LLVM_Utils.swift @@ -29,6 +29,16 @@ extension StaticString { } } +public func ==(lhs: llvm.StringRef, rhs: StaticString) -> Bool { + let lhsBuffer = UnsafeBufferPointer( + start: lhs.__bytes_beginUnsafe(), + count: Int(lhs.__bytes_endUnsafe() - lhs.__bytes_beginUnsafe())) + return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer) in + if lhsBuffer.count != rhsBuffer.count { return false } + return lhsBuffer.elementsEqual(rhsBuffer, by: ==) + } +} + extension llvm.Twine: ExpressibleByStringLiteral { public init(stringLiteral value: String) { self.init(value)