Skip to content

Commit

Permalink
Fix utf8 decode (#145)
Browse files Browse the repository at this point in the history
* remove Error

* replace to String(decoding:as) from String(data: encoding)
  • Loading branch information
zunda-pixel authored Sep 26, 2023
1 parent 261ad6f commit 87773c4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 21 deletions.
6 changes: 2 additions & 4 deletions Tools/markdown-tool/Commands/FormatCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
Expand Down
19 changes: 2 additions & 17 deletions Tools/markdown-tool/MarkdownCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,14 @@ 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,
])

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))
}

Expand All @@ -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))
}
}

0 comments on commit 87773c4

Please sign in to comment.