Skip to content

Commit c115d8c

Browse files
committed
[LLVM] Add string equality operator
This allows checking strings for equality, e.g. `llvmString == "some swift string literal"` without an explicit string conversion.
1 parent 6db3783 commit c115d8c

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Sources/LLVM/LLVM_Utils.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ extension StaticString {
2929
}
3030
}
3131

32+
public func ==(lhs: llvm.StringRef, rhs: StaticString) -> Bool {
33+
let lhsBuffer = UnsafeBufferPointer<UInt8>(
34+
start: lhs.__bytes_beginUnsafe(),
35+
count: Int(lhs.__bytes_endUnsafe() - lhs.__bytes_beginUnsafe()))
36+
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
37+
if lhsBuffer.count != rhsBuffer.count { return false }
38+
return lhsBuffer.elementsEqual(rhsBuffer, by: ==)
39+
}
40+
}
41+
3242
extension llvm.Twine: ExpressibleByStringLiteral {
3343
public init(stringLiteral value: String) {
3444
self.init(value)

0 commit comments

Comments
 (0)