1
// Copyright 2012 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
5
// Package idna implements IDNA2008 (Internationalized Domain Names for
6
// Applications), defined in RFC 5890, RFC 5891, RFC 5892, RFC 5893 and
15
// TODO(nigeltao): specify when errors occur. For example, is ToASCII(".") or
16
// ToASCII("foo\x00") an error? See also http://www.unicode.org/faq/idn.html#11
18
// acePrefix is the ASCII Compatible Encoding prefix.
19
const acePrefix = "xn--"
21
// ToASCII converts a domain or domain label to its ASCII form. For example,
22
// ToASCII("bücher.example.com") is "xn--bcher-kva.example.com", and
23
// ToASCII("golang") is "golang".
24
func ToASCII(s string) (string, error) {
28
labels := strings.Split(s, ".")
29
for i, label := range labels {
31
a, err := encode(acePrefix, label)
38
return strings.Join(labels, "."), nil
41
// ToUnicode converts a domain or domain label to its Unicode form. For example,
42
// ToUnicode("xn--bcher-kva.example.com") is "bücher.example.com", and
43
// ToUnicode("golang") is "golang".
44
func ToUnicode(s string) (string, error) {
45
if !strings.Contains(s, acePrefix) {
48
labels := strings.Split(s, ".")
49
for i, label := range labels {
50
if strings.HasPrefix(label, acePrefix) {
51
u, err := decode(label[len(acePrefix):])
58
return strings.Join(labels, "."), nil
61
func ascii(s string) bool {
62
for i := 0; i < len(s); i++ {
63
if s[i] >= utf8.RuneSelf {