Skip to content

Commit ac42127

Browse files
committed
Simplify String.init(ianaName:).
1 parent 5c9492c commit ac42127

File tree

1 file changed

+29
-49
lines changed

1 file changed

+29
-49
lines changed

Sources/FoundationEssentials/String/String+Encoding+Names.swift

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -222,58 +222,38 @@ extension String.Encoding {
222222
/// to determine which encoding is suitable.
223223
@available(FoundationPreview 6.3, *)
224224
public init?(ianaName charsetName: String) {
225+
let possibilities: [String.Encoding] = [
226+
.utf8,
227+
.ascii,
228+
.japaneseEUC,
229+
.isoLatin1,
230+
.shiftJIS,
231+
.isoLatin2,
232+
.unicode, // .utf16
233+
.windowsCP1251,
234+
.windowsCP1252,
235+
.windowsCP1253,
236+
.windowsCP1254,
237+
.windowsCP1250,
238+
.iso2022JP,
239+
.macOSRoman,
240+
.utf16BigEndian,
241+
.utf16LittleEndian,
242+
.utf32,
243+
.utf32BigEndian,
244+
.utf32LittleEndian,
245+
]
246+
225247
func __determineEncoding() -> String.Encoding? {
226-
func __matches(_ charsets: IANACharset...) -> Bool {
227-
assert(!charsets.isEmpty)
228-
return charsets.contains {
229-
$0.matches(
230-
charsetName,
231-
tokenizedBy: ASCIICaseInsensitiveTokenizer.self
232-
)
248+
for encoding in possibilities {
249+
guard let ianaCharset = encoding._ianaCharset else {
250+
continue
251+
}
252+
if ianaCharset.matches(charsetName, tokenizedBy: ASCIICaseInsensitiveTokenizer.self) {
253+
return encoding
233254
}
234255
}
235-
236-
return if __matches(.utf8) {
237-
.utf8
238-
} else if __matches(.usASCII) {
239-
.ascii
240-
} else if __matches(.eucJP) {
241-
.japaneseEUC
242-
} else if __matches(.iso8859_1) {
243-
.isoLatin1
244-
} else if __matches(.shiftJIS) {
245-
.shiftJIS
246-
} else if __matches(.iso8859_2) {
247-
.isoLatin2
248-
} else if __matches(.utf16) {
249-
.utf16
250-
} else if __matches(.windows1251) {
251-
.windowsCP1251
252-
} else if __matches(.windows1252) {
253-
.windowsCP1252
254-
} else if __matches(.windows1253) {
255-
.windowsCP1253
256-
} else if __matches(.windows1254) {
257-
.windowsCP1254
258-
} else if __matches(.windows1250) {
259-
.windowsCP1250
260-
} else if __matches(.iso2022JP) {
261-
.iso2022JP
262-
} else if __matches(.macintosh) {
263-
.macOSRoman
264-
} else if __matches(.utf16BE) {
265-
.utf16BigEndian
266-
} else if __matches(.utf16LE) {
267-
.utf16LittleEndian
268-
} else if __matches(.utf32) {
269-
.utf32
270-
} else if __matches(.utf32BE) {
271-
.utf32BigEndian
272-
} else if __matches(.utf32LE) {
273-
.utf32LittleEndian
274-
} else {
275-
nil
276-
}
256+
return nil
277257
}
278258

279259
guard let encoding = __determineEncoding() else {

0 commit comments

Comments
 (0)