-
Notifications
You must be signed in to change notification settings - Fork 207
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
Open
YOCKOW
wants to merge
14
commits into
swiftlang:main
Choose a base branch
from
YOCKOW:StringEncodingNames/implementation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3bc76c0
Import implementation for String Encoding Names from other repo.
YOCKOW 0ea8aff
Import tests for String Encoding Names from other repo.
YOCKOW 7acaa40
Remove dead code in terms of the current proposal.
YOCKOW 5563472
Use `Testing` for String Encoding Names tests.
YOCKOW a783db1
NFC: Fix indentation in "String+Encoding+Names.swift".
YOCKOW 7515bf4
SF-0033: Adjust comments/attributes to match the accepted proposal.
YOCKOW c726971
Auto-generate Swift source code for IANA Charset names.
YOCKOW 5c9492c
Remove unnecessary `@inlinable`.
YOCKOW ac42127
Simplify `String.init(ianaName:)`.
YOCKOW c7bdbef
Add new files related to SF-0033 to CMakeLists.txt.
YOCKOW e674fa6
Rewrite script in Swift instead of Python.
YOCKOW 8f84db7
Simplify logic to parse IANA Charset names.
YOCKOW d4ab876
Fix spelling of functions for "case-insensitively".
YOCKOW cf9ed49
Remove redundant nested function in `String.Encoding(ianaName:)`.
YOCKOW File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
213 changes: 213 additions & 0 deletions
213
Sources/FoundationEssentials/String/IANACharsetNames.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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", | ||
| ] | ||
| ) | ||
| } |
155 changes: 155 additions & 0 deletions
155
Sources/FoundationEssentials/String/String+Encoding+Names.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.