1 |
73
|
aaronmk
|
# String manipulation
|
2 |
|
|
|
3 |
|
|
def to_unicode(str_):
|
4 |
|
|
if isinstance(str_, unicode): return str_
|
5 |
|
|
encodings = ['utf_8', 'latin_1']
|
6 |
|
|
for encoding in encodings:
|
7 |
|
|
try: return unicode(str_, encoding)
|
8 |
|
|
except UnicodeDecodeError, e: pass
|
9 |
|
|
raise AssertionError(encoding+' is not a catch-all encoding')
|
10 |
340
|
aaronmk
|
|
11 |
|
|
def ensure_newl(str_): return str_.rstrip()+'\n'
|
12 |
714
|
aaronmk
|
|
13 |
856
|
aaronmk
|
def is_multiline(str_):
|
14 |
|
|
newl_idx = str_.find('\n')
|
15 |
|
|
return newl_idx >= 0 and newl_idx != len(str_)-1 # has newline before end
|
16 |
|
|
|
17 |
|
|
def one_line(str_):
|
18 |
|
|
if is_multiline(str_): return str_
|
19 |
|
|
else: return str_.rstrip()
|
20 |
|
|
|
21 |
714
|
aaronmk
|
def std_newl(str_): return str_.replace('\r\n', '\n').replace('\r', '\n')
|
22 |
|
|
|
23 |
|
|
def cleanup(str_): return std_newl(str_.strip())
|