Skip to content

Commit

Permalink
internal/export/idna: make API compatible with x/net/idna
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
mpvl committed Oct 17, 2016
1 parent c6e511c commit a3ead21
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions internal/export/idna/idna.go
Expand Up @@ -27,6 +27,20 @@ import (
"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
Expand All @@ -35,6 +49,22 @@ type Profile struct {
// 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 {
Expand All @@ -55,6 +85,10 @@ var (
// 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
Expand All @@ -64,12 +98,14 @@ var (
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
Expand Down Expand Up @@ -346,14 +382,6 @@ func (p *Profile) validate(s string) error {
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 {
Expand Down

0 comments on commit a3ead21

Please sign in to comment.