func (test translateTest) run(t *testing.T) { cs := charset.Info(test.charset) if cs == nil { t.Fatalf("no info found for %q", test.charset) } fromtr, err := charset.TranslatorFrom(test.charset) if err != nil { t.Fatalf("error making translator from %q: %v", test.charset, err) } out, err := translate(fromtr, test.in) if err != nil { t.Fatalf("error translating from %q: %v", test.charset, err) } if out != test.out { t.Fatalf("error translating from %q: expected %x got %x", test.charset, test.out, out) } if cs.NoTo || !test.canRoundTrip { return } totr, err := charset.TranslatorTo(test.charset) if err != nil { t.Fatalf("error making translator to %q: %v", test.charset, err) } in, err := translate(totr, out) if err != nil { t.Fatalf("error translating to %q: %v", test.charset, err) } if in != test.in { t.Fatalf("%q round trip conversion failed; expected %x got %x", test.charset, test.in, in) } }
func ParseFileList(in io.Reader, out *FileListing) (err error) { defer func() { out.Name = out.Base }() data, err := ioutil.ReadAll(in) if err != nil { return } /* First, try just reading it */ err = xml.Unmarshal(data, out) if err == nil { return } /* If that failed, then try to read in another charset. This happens because * microdc2 is known to lie by saying that the content is utf-8 when it's * actually iso-8859-1 */ translator, err := charset.TranslatorFrom("iso-8859-1") if err != nil { return } _, data, err = translator.Translate(data, true) if err != nil { return } return xml.Unmarshal(data, out) }
func loadUrl(url string) (string, error) { resp, err := http.Get(url) if err != nil { return "", err } defer resp.Body.Close() bodyBytes, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } t, err := charset.TranslatorFrom("windows-1251") if err != nil { return "", err } _, translatedBytes, err := t.Translate(bodyBytes, true) if err != nil { return "", err } return string(translatedBytes), nil }