Skip to content

Commit 117d402

Browse files
Add extension to String init(contentsOfFile:encoding) (#100)
Add extension to String init(contentsOfFile:encoding)
1 parent 1a8a46b commit 117d402

2 files changed

Lines changed: 18 additions & 0 deletions

File tree

Sources/Extensions.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ public extension Bundle {
4444
/// Extensions on `String` that work with `Path` rather than `String` or `URL`
4545
public extension String {
4646
/// Initializes this `String` with the contents of the provided path.
47+
/// - SeeAlso: `init(contentsOf:encoding:)`
48+
@available(macOS, deprecated: 15, message: "Use `init(contentsOf:encoding:)` instead")
49+
@available(iOS, deprecated: 18, message: "Use `init(contentsOf:encoding:)` instead")
50+
@available(tvOS, deprecated: 18, message: "Use `init(contentsOf:encoding:)` instead")
51+
@available(watchOS, deprecated: 11, message: "Use `init(contentsOf:encoding:)` instead")
4752
@inlinable
4853
init<P: Pathish>(contentsOf path: P) throws {
4954
try self.init(contentsOfFile: path.string)
5055
}
5156

57+
/// Initializes this `String` with the contents of the provided path interpreted using a given encoding.
58+
@inlinable
59+
init<P: Pathish>(contentsOf path: P, encoding: String.Encoding) throws {
60+
try self.init(contentsOfFile: path.string, encoding: encoding)
61+
}
62+
5263
/// - Returns: `to` to allow chaining
5364
@inlinable
5465
@discardableResult

Tests/PathTests/PathTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,13 @@ class PathTests: XCTestCase {
496496
}
497497
}
498498

499+
func testStringEncodingExtensions() throws {
500+
let string = try String(contentsOf: Path(#file)!, encoding: .utf8)
501+
try Path.mktemp { tmpdir in
502+
_ = try string.write(to: tmpdir.foo, encoding: .utf8)
503+
}
504+
}
505+
499506
func testFileHandleExtensions() throws {
500507
_ = try FileHandle(forReadingAt: Path(#file)!)
501508
_ = try FileHandle(forWritingAt: Path(#file)!)

0 commit comments

Comments
 (0)