331deca0a8
This shoud also fix issue #25: When the amount of data will be truncated iconv() will return EINVAL when An incomplete multibyte sequence is encountered in the input, and the input byte sequence terminates after it. So if the input is larger than the internal buffer of Reader and the end of the buffer conatins partial multi-byte chars, then Reader will failed with EINVAL. So when iconv() return EINVAL, we checks whether there are more data to process, if so, we continue without report an error to user.
39 lines
771 B
Go
39 lines
771 B
Go
package iconv
|
|
|
|
import (
|
|
"bytes"
|
|
"io/ioutil"
|
|
"testing"
|
|
)
|
|
|
|
func GbkToUtf8(src []byte) ([]byte, error) {
|
|
reader, err := NewReader(bytes.NewReader(src), "gbk", "utf-8")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ioutil.ReadAll(reader)
|
|
}
|
|
|
|
func Utf8ToGbk(src []byte) ([]byte, error) {
|
|
reader, err := NewReader(bytes.NewReader(src), "utf-8", "gbk")
|
|
reader.buffer = make([]byte, 16)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ioutil.ReadAll(reader)
|
|
}
|
|
|
|
func TestReaderWithDataLargerThanBuffer(t *testing.T) {
|
|
chars := []byte("梅")
|
|
for len(chars) < bufferSize*2 {
|
|
t.Logf("input size: %d", len(chars))
|
|
chars = append(chars, chars...)
|
|
_, err := Utf8ToGbk(chars)
|
|
if err != nil {
|
|
t.Fail()
|
|
t.Logf("failed with %d bytes data", len(chars))
|
|
return
|
|
}
|
|
}
|
|
}
|