OSDN Git Service

new repo
[bytom/vapor.git] / vendor / golang.org / x / text / internal / export / idna / example_test.go
1 // Copyright 2017 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.
4
5 package idna_test
6
7 import (
8         "fmt"
9
10         "golang.org/x/text/internal/export/idna"
11 )
12
13 func ExampleProfile() {
14         // Raw Punycode has no restrictions and does no mappings.
15         fmt.Println(idna.ToASCII(""))
16         fmt.Println(idna.ToASCII("*.faß.com"))
17         fmt.Println(idna.Punycode.ToASCII("*.faß.com"))
18
19         // Rewrite IDN for lookup. This (currently) uses transitional mappings to
20         // find a balance between IDNA2003 and IDNA2008 compatibility.
21         fmt.Println(idna.Lookup.ToASCII(""))
22         fmt.Println(idna.Lookup.ToASCII("www.faß.com"))
23
24         // Convert an IDN to ASCII for registration purposes. This changes the
25         // encoding, but reports an error if the input was illformed.
26         fmt.Println(idna.Registration.ToASCII(""))
27         fmt.Println(idna.Registration.ToASCII("www.faß.com"))
28
29         // Output:
30         //  <nil>
31         // *.xn--fa-hia.com <nil>
32         // *.xn--fa-hia.com <nil>
33         //  <nil>
34         // www.fass.com <nil>
35         //  idna: invalid label ""
36         // www.xn--fa-hia.com <nil>
37 }
38
39 func ExampleNew() {
40         var p *idna.Profile
41
42         // Raw Punycode has no restrictions and does no mappings.
43         p = idna.New()
44         fmt.Println(p.ToASCII("*.faß.com"))
45
46         // Do mappings. Note that star is not allowed in a DNS lookup.
47         p = idna.New(
48                 idna.MapForLookup(),
49                 idna.Transitional(true)) // Map ß -> ss
50         fmt.Println(p.ToASCII("*.faß.com"))
51
52         // Lookup for registration. Also does not allow '*'.
53         p = idna.New(idna.ValidateForRegistration())
54         fmt.Println(p.ToUnicode("*.faß.com"))
55
56         // Set up a profile maps for lookup, but allows wild cards.
57         p = idna.New(
58                 idna.MapForLookup(),
59                 idna.Transitional(true),      // Map ß -> ss
60                 idna.StrictDomainName(false)) // Set more permissive ASCII rules.
61         fmt.Println(p.ToASCII("*.faß.com"))
62
63         // Output:
64         // *.xn--fa-hia.com <nil>
65         // *.fass.com idna: disallowed rune U+002A
66         // *.faß.com idna: disallowed rune U+002A
67         // *.fass.com <nil>
68 }