MCPcopy Create free account
hub / github.com/golang/text / TestEncodeInvalidUTF8

Function TestEncodeInvalidUTF8

encoding/encoding_test.go:17–52  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func TestEncodeInvalidUTF8(t *testing.T) {
18 inputs := []string{
19 "hello.",
20 "wo\ufffdld.",
21 "ABC\xff\x80\x80", // Invalid UTF-8.
22 "\x80\x80\x80\x80\x80",
23 "\x80\x80D\x80\x80", // Valid rune at "D".
24 "E\xed\xa0\x80\xed\xbf\xbfF", // Two invalid UTF-8 runes (surrogates).
25 "G",
26 "H\xe2\x82", // U+20AC in UTF-8 is "\xe2\x82\xac", which we split over two
27 "\xacI\xe2\x82", // input lines. It maps to 0x80 in the Windows-1252 encoding.
28 }
29 // Each invalid source byte becomes '\x1a'.
30 want := strings.Replace("hello.wo?ld.ABC??????????D??E??????FGH\x80I??", "?", "\x1a", -1)
31
32 transformer := encoding.ReplaceUnsupported(charmap.Windows1252.NewEncoder())
33 gotBuf := make([]byte, 0, 1024)
34 src := make([]byte, 0, 1024)
35 for i, input := range inputs {
36 dst := make([]byte, 1024)
37 src = append(src, input...)
38 atEOF := i == len(inputs)-1
39 nDst, nSrc, err := transformer.Transform(dst, src, atEOF)
40 gotBuf = append(gotBuf, dst[:nDst]...)
41 src = src[nSrc:]
42 if err != nil && err != transform.ErrShortSrc {
43 t.Fatalf("i=%d: %v", i, err)
44 }
45 if atEOF && err != nil {
46 t.Fatalf("i=%d: atEOF: %v", i, err)
47 }
48 }
49 if got := string(gotBuf); got != want {
50 t.Fatalf("\ngot %+q\nwant %+q", got, want)
51 }
52}
53
54func TestReplacement(t *testing.T) {
55 for _, direction := range []string{"Decode", "Encode"} {

Callers

nothing calls this directly

Calls 3

ReplaceUnsupportedFunction · 0.92
NewEncoderMethod · 0.65
TransformMethod · 0.65

Tested by

no test coverage detected