From 87773c40383ae7dfe2f62014321bfa0378503c33 Mon Sep 17 00:00:00 2001 From: zunda <47569369+zunda-pixel@users.noreply.github.com> Date: Wed, 27 Sep 2023 02:24:49 +0900 Subject: [PATCH] Fix utf8 decode (#145) * remove Error * replace to String(decoding:as) from String(data: encoding) --- .../Commands/FormatCommand.swift | 6 ++---- Tools/markdown-tool/MarkdownCommand.swift | 19 ++----------------- 2 files changed, 4 insertions(+), 21 deletions(-) diff --git a/Tools/markdown-tool/Commands/FormatCommand.swift b/Tools/markdown-tool/Commands/FormatCommand.swift index 2910b47d..4f81c791 100644 --- a/Tools/markdown-tool/Commands/FormatCommand.swift +++ b/Tools/markdown-tool/Commands/FormatCommand.swift @@ -138,10 +138,8 @@ extension MarkdownCommand { } which.waitUntilExit() - guard which.terminationStatus == 0, - let output = String(data: standardOutput.fileHandleForReading.readDataToEndOfFile(), encoding: .utf8) else { - return nil - } + guard which.terminationStatus == 0 else { return nil } + let output = String(decoding: standardOutput.fileHandleForReading.readDataToEndOfFile(), as: UTF8.self) return output.trimmingCharacters(in: CharacterSet(charactersIn: "\n")) } diff --git a/Tools/markdown-tool/MarkdownCommand.swift b/Tools/markdown-tool/MarkdownCommand.swift index 45aecba8..a1e5d4ff 100644 --- a/Tools/markdown-tool/MarkdownCommand.swift +++ b/Tools/markdown-tool/MarkdownCommand.swift @@ -14,17 +14,6 @@ import Markdown @main struct MarkdownCommand: ParsableCommand { - enum Error: LocalizedError { - case couldntDecodeInputAsUTF8 - - var errorDescription: String? { - switch self { - case .couldntDecodeInputAsUTF8: - return "Couldn't decode input as UTF-8" - } - } - } - static let configuration = CommandConfiguration(commandName: "markdown", shouldDisplay: false, subcommands: [ DumpTree.self, Format.self, @@ -32,9 +21,7 @@ struct MarkdownCommand: ParsableCommand { static func parseFile(at path: String, options: ParseOptions) throws -> (source: String, parsed: Document) { let data = try Data(contentsOf: URL(fileURLWithPath: path)) - guard let inputString = String(data: data, encoding: .utf8) else { - throw Error.couldntDecodeInputAsUTF8 - } + let inputString = String(decoding: data, as: UTF8.self) return (inputString, Document(parsing: inputString, options: options)) } @@ -45,9 +32,7 @@ struct MarkdownCommand: ParsableCommand { } else { stdinData = FileHandle.standardInput.readDataToEndOfFile() } - guard let stdinString = String(data: stdinData, encoding: .utf8) else { - throw Error.couldntDecodeInputAsUTF8 - } + let stdinString = String(decoding: stdinData, as: UTF8.self) return (stdinString, Document(parsing: stdinString, options: options)) } }