Index: src/0dev.org/commands/short/main.go ================================================================== --- src/0dev.org/commands/short/main.go +++ src/0dev.org/commands/short/main.go @@ -19,24 +19,33 @@ rec := recommend(pairs, symbols) fmt.Println(*rec) } -type pair struct { - value uint16 - count uint64 -} - -type symbol struct { - value byte - count uint64 -} - type recommendation struct { p2s map[uint16]byte s2p map[byte]uint16 } + +func apply(rec *recommendation, reader io.Reader) { + symbolReader := iou.SizedReader(iou.ReaderFunc(func(output []byte) (int, error) { + for i := 0; i < len(output)-1; i++ { + // Read a byte from the underlying reader + count, err := reader.Read(output[i : i+1]) + if err != nil || count != 1 { + return i + count, err + } + + // Convert the byte to a pair if there is a mapping for it + if pair, ok := rec.s2p[output[i]]; ok { + output[i] = byte(pair >> 8) // extract the high byte from the pair + i++ + output[i] = byte(pair) // leave only the low byte from the pair + } + } + }), 2) +} func recommend(pairs pairSlice, symbols symbolSlice) *recommendation { var ( rec recommendation pairsLength = len(pairs) @@ -131,10 +140,20 @@ } sort.Sort(allSymbols) return availablePairs, allSymbols } + +type pair struct { + value uint16 + count uint64 +} + +type symbol struct { + value byte + count uint64 +} // Implements fmt.Stringer, used for debugging func (p pair) String() string { return fmt.Sprintf("[ %d %d (%d) ]", (p.value >> 8), ((p.value << 8) >> 8), p.count) }