diff --git a/Sources/System/PlatformString.swift b/Sources/System/PlatformString.swift index 358f349..b75a2c8 100644 --- a/Sources/System/PlatformString.swift +++ b/Sources/System/PlatformString.swift @@ -24,6 +24,56 @@ extension String { self.init(_errorCorrectingPlatformString: platformString) } + /// Creates a string by interpreting the null-terminated platform string as + /// UTF-8 on Unix and UTF-16 on Windows. + /// + /// - Parameter platformString: The null-terminated platform string to be + /// interpreted as `CInterop.PlatformUnicodeEncoding`. + /// + /// - Note It is a precondition that `platformString` must be null-terminated. + /// The absence of a null byte will trigger a runtime error. + /// + /// If the content of the platform string isn't well-formed Unicode, + /// this initializer replaces invalid bytes with U+FFFD. + /// This means that, depending on the semantics of the specific platform, + /// conversion to a string and back might result in a value that's different + /// from the original platform string. + @inlinable + @_alwaysEmitIntoClient + public init(platformString: [CInterop.PlatformChar]) { + guard let _ = platformString.firstIndex(of: 0) else { + fatalError( + "input of String.init(platformString:) must be null-terminated" + ) + } + self = platformString.withUnsafeBufferPointer { + String(platformString: $0.baseAddress!) + } + } + + @available(*, deprecated, message: "Use String(_ scalar: Unicode.Scalar)") + @inlinable + @_alwaysEmitIntoClient + public init(platformString: inout CInterop.PlatformChar) { + guard platformString == 0 else { + fatalError( + "input of String.init(platformString:) must be null-terminated" + ) + } + self = "" + } + + @available(*, deprecated, message: "Use a copy of the String argument") + @inlinable + @_alwaysEmitIntoClient + public init(platformString: String) { + if let nullLoc = platformString.firstIndex(of: "\0") { + self = String(platformString[..