Skip to content

Commit 87773c4

Browse files
authored
Fix utf8 decode (swiftlang#145)
* remove Error * replace to String(decoding:as) from String(data: encoding)
1 parent 261ad6f commit 87773c4

File tree

2 files changed

+4
-21
lines changed

2 files changed

+4
-21
lines changed

Tools/markdown-tool/Commands/FormatCommand.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,8 @@ extension MarkdownCommand {
138138
}
139139
which.waitUntilExit()
140140

141-
guard which.terminationStatus == 0,
142-
let output = String(data: standardOutput.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) else {
143-
return nil
144-
}
141+
guard which.terminationStatus == 0 else { return nil }
142+
let output = String(decoding: standardOutput.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self)
145143

146144
return output.trimmingCharacters(in: CharacterSet(charactersIn: "\n"))
147145
}

Tools/markdown-tool/MarkdownCommand.swift

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,14 @@ import Markdown
1414

1515
@main
1616
struct MarkdownCommand: ParsableCommand {
17-
enum Error: LocalizedError {
18-
case couldntDecodeInputAsUTF8
19-
20-
var errorDescription: String? {
21-
switch self {
22-
case .couldntDecodeInputAsUTF8:
23-
return "Couldn't decode input as UTF-8"
24-
}
25-
}
26-
}
27-
2817
static let configuration = CommandConfiguration(commandName: "markdown", shouldDisplay: false, subcommands: [
2918
DumpTree.self,
3019
Format.self,
3120
])
3221

3322
static func parseFile(at path: String, options: ParseOptions) throws -> (source: String, parsed: Document) {
3423
let data = try Data(contentsOf: URL(fileURLWithPath: path))
35-
guard let inputString = String(data: data, encoding: .utf8) else {
36-
throw Error.couldntDecodeInputAsUTF8
37-
}
24+
let inputString = String(decoding: data, as: UTF8.self)
3825
return (inputString, Document(parsing: inputString, options: options))
3926
}
4027

@@ -45,9 +32,7 @@ struct MarkdownCommand: ParsableCommand {
4532
} else {
4633
stdinData = FileHandle.standardInput.readDataToEndOfFile()
4734
}
48-
guard let stdinString = String(data: stdinData, encoding: .utf8) else {
49-
throw Error.couldntDecodeInputAsUTF8
50-
}
35+
let stdinString = String(decoding: stdinData, as: UTF8.self)
5136
return (stdinString, Document(parsing: stdinString, options: options))
5237
}
5338
}

0 commit comments

Comments
 (0)