Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swap String(bytes:encoding:)! for String(decoding:as:) #1731

Merged
merged 1 commit into from
Nov 21, 2024
Merged
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
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal struct JSONEncoder {

internal var stringResult: String {
get {
String(bytes: data, encoding: String.Encoding.utf8)!
String(decoding: data, as: UTF8.self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/Message+JSONAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ extension Message {
return try m.encodedJSONString(options: options)
}
let data: [UInt8] = try jsonUTF8Bytes(options: options)
return String(bytes: data, encoding: .utf8)!
return String(decoding: data, as: UTF8.self)
}

/// Returns a `SwiftProtobufContiguousBytes` containing the UTF-8 JSON serialization of the message.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/Message+JSONArrayAdditions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ extension Message {
options: JSONEncodingOptions = JSONEncodingOptions()
) throws -> String where C.Iterator.Element == Self {
let data: [UInt8] = try jsonUTF8Bytes(from: collection, options: options)
return String(bytes: data, encoding: .utf8)!
return String(decoding: data, as: UTF8.self)
}

/// Returns a `SwiftProtobufContiguousBytes` containing the UTF-8 JSON serialization of the messages.
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftProtobuf/TextFormatEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal struct TextFormatEncoder {
private var indentString: [UInt8] = []
var stringResult: String {
get {
String(bytes: data, encoding: String.Encoding.utf8)!
String(decoding: data, as: UTF8.self)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftProtobufTests/TestHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ extension PBTestHelpers where MessageTestType: SwiftProtobuf.Message & Equatable

do {
let encoded: [UInt8] = try decoded.jsonUTF8Bytes()
let encodedString = String(bytes: encoded, encoding: String.Encoding.utf8)!
let encodedString = String(decoding: encoded, as: UTF8.self)
do {
let redecoded = try MessageTestType(
jsonUTF8Bytes: encoded,
Expand Down