Skip to content

Wevah/IDNA-Cocoa

Folders and files

NameName
Last commit message
Last commit date

Latest commit

96de66e · Mar 13, 2023
Apr 7, 2022
Nov 30, 2021
Apr 9, 2022
Jul 3, 2020
Apr 18, 2020
May 9, 2020
May 11, 2020
Jul 13, 2022
Apr 7, 2022
Jul 14, 2022
Jul 8, 2020
Feb 7, 2023
Jul 13, 2022
Dec 2, 2020
Jul 13, 2022
Apr 9, 2022
Apr 7, 2022
Jul 13, 2022
Mar 16, 2020
Jul 13, 2022
Mar 13, 2023

Repository files navigation

IDNA Cocoa

v2.0b4 (2021) by Nate Weaver (Wevah)
https://derailer.org/
https://github.com/Wevah/Punycode-Cocoa

Swift

An IDNA String extension and NSString overlay, based on UTS #46. Currently implents the full conversion table and joiner validation.

Use this to convert internationalized domain names (IDN) between Unicode and ASCII.

To use in your own projects, this repository can be imported as a Swift package.

Alternatively, the files can be manually included: Everything in Sources/IDNA:

  • UTS46.swift
  • UTS46+Loading.swift
  • Data+Extensions.swift
  • Scanner+Extensions.swift
  • String+Punycode.swift

If your project needs to call from Objective-C make sure to include NSString+IDNA.swift for the NSString overlays.

(UTS46+Conversion.swift is for importing text files containing UTS #46 mappings and exporting to the binary format used by UTS46+Loading.swift, and isn't necessary if you just want to use the encoding/decoding routines.)

Note that these API currently don't do homograph detection.

Interface

String/NSString

Swift:
var idnaEncoded: String? { get }
Objective-C:
@property (readonly, copy, nullable) NSString *IDNAEncodedString;

If self contains non-ASCII, encodes the string's domain components as Punycode and prepends xn-- to the transformed components.


Swift:
var idnaDecoded: String? { get }
Objective-C:
@property (readonly, copy, nullable) NSString *IDNADecodedString;

Decodes a string returned by idnaEncoded/-IDNAEncodedString.


Swift:
var encodedURLString: String? { get }
var decodedURLString: String? { get }
Objective-C:
@property (readonly, copy, nullable) NSString *encodedURLString;
@property (readonly, copy, nullable) NSString *decodedURLString;

Performs Punycode encode/decode operations on each appropriate part (the domain bits) of an URL string, and URL encodes/decodes the path/query/fragment.


URL/NSURL

Swift:
init?(unicodeString: String)
Objective-C:
+ (nullable instancetype)URLWithUnicodeString:(NSString *)URLString;

Convenience initializer equivalent to URL(string: unicodeString.encodedURLString)/ [NSURL URLWithString:URLString.encodedURLString].


Swift:
var decodedURLString: String? { get }
Objective-C:
@property (readonly, copy, nullable) NSString *decodedURLString;

Convenience property equivalent to someURL.absoluteString.decodedURLString.


Swift:
init?(unicodeString: String, relativeTo url: URL?) {
Objective-C:
+ (nullable instancetype)URLWithUnicodeString:(NSString *)URLString relativeToURL:(nullable NSURL *)baseURL;

Convenience initializer for creating a URL from a Unicode string, relative to another URL.


© 2012–2022 Nate Weaver (Wevah)