Пример #1
0
func (r *ReadCloser) Close() error {
	_, err1 := C.ucnv_close(r.from)
	_, err2 := C.ucnv_close(r.to)
	if err1 != nil {
		return err1
	}
	return err2
}
Пример #2
0
func (w *WriteCloser) Close() error {
	_, err1 := C.ucnv_close(w.from)
	_, err2 := C.ucnv_close(w.to)
	if err1 != nil {
		return err1
	}
	return err2
}
Пример #3
0
func From(encoding string, in []byte) (ret []rune, err error) {
	var errCode C.UErrorCode
	cEncoding := C.CString(encoding)
	defer C.free(unsafe.Pointer(cEncoding))
	conv := C.ucnv_open(cEncoding, &errCode)
	if errCode != C.U_ZERO_ERROR {
		err = errors.New("to_runes: error when open converter")
		return
	}
	defer C.ucnv_close(conv)
	ret = make([]rune, 0, len(in))
	var target C.UChar32
	header := (*reflect.SliceHeader)(unsafe.Pointer(&in))
	source := (*C.char)(unsafe.Pointer(header.Data))
	sourceLimit := (*C.char)(unsafe.Pointer(header.Data + uintptr(header.Len)))
	for C.pointerLess(source, sourceLimit) == C.int(1) {
		target = C.ucnv_getNextUChar(conv, &source, sourceLimit, &errCode)
		if errCode != C.U_ZERO_ERROR {
			err = errors.New("to_runes: error when decoding")
			return
		}
		ret = append(ret, rune(target))
	}
	return
}