Skip to content
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sources/FoundationEssentials/String/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
target_sources(FoundationEssentials PRIVATE
BidirectionalCollection.swift
BuiltInUnicodeScalarSet.swift
IANACharsetNames.swift
RegexPatternCache.swift
String+Bridging.swift
String+Comparison.swift
String+Encoding.swift
String+Encoding+Names.swift
String+EndianAdaptorSequence.swift
String+Essentials.swift
String+IO.swift
Expand Down
213 changes: 213 additions & 0 deletions Sources/FoundationEssentials/String/IANACharsetNames.swift
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",
]
)
}
164 changes: 164 additions & 0 deletions Sources/FoundationEssentials/String/String+Encoding+Names.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
//===----------------------------------------------------------------------===//
//
// 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] = [
Copy link
Contributor

@itingliu itingliu Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work if you write

let possibilities: [IANACharset] = {...

and below just do

         func __determineEncoding() -> String.Encoding? {
            for ianaCharset in possibilities {
                if ianaCharset.matches(charsetName) {
                    return encoding
                }
            }
            return nil
        }

Am I missing anything?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't we lose encoding for return encoding in that implementation?

.utf8,
.ascii,
.japaneseEUC,
.isoLatin1,
.shiftJIS,
.isoLatin2,
.unicode, // .utf16
.windowsCP1251,
.windowsCP1252,
.windowsCP1253,
.windowsCP1254,
.windowsCP1250,
.iso2022JP,
.macOSRoman,
.utf16BigEndian,
.utf16LittleEndian,
.utf32,
.utf32BigEndian,
.utf32LittleEndian,
]

func __determineEncoding() -> String.Encoding? {
for encoding in possibilities {
guard let ianaCharset = encoding._ianaCharset else {
continue
}
if ianaCharset.matches(charsetName) {
return encoding
}
}
return nil
}

guard let encoding = __determineEncoding() else {
Copy link
Contributor

@itingliu itingliu Oct 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And to simply this even further, it looks like this logic inside func __determineEncoding() doesn't even needed to be extracted in a function since it's only used once?

Copy link
Member Author

@YOCKOW YOCKOW Oct 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why nested function is used is just because of readability since self = nil; return is invalid in Swift.

Sorry, we can write self = encoding; return...
I'm fixing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed String.Encoding(ianaName:).
I hope that this is in line with your intention.

return nil
}
self = encoding
}
}

Loading