1 |
73
|
aaronmk
|
# String manipulation
|
2 |
|
|
|
3 |
1295
|
aaronmk
|
import codecs
|
4 |
861
|
aaronmk
|
import re
|
5 |
|
|
|
6 |
1232
|
aaronmk
|
import util
|
7 |
|
|
|
8 |
1622
|
aaronmk
|
##### Parsing
|
9 |
|
|
|
10 |
|
|
def split(sep, str_):
|
11 |
|
|
'''Returns [] if str_ == ""'''
|
12 |
|
|
if str_ == '': return []
|
13 |
|
|
else: return str_.split(sep)
|
14 |
|
|
|
15 |
|
|
def remove_prefix(prefix, str_):
|
16 |
|
|
if str_.startswith(prefix): return str_[len(prefix):]
|
17 |
|
|
else: return str_
|
18 |
|
|
|
19 |
|
|
def remove_suffix(suffix, str_):
|
20 |
|
|
if str_.endswith(suffix): return str_[:-len(suffix)]
|
21 |
|
|
else: return str_
|
22 |
|
|
|
23 |
|
|
def remove_prefixes(prefixes, str_):
|
24 |
|
|
for prefix in prefixes: str_ = remove_prefix(prefix, str_)
|
25 |
|
|
return str_
|
26 |
|
|
|
27 |
|
|
def overlaps(str0, str1): return str0.find(str1) >= 0 or str1.find(str0) >= 0
|
28 |
|
|
|
29 |
1401
|
aaronmk
|
##### Unicode
|
30 |
|
|
|
31 |
1295
|
aaronmk
|
unicode_reader = codecs.getreader('utf_8')
|
32 |
|
|
|
33 |
73
|
aaronmk
|
def to_unicode(str_):
|
34 |
|
|
if isinstance(str_, unicode): return str_
|
35 |
|
|
encodings = ['utf_8', 'latin_1']
|
36 |
|
|
for encoding in encodings:
|
37 |
|
|
try: return unicode(str_, encoding)
|
38 |
|
|
except UnicodeDecodeError, e: pass
|
39 |
|
|
raise AssertionError(encoding+' is not a catch-all encoding')
|
40 |
340
|
aaronmk
|
|
41 |
1232
|
aaronmk
|
def ustr(val):
|
42 |
|
|
'''Like built-in str() but converts to unicode object'''
|
43 |
|
|
if not util.is_str(val): val = str(val)
|
44 |
|
|
return to_unicode(val)
|
45 |
|
|
|
46 |
1401
|
aaronmk
|
##### Line endings
|
47 |
|
|
|
48 |
1622
|
aaronmk
|
def extract_line_ending(line):
|
49 |
|
|
'''@return tuple (contents, ending)'''
|
50 |
|
|
contents = remove_suffix('\r', remove_suffix('\n', line))
|
51 |
|
|
return (contents, line[len(contents):])
|
52 |
714
|
aaronmk
|
|
53 |
1622
|
aaronmk
|
def remove_line_ending(line): return extract_line_ending(line)[0]
|
54 |
|
|
|
55 |
|
|
def ensure_newl(str_): return remove_line_ending(str_)+'\n'
|
56 |
|
|
|
57 |
856
|
aaronmk
|
def is_multiline(str_):
|
58 |
|
|
newl_idx = str_.find('\n')
|
59 |
|
|
return newl_idx >= 0 and newl_idx != len(str_)-1 # has newline before end
|
60 |
|
|
|
61 |
860
|
aaronmk
|
def remove_extra_newl(str_):
|
62 |
856
|
aaronmk
|
if is_multiline(str_): return str_
|
63 |
|
|
else: return str_.rstrip()
|
64 |
|
|
|
65 |
714
|
aaronmk
|
def std_newl(str_): return str_.replace('\r\n', '\n').replace('\r', '\n')
|
66 |
|
|
|
67 |
1401
|
aaronmk
|
##### Whitespace
|
68 |
|
|
|
69 |
714
|
aaronmk
|
def cleanup(str_): return std_newl(str_.strip())
|
70 |
861
|
aaronmk
|
|
71 |
1364
|
aaronmk
|
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
|
72 |
|
|
|
73 |
861
|
aaronmk
|
def one_line(str_): return re.sub(r'\n *', r' ', cleanup(str_))
|