Puny

public class Puny

Puny class provides methods to encode and decode strings using Punycode (RFC 3492). It allows for the conversion of Unicode strings into ASCII-compatible encoding, which is essential for domain names and other applications that require ASCII representation.

Note

This implementation follows the specifications outlined in RFC 3492.
  • Usage: Create an instance of Puny and call the appropriate methods for encoding or decoding.
  • Decodes a punycode encoded string to its original representation.

    Declaration

    Swift

    public func decodePunycode(_ punycode: Substring) -> String?

    Parameters

    punycode

    A substring containing the punycode encoding (RFC 3492).

    Return Value

    The decoded original string or nil if the input cannot be decoded due to invalid formatting.

  • Encodes a substring to punycode (RFC 3492).

    Declaration

    Swift

    public func encodePunycode(_ input: Substring) -> String?

    Parameters

    input

    A substring to be encoded in punycode.

    Return Value

    A punycode encoded string or nil if the input contains invalid characters.

  • Returns new string containing IDNA-encoded hostname.

    Declaration

    Swift

    public func encodeIDNA(_ input: Substring) -> String?

    Parameters

    input

    The Substring to be encoded.

    Return Value

    An IDNA encoded hostname or nil if the string can’t be encoded.

  • Returns a new string containing the hostname decoded from IDNA representation.

    Declaration

    Swift

    public func decodedIDNA(_ input: Substring) -> String?

    Parameters

    input

    The Substring to be decoded.

    Return Value

    The original hostname or nil if the string doesn’t contain correct encoding.