example_test.go 1.8 KB

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