Robot Framework Integrated Development Environment (RIDE)
stringcache.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from collections import OrderedDict
17 
18 from robotide.lib.robot.utils import compress_text
19 
20 
22  pass
23 
24 
25 class StringCache():
26 
29  _compress_threshold = 80
30 
33  _use_compressed_threshold = 1.1
34 
37  _zero_index = StringIndex(0)
38 
39  def __init__(self):
40  self._cache_cache = OrderedDict({'*': self._zero_index_zero_index})
41 
42  def add(self, text):
43  if not text:
44  return self._zero_index_zero_index
45  text = self._encode_encode(text)
46  if text not in self._cache_cache:
47  self._cache_cache[text] = StringIndex(len(self._cache_cache))
48  return self._cache_cache[text]
49 
50  def _encode(self, text):
51  raw = self._raw_raw(text)
52  if raw in self._cache_cache or len(raw) < self._compress_threshold_compress_threshold:
53  return raw
54  compressed = compress_text(text)
55  if len(compressed) * self._use_compressed_threshold_use_compressed_threshold < len(raw):
56  return compressed
57  return raw
58 
59  def _raw(self, text):
60  return '*'+text
61 
62  def dump(self):
63  return tuple(self._cache_cache)