Skip to content

[cxx-interop] Add operators for comparing and concatenating std::strings #64579

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions stdlib/public/Cxx/std/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,40 @@ extension std.u16string: ExpressibleByStringLiteral {
}
}

// MARK: Concatenating and comparing C++ strings

extension std.string: Equatable {
public static func ==(lhs: std.string, rhs: std.string) -> Bool {
return lhs.compare(rhs) == 0
}

public static func +=(lhs: inout std.string, rhs: std.string) {
lhs.__appendUnsafe(rhs) // ignore the returned pointer
}

public static func +(lhs: std.string, rhs: std.string) -> std.string {
var copy = lhs
copy += rhs
return copy
}
}

extension std.u16string: Equatable {
public static func ==(lhs: std.u16string, rhs: std.u16string) -> Bool {
return lhs.compare(rhs) == 0
}

public static func +=(lhs: inout std.u16string, rhs: std.u16string) {
lhs.__appendUnsafe(rhs) // ignore the returned pointer
}

public static func +(lhs: std.u16string, rhs: std.u16string) -> std.u16string {
var copy = lhs
copy += rhs
return copy
}
}

// MARK: Getting a Swift description of a C++ string

extension std.string: CustomDebugStringConvertible {
Expand Down
40 changes: 40 additions & 0 deletions test/Interop/Cxx/stdlib/overlay/std-string-overlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,46 @@ StdStringOverlayTestSuite.test("std::string <=> Swift.String") {
expectEqual(swift7, "���")
}

StdStringOverlayTestSuite.test("std::string operators") {
var s1 = std.string("something")
let s2 = std.string("123")
let sum = s1 + s2
expectEqual(sum, std.string("something123"))

expectFalse(s1 == s2)
let s3 = std.string("something123")
expectFalse(s1 == s3)
expectFalse(s2 == s3)

s1 += s2
expectTrue(s1 == std.string("something123"))
expectTrue(s1 == s3)

// Make sure the operators work together with ExpressibleByStringLiteral conformance.
s1 += "literal"
expectTrue(s1 == "something123literal")
}

StdStringOverlayTestSuite.test("std::u16string operators") {
var s1 = std.u16string("something")
let s2 = std.u16string("123")
let sum = s1 + s2
expectEqual(sum, std.u16string("something123"))

expectFalse(s1 == s2)
let s3 = std.u16string("something123")
expectFalse(s1 == s3)
expectFalse(s2 == s3)

s1 += s2
expectTrue(s1 == std.u16string("something123"))
expectTrue(s1 == s3)

// Make sure the operators work together with ExpressibleByStringLiteral conformance.
s1 += "literal"
expectTrue(s1 == "something123literal")
}

StdStringOverlayTestSuite.test("std::u16string <=> Swift.String") {
let cxx1 = std.u16string()
let swift1 = String(cxx1)
Expand Down