Exemple #1
0
func (t *Table) Encode() (string, error) {
	var buf bytes.Buffer
	w, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
	if err != nil {
		return "", err
	}
	if err := binary.Write(w, binary.BigEndian, int32(len(t.translations))); err != nil {
		return "", err
	}
	for k, v := range t.translations {
		if err := writeString(w, k); err != nil {
			return "", err
		}
		if err := binary.Write(w, binary.BigEndian, int32(len(v))); err != nil {
			return "", err
		}
		for _, s := range v {
			if err := writeString(w, s); err != nil {
				return "", err
			}
		}
	}
	if err := w.Flush(); err != nil {
		return "", err
	}
	if err := w.Close(); err != nil {
		return "", err
	}
	_, err = Decode(buf.String())
	if err != nil {
		return "", fmt.Errorf("error testing encoding table data: %v", err)
	}
	return buf.String(), nil
}
Exemple #2
0
func writeString(w io.Writer, s string) error {
	b := []byte(s)
	if err := binary.Write(w, binary.BigEndian, int32(len(b))); err != nil {
		return err
	}
	_, err := w.Write(b)
	return err
}