root/lib/strings.py @ 818
1 |
# 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 |
|
11 |
def ensure_newl(str_): return str_.rstrip()+'\n' |
12 |
|
13 |
def std_newl(str_): return str_.replace('\r\n', '\n').replace('\r', '\n') |
14 |
|
15 |
def cleanup(str_): return std_newl(str_.strip()) |