github.com/lianghucheng/zrddz@v0.0.0-20200923083010-c71f680932e2/src/golang.org/x/net/idna/example_test.go (about)

     1  // Code generated by running "go generate" in golang.org/x/text. DO NOT EDIT.
     2  
     3  // Copyright 2017 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  package idna_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"golang.org/x/net/idna"
    13  )
    14  
    15  func ExampleProfile() {
    16  	// Raw Punycode has no restrictions and does no mappings.
    17  	fmt.Println(idna.ToASCII(""))
    18  	fmt.Println(idna.ToASCII("*.faß.com"))
    19  	fmt.Println(idna.Punycode.ToASCII("*.faß.com"))
    20  
    21  	// Rewrite IDN for lookup. This (currently) uses transitional mappings to
    22  	// find a balance between IDNA2003 and IDNA2008 compatibility.
    23  	fmt.Println(idna.Lookup.ToASCII(""))
    24  	fmt.Println(idna.Lookup.ToASCII("www.faß.com"))
    25  
    26  	// Convert an IDN to ASCII for registration purposes. This changes the
    27  	// encoding, but reports an error if the input was illformed.
    28  	fmt.Println(idna.Registration.ToASCII(""))
    29  	fmt.Println(idna.Registration.ToASCII("www.faß.com"))
    30  
    31  	// Output:
    32  	//  <nil>
    33  	// *.xn--fa-hia.com <nil>
    34  	// *.xn--fa-hia.com <nil>
    35  	//  <nil>
    36  	// www.fass.com <nil>
    37  	//  idna: invalid label ""
    38  	// www.xn--fa-hia.com <nil>
    39  }
    40  
    41  func ExampleNew() {
    42  	var p *idna.Profile
    43  
    44  	// Raw Punycode has no restrictions and does no mappings.
    45  	p = idna.New()
    46  	fmt.Println(p.ToASCII("*.faß.com"))
    47  
    48  	// Do mappings. Note that star is not allowed in a DNS lookup.
    49  	p = idna.New(
    50  		idna.MapForLookup(),
    51  		idna.Transitional(true)) // Map ß -> ss
    52  	fmt.Println(p.ToASCII("*.faß.com"))
    53  
    54  	// Lookup for registration. Also does not allow '*'.
    55  	p = idna.New(idna.ValidateForRegistration())
    56  	fmt.Println(p.ToUnicode("*.faß.com"))
    57  
    58  	// Set up a profile maps for lookup, but allows wild cards.
    59  	p = idna.New(
    60  		idna.MapForLookup(),
    61  		idna.Transitional(true),      // Map ß -> ss
    62  		idna.StrictDomainName(false)) // Set more permissive ASCII rules.
    63  	fmt.Println(p.ToASCII("*.faß.com"))
    64  
    65  	// Output:
    66  	// *.xn--fa-hia.com <nil>
    67  	// *.fass.com idna: disallowed rune U+002A
    68  	// *.faß.com idna: disallowed rune U+002A
    69  	// *.fass.com <nil>
    70  }