File tree Expand file tree Collapse file tree
Sources/SwiftMIDIFile/MIDIFileEvent/Events Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -489,9 +489,15 @@ extension MIDIFileEvent.Text.EncodingMode {
489489 let data = Data ( rawStringBytes)
490490
491491 let isAllASCIIPrintable = data. allSatisfy ( { CharacterSet . asciiPrintable. contains ( . init( $0) ) } )
492+ let replacementChar = " \u{FFFD} " // Encoded in UTF-8 as 0xEF 0xBF 0xBD
492493
493- // only allow UTF-8 decoding if necessary
494- let string : String = if !isAllASCIIPrintable, let text = String ( data: data, encoding: . utf8) {
494+ // only allow UTF-8 decoding if necessary and if it does not contain any replacement chars.
495+ // UTF-8 decoding pre-macOS 15 will allow lossy decoding of invalid UTF-8 and use replacement chars
496+ // but we want to avoid that. it's not a problem on macOS 15+.
497+ let string : String = if !isAllASCIIPrintable,
498+ let text = String ( data: data, encoding: . utf8) ,
499+ !text. contains ( replacementChar)
500+ {
495501 text
496502 } else if let text = String ( data: data, encoding: . nonLossyASCII) {
497503 text
You can’t perform that action at this time.
0 commit comments