Skip to content
Open
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
11 changes: 11 additions & 0 deletions Sources/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<P: Pathish>(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<P: Pathish>(contentsOf path: P, encoding: String.Encoding) throws {
try self.init(contentsOfFile: path.string, encoding: encoding)
}

/// - Returns: `to` to allow chaining
@inlinable
@discardableResult
Expand Down
7 changes: 7 additions & 0 deletions Tests/PathTests/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)!)
Expand Down