-
Couldn't load subscription status.
- Fork 205
SF-0033: Implement String.Encoding.ianaName and String.Encoding(ianaName:).
#1286
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
base: main
Are you sure you want to change the base?
Changes from all commits
3bc76c0
0ea8aff
7acaa40
5563472
a783db1
7515bf4
c726971
5c9492c
ac42127
c7bdbef
e674fa6
8f84db7
d4ab876
cf9ed49
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,213 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
|
|
||
| // WARNING: DO NOT EDIT THIS FILE DIRECTLY. | ||
| // This is auto-generated by `update-iana-charset-names`. | ||
|
|
||
|
|
||
| extension IANACharset { | ||
| /// IANA Charset `US-ASCII`. | ||
| static let usASCII = IANACharset( | ||
| preferredMIMEName: "US-ASCII", | ||
| name: "US-ASCII", | ||
| aliases: [ | ||
| "iso-ir-6", | ||
| "ANSI_X3.4-1968", | ||
| "ANSI_X3.4-1986", | ||
| "ISO_646.irv:1991", | ||
| "ISO646-US", | ||
| "US-ASCII", | ||
| "us", | ||
| "IBM367", | ||
| "cp367", | ||
| "csASCII", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `ISO-8859-1`. | ||
| static let iso8859_1 = IANACharset( | ||
| preferredMIMEName: "ISO-8859-1", | ||
| name: "ISO_8859-1:1987", | ||
| aliases: [ | ||
| "iso-ir-100", | ||
| "ISO_8859-1", | ||
| "ISO-8859-1", | ||
| "latin1", | ||
| "l1", | ||
| "IBM819", | ||
| "CP819", | ||
| "csISOLatin1", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `ISO-8859-2`. | ||
| static let iso8859_2 = IANACharset( | ||
| preferredMIMEName: "ISO-8859-2", | ||
| name: "ISO_8859-2:1987", | ||
| aliases: [ | ||
| "iso-ir-101", | ||
| "ISO_8859-2", | ||
| "ISO-8859-2", | ||
| "latin2", | ||
| "l2", | ||
| "csISOLatin2", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `Shift_JIS`. | ||
| static let shiftJIS = IANACharset( | ||
| preferredMIMEName: "Shift_JIS", | ||
| name: "Shift_JIS", | ||
| aliases: [ | ||
| "MS_Kanji", | ||
| "csShiftJIS", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `EUC-JP`. | ||
| static let eucJP = IANACharset( | ||
| preferredMIMEName: "EUC-JP", | ||
| name: "Extended_UNIX_Code_Packed_Format_for_Japanese", | ||
| aliases: [ | ||
| "csEUCPkdFmtJapanese", | ||
| "EUC-JP", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `ISO-2022-JP`. | ||
| static let iso2022JP = IANACharset( | ||
| preferredMIMEName: "ISO-2022-JP", | ||
| name: "ISO-2022-JP", | ||
| aliases: [ | ||
| "csISO2022JP", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `UTF-8`. | ||
| static let utf8 = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "UTF-8", | ||
| aliases: [ | ||
| "csUTF8", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `UTF-16BE`. | ||
| static let utf16BE = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "UTF-16BE", | ||
| aliases: [ | ||
| "csUTF16BE", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `UTF-16LE`. | ||
| static let utf16LE = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "UTF-16LE", | ||
| aliases: [ | ||
| "csUTF16LE", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `UTF-16`. | ||
| static let utf16 = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "UTF-16", | ||
| aliases: [ | ||
| "csUTF16", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `UTF-32`. | ||
| static let utf32 = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "UTF-32", | ||
| aliases: [ | ||
| "csUTF32", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `UTF-32BE`. | ||
| static let utf32BE = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "UTF-32BE", | ||
| aliases: [ | ||
| "csUTF32BE", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `UTF-32LE`. | ||
| static let utf32LE = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "UTF-32LE", | ||
| aliases: [ | ||
| "csUTF32LE", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `macintosh`. | ||
| static let macintosh = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "macintosh", | ||
| aliases: [ | ||
| "mac", | ||
| "csMacintosh", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `windows-1250`. | ||
| static let windows1250 = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "windows-1250", | ||
| aliases: [ | ||
| "cswindows1250", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `windows-1251`. | ||
| static let windows1251 = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "windows-1251", | ||
| aliases: [ | ||
| "cswindows1251", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `windows-1252`. | ||
| static let windows1252 = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "windows-1252", | ||
| aliases: [ | ||
| "cswindows1252", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `windows-1253`. | ||
| static let windows1253 = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "windows-1253", | ||
| aliases: [ | ||
| "cswindows1253", | ||
| ] | ||
| ) | ||
|
|
||
| /// IANA Charset `windows-1254`. | ||
| static let windows1254 = IANACharset( | ||
| preferredMIMEName: nil, | ||
| name: "windows-1254", | ||
| aliases: [ | ||
| "cswindows1254", | ||
| ] | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the Swift.org open source project | ||
| // | ||
| // Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
| // Licensed under Apache License v2.0 with Runtime Library Exception | ||
| // | ||
| // See https://swift.org/LICENSE.txt for license information | ||
| // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
|
|
||
| // MARK: - Private extensions for parsing encoding names | ||
|
|
||
| private extension UTF8.CodeUnit { | ||
| func _isASCIICaseInsensitivelyEqual(to other: UTF8.CodeUnit) -> Bool { | ||
| return switch self { | ||
| case other, other._uppercased, other._lowercased: true | ||
| default: false | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private extension String { | ||
| func _isASCIICaseInsensitivelyEqual(to other: String) -> Bool { | ||
| let (myUTF8, otherUTF8) = (self.utf8, other.utf8) | ||
| var (myIndex, otherIndex) = (myUTF8.startIndex, otherUTF8.startIndex) | ||
| while myIndex < myUTF8.endIndex && otherIndex < otherUTF8.endIndex { | ||
| guard myUTF8[myIndex]._isASCIICaseInsensitivelyEqual(to: otherUTF8[otherIndex]) else { | ||
| return false | ||
| } | ||
|
|
||
| myUTF8.formIndex(after: &myIndex) | ||
| otherUTF8.formIndex(after: &otherIndex) | ||
| } | ||
| return myIndex == myUTF8.endIndex && otherIndex == otherUTF8.endIndex | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MARK: - IANA Charset Names | ||
|
|
||
| /// Info about IANA Charset. | ||
| internal struct IANACharset { | ||
| /// Preferred MIME Name | ||
| let preferredMIMEName: String? | ||
|
|
||
| /// The name of this charset | ||
| let name: String | ||
|
|
||
| /// The aliases of this charset | ||
| let aliases: Array<String> | ||
|
|
||
| var representativeName: String { | ||
| return preferredMIMEName ?? name | ||
| } | ||
|
|
||
| init(preferredMIMEName: String?, name: String, aliases: Array<String>) { | ||
| self.preferredMIMEName = preferredMIMEName | ||
| self.name = name | ||
| self.aliases = aliases | ||
| } | ||
|
|
||
| func matches(_ string: String) -> Bool { | ||
| if let preferredMIMEName = self.preferredMIMEName, | ||
| preferredMIMEName._isASCIICaseInsensitivelyEqual(to: string) { | ||
| return true | ||
| } | ||
| if name._isASCIICaseInsensitivelyEqual(to: string) { | ||
| return true | ||
| } | ||
| for alias in aliases { | ||
| if alias._isASCIICaseInsensitivelyEqual(to: string) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // MARK: - `String.Encoding` Names | ||
|
|
||
| extension String.Encoding { | ||
| private var _ianaCharset: IANACharset? { | ||
| switch self { | ||
| case .utf8: .utf8 | ||
| case .ascii: .usASCII | ||
| case .japaneseEUC: .eucJP | ||
| case .isoLatin1: .iso8859_1 | ||
| case .shiftJIS: .shiftJIS | ||
| case .isoLatin2: .iso8859_2 | ||
| case .unicode: .utf16 | ||
| case .windowsCP1251: .windows1251 | ||
| case .windowsCP1252: .windows1252 | ||
| case .windowsCP1253: .windows1253 | ||
| case .windowsCP1254: .windows1254 | ||
| case .windowsCP1250: .windows1250 | ||
| case .iso2022JP: .iso2022JP | ||
| case .macOSRoman: .macintosh | ||
| case .utf16BigEndian: .utf16BE | ||
| case .utf16LittleEndian: .utf16LE | ||
| case .utf32: .utf32 | ||
| case .utf32BigEndian: .utf32BE | ||
| case .utf32LittleEndian: .utf32LE | ||
| default: nil | ||
| } | ||
| } | ||
|
|
||
| /// The name of this encoding that is compatible with the one of the IANA registry "charset". | ||
| @available(FoundationPreview 6.3, *) | ||
| public var ianaName: String? { | ||
| return _ianaCharset?.representativeName | ||
| } | ||
|
|
||
| /// Creates an instance from the name of the IANA registry "charset". | ||
| /// | ||
| /// - Note: The given name is compared to each IANA "charset" name | ||
| /// with ASCII case-insensitive collation | ||
| /// to determine which encoding is suitable. | ||
| @available(FoundationPreview 6.3, *) | ||
| public init?(ianaName charsetName: String) { | ||
| let possibilities: [String.Encoding] = [ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it work if you write and below just do Am I missing anything? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't we lose |
||
| .utf8, | ||
| .ascii, | ||
| .japaneseEUC, | ||
| .isoLatin1, | ||
| .shiftJIS, | ||
| .isoLatin2, | ||
| .unicode, // .utf16 | ||
| .windowsCP1251, | ||
| .windowsCP1252, | ||
| .windowsCP1253, | ||
| .windowsCP1254, | ||
| .windowsCP1250, | ||
| .iso2022JP, | ||
| .macOSRoman, | ||
| .utf16BigEndian, | ||
| .utf16LittleEndian, | ||
| .utf32, | ||
| .utf32BigEndian, | ||
| .utf32LittleEndian, | ||
| ] | ||
|
|
||
| for encoding in possibilities { | ||
| if encoding._ianaCharset!.matches(charsetName) { | ||
| self = encoding | ||
| return | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.