golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/idna/example_test.go (about)

     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/net/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("*.GÖPHER.com"))
    17  	fmt.Println(idna.Punycode.ToASCII("*.GÖPHER.com"))
    18  
    19  	// Rewrite IDN for lookup.
    20  	fmt.Println(idna.Lookup.ToASCII(""))
    21  	fmt.Println(idna.Lookup.ToASCII("www.GÖPHER.com"))
    22  
    23  	// Convert an IDN to ASCII for registration purposes.
    24  	// This reports an error if the input was illformed.
    25  	fmt.Println(idna.Registration.ToASCII("www.GÖPHER.com"))
    26  	fmt.Println(idna.Registration.ToASCII("www.göpher.com"))
    27  
    28  	// Output:
    29  	//  <nil>
    30  	// *.xn--GPHER-1oa.com <nil>
    31  	// *.xn--GPHER-1oa.com <nil>
    32  	//  <nil>
    33  	// www.xn--gpher-jua.com <nil>
    34  	// www.xn--GPHER-1oa.com idna: disallowed rune U+0047
    35  	// www.xn--gpher-jua.com <nil>
    36  }
    37  
    38  func ExampleNew() {
    39  	var p *idna.Profile
    40  
    41  	// Raw Punycode has no restrictions and does no mappings.
    42  	p = idna.New()
    43  	fmt.Println(p.ToASCII("*.faß.com"))
    44  
    45  	// Do mappings. Note that star is not allowed in a DNS lookup.
    46  	p = idna.New(
    47  		idna.MapForLookup(),
    48  		idna.Transitional(true)) // Map ß -> ss
    49  	fmt.Println(p.ToASCII("*.faß.com"))
    50  
    51  	// Lookup for registration. Also does not allow '*'.
    52  	p = idna.New(idna.ValidateForRegistration())
    53  	fmt.Println(p.ToUnicode("*.faß.com"))
    54  
    55  	// Set up a profile maps for lookup, but allows wild cards.
    56  	p = idna.New(
    57  		idna.MapForLookup(),
    58  		idna.Transitional(true),      // Map ß -> ss
    59  		idna.StrictDomainName(false)) // Set more permissive ASCII rules.
    60  	fmt.Println(p.ToASCII("*.faß.com"))
    61  
    62  	// Output:
    63  	// *.xn--fa-hia.com <nil>
    64  	// *.fass.com idna: disallowed rune U+002A
    65  	// *.faß.com idna: disallowed rune U+002A
    66  	// *.fass.com <nil>
    67  }