You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 lines
1.9 KiB

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