Revision 1401
Added by Aaron Marcuse-Kubitza almost 13 years ago
lib/strings.py | ||
---|---|---|
5 | 5 |
|
6 | 6 |
import util |
7 | 7 |
|
8 |
##### Unicode |
|
9 |
|
|
8 | 10 |
unicode_reader = codecs.getreader('utf_8') |
9 | 11 |
|
10 | 12 |
def to_unicode(str_): |
... | ... | |
20 | 22 |
if not util.is_str(val): val = str(val) |
21 | 23 |
return to_unicode(val) |
22 | 24 |
|
25 |
##### Line endings |
|
26 |
|
|
23 | 27 |
def ensure_newl(str_): return str_.rstrip()+'\n' |
24 | 28 |
|
25 | 29 |
def is_multiline(str_): |
... | ... | |
32 | 36 |
|
33 | 37 |
def std_newl(str_): return str_.replace('\r\n', '\n').replace('\r', '\n') |
34 | 38 |
|
39 |
##### Whitespace |
|
40 |
|
|
35 | 41 |
def cleanup(str_): return std_newl(str_.strip()) |
36 | 42 |
|
37 | 43 |
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip()) |
38 | 44 |
|
39 | 45 |
def one_line(str_): return re.sub(r'\n *', r' ', cleanup(str_)) |
46 |
|
|
47 |
##### Parsing |
|
48 |
|
|
49 |
def split(sep, str_): |
|
50 |
'''Returns [] if str_ == ""''' |
|
51 |
if str_ == '': return [] |
|
52 |
else: return str_.split(sep) |
|
53 |
|
|
54 |
def remove_prefix(prefix, str_): |
|
55 |
if str_.startswith(prefix): return str_[len(prefix):] |
|
56 |
else: return str_ |
|
57 |
|
|
58 |
def remove_suffix(suffix, str_): |
|
59 |
if str_.endswith(suffix): return str_[:-len(suffix)] |
|
60 |
else: return str_ |
|
61 |
|
|
62 |
def remove_prefixes(prefixes, str_): |
|
63 |
for prefix in prefixes: str_ = remove_prefix(prefix, str_) |
|
64 |
return str_ |
Also available in: Unified diff
strings.py: Added split(), remove_prefix(), remove_suffix(), and remove_prefixes(). Added section comments.