example_test.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2013 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 encoding_test
  5. import (
  6. "fmt"
  7. "io"
  8. "os"
  9. "strings"
  10. "golang.org/x/text/encoding"
  11. "golang.org/x/text/encoding/charmap"
  12. "golang.org/x/text/encoding/unicode"
  13. "golang.org/x/text/transform"
  14. )
  15. func ExampleDecodeWindows1252() {
  16. sr := strings.NewReader("Gar\xe7on !")
  17. tr := charmap.Windows1252.NewDecoder().Reader(sr)
  18. io.Copy(os.Stdout, tr)
  19. // Output: Garçon !
  20. }
  21. func ExampleUTF8Validator() {
  22. for i := 0; i < 2; i++ {
  23. var transformer transform.Transformer
  24. transformer = unicode.UTF16(unicode.BigEndian, unicode.IgnoreBOM).NewEncoder()
  25. if i == 1 {
  26. transformer = transform.Chain(encoding.UTF8Validator, transformer)
  27. }
  28. dst := make([]byte, 256)
  29. src := []byte("abc\xffxyz") // src is invalid UTF-8.
  30. nDst, nSrc, err := transformer.Transform(dst, src, true)
  31. fmt.Printf("i=%d: produced %q, consumed %q, error %v\n",
  32. i, dst[:nDst], src[:nSrc], err)
  33. }
  34. // Output:
  35. // i=0: produced "\x00a\x00b\x00c\xff\xfd\x00x\x00y\x00z", consumed "abc\xffxyz", error <nil>
  36. // i=1: produced "\x00a\x00b\x00c", consumed "abc", error encoding: invalid UTF-8
  37. }