| 1 | SCSU |
| 2 | ==== |
| 3 | |
| 4 | A Standard Compression Scheme for Unicode implementation in Go. |
| 5 | |
| 6 | [](https://godoc.org/github.com/dop251/scsu) |
| 7 | |
| 8 | This in an implementation of SCSU as described in https://www.unicode.org/reports/tr6/tr6-4.html |
| 9 | |
| 10 | Although UTF-8 is now the most commonly used and recommended encoding, in some cases the use of SCSU can |
| 11 | be beneficial. For example when storing or transmitting short alphabetical texts (Arabic, Hebrew, Russian, etc.) |
| 12 | where general-purpose compression algorithms are inefficient, but SCSU provides nearly 50% compression ratio |
| 13 | over UTF-8. |
| 14 | |
| 15 | The code is based on the sample Java implementation found at ftp://ftp.unicode.org/Public/PROGRAMS/SCSU/ however the |
| 16 | encoding algorithm has been slightly modified as the implementation above contains a few bugs. |
| 17 | |
| 18 | The code has been fuzz-tested using https://github.com/dvyukov/go-fuzz to ensure that random input neither crashes the |
| 19 | Encoder nor the Decoder, and if it happens to be a valid UTF-8, an Encode/Decode cycle produces identical output. |
| 20 | |
| 21 | Usage Scenarios. |
| 22 | ----- |
| 23 | |
| 24 | Encode a string into a []byte: |
| 25 | |
| 26 | ```go |
| 27 | b, err := scsu.Encode(s, nil) // the second argument can be an existing slice which will be appended |
| 28 | ``` |
| 29 | |
| 30 | Decode a []byte into a string: |
| 31 | |
| 32 | ```go |
| 33 | s, err := scsu.Decode(b) |
| 34 | ``` |
| 35 | |
| 36 | Use a Writer: |
| 37 | ```go |
| 38 | writer := scsu.NewWriter(outWriter) |
| 39 | n, err := writer.WriteString(s) |
| 40 | n, err = writer.WriteRune(r) |
| 41 | n, err = writer.WriteRunes(runeSource) |
| 42 | ``` |
| 43 | |
| 44 | Use an Encoder: |
| 45 | ```go |
| 46 | encoder := scsu.NewEncoder() |
| 47 | buf, err := encoder.Encode(runeSource, buf) // assuming buf has enough capacity this does zero allocs |
| 48 | // encoder then can be re-used |
| 49 | ``` |
| 50 | |
| 51 | Use a Reader: |
| 52 | ```go |
| 53 | reader := scsu.NewReader(byteReader) |
| 54 | s, err := reader.ReadString() // read the entire string |
| 55 | r, size, err := reader.ReadRune() // or a single rune |
| 56 | ``` |