example_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // Lookup for registration. Also does not allow '*'.
  43. p = idna.New(idna.ValidateForRegistration())
  44. fmt.Println(p.ToUnicode("*.faß.com"))
  45. // Set up a profile maps for lookup, but allows wild cards.
  46. p = idna.New(
  47. idna.MapForLookup(),
  48. idna.Transitional(true), // Map ß -> ss
  49. idna.StrictDomainName(false)) // Set more permissive ASCII rules.
  50. fmt.Println(p.ToASCII("*.faß.com"))
  51. // Output:
  52. // *.xn--fa-hia.com <nil>
  53. // *.fass.com idna: disallowed rune U+002A
  54. // *.faß.com idna: disallowed rune U+002A
  55. // *.fass.com <nil>
  56. }