Files
swift-mirror/stdlib/public/Cxx/std/String.swift
Egor Zhdan dc8ff65195 [cxx-interop] Do not crash for non-ASCII strings
This fixes a crash that happened when creating an instance of `std::string` from a `Swift.String` that contains non-ASCII characters.

When converted to a UTF-8 array, such characters might be represented by numbers that fall out of `CChar`'s boundaries if they have a sign bit set to 1. This is normal and shouldn't trigger a crash or an assertion failure.
2022-09-28 18:30:20 +01:00

34 lines
964 B
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
extension std.string {
public init(_ string: String) {
self.init()
for char in string.utf8 {
self.push_back(value_type(bitPattern: char))
}
}
}
extension std.string: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self.init(value)
}
}
extension String {
public init(cxxString: std.string) {
self.init(cString: cxxString.__c_strUnsafe())
withExtendedLifetime(cxxString) {}
}
}