internal/export/idna: make API compatible with x/net/idna
Also include a profile for displaying IDNs as recommended
by UTS 46.
Updates golang/go#17268
Change-Id: I33189fa8115e7891dbf21ba222ca28ce294437e2
Reviewed-on: https://go-review.googlesource.com/31274
Run-TryBot: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
diff --git a/internal/export/idna/idna.go b/internal/export/idna/idna.go
index 85317e0..d7c5a1c 100644
--- a/internal/export/idna/idna.go
+++ b/internal/export/idna/idna.go
@@ -27,6 +27,20 @@
"golang.org/x/text/unicode/norm"
)
+// ToASCII converts a domain or domain label to its ASCII form. For example,
+// ToASCII("bücher.example.com") is "xn--bcher-kva.example.com", and
+// ToASCII("golang") is "golang".
+func ToASCII(s string) (string, error) {
+ return Resolve.process(s, true)
+}
+
+// ToUnicode converts a domain or domain label to its Unicode form. For example,
+// ToUnicode("xn--bcher-kva.example.com") is "bücher.example.com", and
+// ToUnicode("golang") is "golang".
+func ToUnicode(s string) (string, error) {
+ return NonTransitional.process(s, false)
+}
+
// A Profile defines the configuration of a IDNA mapper.
type Profile struct {
Transitional bool
@@ -35,6 +49,22 @@
// ErrHandler func(error)
}
+// ToASCII converts a domain or domain label to its ASCII form. For example,
+// ToASCII("bücher.example.com") is "xn--bcher-kva.example.com", and
+// ToASCII("golang") is "golang".
+func (p *Profile) ToASCII(s string) (string, error) {
+ return p.process(s, true)
+}
+
+// ToUnicode converts a domain or domain label to its Unicode form. For example,
+// ToUnicode("xn--bcher-kva.example.com") is "bücher.example.com", and
+// ToUnicode("golang") is "golang".
+func (p *Profile) ToUnicode(s string) (string, error) {
+ pp := *p
+ pp.Transitional = false
+ return pp.process(s, false)
+}
+
// String reports a string with a description of the profile for debugging
// purposes. The string format may change with different versions.
func (p *Profile) String() string {
@@ -55,6 +85,10 @@
// The configuration of this profile may change over time.
Resolve = resolve
+ // Display is the recommended profile for displaying domain names.
+ // The configuration of this profile may change over time.
+ Display = display
+
// Transitional defines a profile that implements the Transitional mapping
// as defined in UTS #46 with no additional constraints.
Transitional = transitional
@@ -64,12 +98,14 @@
NonTransitional = nonTransitional
resolve = &Profile{Transitional: true}
+ display = &Profile{}
transitional = &Profile{Transitional: true}
nonTransitional = &Profile{}
// TODO: profiles
// V2008: strict IDNA2008
- // Registrar: recommended for approving domain names.
+ // Register: recommended for approving domain names: nontransitional, but
+ // bundle or block deviation characters.
)
// TODO: rethink error strategy
@@ -346,14 +382,6 @@
return nil
}
-func (p *Profile) ToASCII(s string) (string, error) {
- return p.process(s, true)
-}
-
-func (p *Profile) ToUnicode(s string) (string, error) {
- return NonTransitional.process(s, false)
-}
-
func ascii(s string) bool {
for i := 0; i < len(s); i++ {
if s[i] >= utf8.RuneSelf {