<s> from robot . utils import OrderedDict , compress_text <EOL> class StringIndex ( int ) : <EOL> pass <EOL> class StringCache ( object ) : <EOL> _compress_threshold = <NUM_LIT> <EOL> _use_compressed_threshold = <NUM_LIT> <EOL> _zero_index = StringIndex ( <NUM_LIT:0> ) <EOL> def __init__ ( self ) : <EOL> self . _cache = OrderedDict ( { '<STR_LIT:*>' : self . _zero_index } ) <EOL> def add ( self , text ) : <EOL> if not text : <EOL> return self . _zero_index <EOL> text = self . _encode ( text ) <EOL> if text not in self . _cache : <EOL> self . _cache [ text ] = StringIndex ( len ( self . _cache ) ) <EOL> return self . _cache [ text ] <EOL> def _encode ( self , text ) : <EOL> raw = self . _raw ( text ) <EOL> if raw in self . _cache or len ( raw ) < self . _compress_threshold : <EOL> return raw <EOL> compressed = compress_text ( text ) <EOL> if len ( compressed ) * self . _use_compressed_threshold < len ( raw ) : <EOL> return compressed <EOL> return raw <EOL> def _raw ( self , text ) : <EOL> return '<STR_LIT:*>' + text <EOL> def dump ( self ) : <EOL> return tuple ( self . _cache ) </s>
