diff --git a/Sources/Extensions.swift b/Sources/Extensions.swift index 27f84b8..c0e05f2 100644 --- a/Sources/Extensions.swift +++ b/Sources/Extensions.swift @@ -44,11 +44,22 @@ public extension Bundle { /// Extensions on `String` that work with `Path` rather than `String` or `URL` public extension String { /// Initializes this `String` with the contents of the provided path. + /// - SeeAlso: `init(contentsOf:encoding:)` + @available(macOS, deprecated: 15, message: "Use `init(contentsOf:encoding:)` instead") + @available(iOS, deprecated: 18, message: "Use `init(contentsOf:encoding:)` instead") + @available(tvOS, deprecated: 18, message: "Use `init(contentsOf:encoding:)` instead") + @available(watchOS, deprecated: 11, message: "Use `init(contentsOf:encoding:)` instead") @inlinable init(contentsOf path: P) throws { try self.init(contentsOfFile: path.string) } + /// Initializes this `String` with the contents of the provided path interpreted using a given encoding. + @inlinable + init(contentsOf path: P, encoding: String.Encoding) throws { + try self.init(contentsOfFile: path.string, encoding: encoding) + } + /// - Returns: `to` to allow chaining @inlinable @discardableResult diff --git a/Tests/PathTests/PathTests.swift b/Tests/PathTests/PathTests.swift index 65a5ada..5e0f7ab 100644 --- a/Tests/PathTests/PathTests.swift +++ b/Tests/PathTests/PathTests.swift @@ -496,6 +496,13 @@ class PathTests: XCTestCase { } } + func testStringEncodingExtensions() throws { + let string = try String(contentsOf: Path(#file)!, encoding: .utf8) + try Path.mktemp { tmpdir in + _ = try string.write(to: tmpdir.foo, encoding: .utf8) + } + } + func testFileHandleExtensions() throws { _ = try FileHandle(forReadingAt: Path(#file)!) _ = try FileHandle(forWritingAt: Path(#file)!)