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