Skip to content

Commit 07f38c4

Browse files
authored
Add compatibility path for invalid UTF8 data (swiftlang#815)
1 parent c82d167 commit 07f38c4

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Sources/FoundationEssentials/String/String+IO.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,17 @@ extension String {
112112
self = string
113113
} else {
114114
// String is not valid UTF8
115+
#if FOUNDATION_FRAMEWORK && os(macOS)
116+
// Allow for invalid UTF8 to be repaired in compatibility cases
117+
if Self.compatibiltity2 {
118+
self = String(decoding: Array(bytes), as: UTF8.self)
119+
} else {
120+
return nil
121+
}
122+
123+
#else
115124
return nil
125+
#endif
116126
}
117127
case .utf16BigEndian, .utf16LittleEndian, .utf16:
118128
// See also the package extension String?(_utf16:), which does something similar to this without the swapping of big/little.

0 commit comments

Comments
 (0)