-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStringEncoding.swift
72 lines (66 loc) · 2.2 KB
/
StringEncoding.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//
// StringEncoding.swift
// Macro
//
// Created by Helge Hess.
// Copyright © 2020-2023 ZeeZide GmbH. All rights reserved.
//
#if canImport(Foundation)
import Foundation
@usableFromInline
internal let stringEncodingNames : Set<String> = [
"utf8", "utf-8",
"ascii",
"iso2022jp",
"isolatin1", "latin1", "iso-8859-1",
"isolatin2", "latin2", "iso-8859-2",
"japaneseeuc",
"macosroman", "macos",
"nextstep",
"nonlossyascii",
"shiftjis",
"symbol",
"unicode",
"utf-16", "utf16",
"utf-32", "utf32"
// TODO: add the rest
]
public extension String.Encoding {
@inlinable
static func isEncoding(_ name: String) -> Bool {
return stringEncodingNames.contains(name)
}
@inlinable
static func encodingWithName(_ name : String,
fallbackEncoding : String.Encoding = .utf8)
-> String.Encoding
{
let lc = name.lowercased()
switch lc {
case "utf8", "utf-8" : return .utf8
case "ascii" : return .ascii
case "iso2022jp" : return .iso2022JP
case "isolatin1", "latin1", "iso-8859-1" : return .isoLatin1
case "isolatin2", "latin2", "iso-8859-2" : return .isoLatin2
case "japaneseeuc" : return .japaneseEUC
case "macosroman", "macos" : return .macOSRoman
case "nextstep" : return .nextstep
case "nonlossyascii" : return .nonLossyASCII
case "shiftjis" : return .shiftJIS
case "symbol" : return .symbol
case "unicode" : return .unicode
case "utf-16", "utf16" : return .utf16
case "utf-32", "utf32" : return .utf32
// TODO: add the rest
default:
process.emitWarning("Unexpected String encoding: '\(name)'",
name: #function)
return fallbackEncoding
}
}
}
public enum CharsetConversionError: Swift.Error {
case failedToConverData (encoding: String.Encoding)
case failedToConvertString(encoding: String.Encoding)
}
#endif // canImport(Foundation)