Example #1
0
func readString(r io.Reader) (string, error) {
	var s int32
	if err := binary.Read(r, binary.BigEndian, &s); err != nil {
		return "", err
	}
	b := make([]byte, int(s))
	if _, err := r.Read(b); err != nil {
		return "", err
	}
	return string(b), nil
}
Example #2
0
func Decode(data string) (*Table, error) {
	r, err := gzip.NewReader(strings.NewReader(data))
	if err != nil {
		return nil, err
	}
	defer r.Close()
	var count int32
	if err := binary.Read(r, binary.BigEndian, &count); err != nil {
		return nil, err
	}
	c := int(count)
	translations := make(map[string]Translation, c)
	for ii := 0; ii < c; ii++ {
		key, err := readString(r)
		if err != nil {
			return nil, err
		}
		var tcount int32
		if err := binary.Read(r, binary.BigEndian, &tcount); err != nil {
			return nil, err
		}
		tc := int(tcount)
		value := make([]string, tc)
		for jj := 0; jj < tc; jj++ {
			tr, err := readString(r)
			if err != nil {
				return nil, err
			}
			value[jj] = tr
		}
		translations[key] = value
	}
	return &Table{
		translations: translations,
	}, nil
}