Project

simpleidn

1.06
Low commit activity in last 3 years
A long-lived project that still receives updates
This gem allows easy conversion from punycode ACE strings to unicode UTF-8 strings and vice-versa.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 13.0
~> 3.13
 Project Readme

SimpleIDN

CI Gem Version

This gem converts punycode ACE strings to unicode UTF-8 strings and vice versa. It is pure Ruby and has no runtime dependencies.

The punycode conversion is based on the RFC 3492 C example implementation. It is simplified and does not preserve case. Input is first mapped with the UTS #46 (IDNA Compatible Preprocessing) mapping table. The bundled mapping table is generated from Unicode 17.0.0.

The gem works with Ruby 2.2 and later, JRuby, and TruffleRuby.

Installation

gem install simpleidn

Usage

require 'simpleidn'

SimpleIDN.to_unicode("xn--mllerriis-l8a.com")
# => "møllerriis.com"

SimpleIDN.to_ascii("møllerriis.com")
# => "xn--mllerriis-l8a.com"

Transitional processing

to_ascii and to_unicode accept an optional second argument, transitional (default false). When true, the deviation characters ß, ς, ZWJ, and ZWNJ are mapped as in IDNA2003:

SimpleIDN.to_ascii("faß.de")
# => "xn--fa-hia.de"

SimpleIDN.to_ascii("faß.de", true)
# => "fass.de"

Unicode removed transitional processing from UTS #46 in Unicode 16.0. The argument stays for backward compatibility. Use the default (false) unless you must match legacy IDNA2003 behavior.

Errors

Invalid input raises SimpleIDN::ConversionError, a subclass of RangeError:

SimpleIDN.to_unicode("xn---")
# => SimpleIDN::ConversionError

This includes xn-- labels that decode to an empty or all-ASCII string, as required by UTS #46.

Scope and known issues

  • Case is not preserved.
  • This is not a full UTS #46 / IDNA2008 implementation. The gem maps and converts labels. It does not enforce validity rules such as bidi rules, joiner context rules, or label length limits.

Please report any issues!

Development

Run the tests:

bundle install
bundle exec rspec

The test suite includes the official Unicode IdnaTestV2.txt conformance vectors and the JOSEFSSON test vectors.

To update the UTS #46 mapping table, download the new IdnaMappingTable.txt and IdnaTestV2.txt from unicode.org into tables/ and spec/, then regenerate:

ruby tables/generate_mapping_table.rb tables/IdnaMappingTable.txt > lib/simpleidn/uts46mapping.rb

License

MIT. See LICENCE.