Project

General

Profile

1
# String manipulation
2

    
3
import codecs
4
import re
5

    
6
import util
7

    
8
##### 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_, removed_ref=None):
16
    if removed_ref == None: removed_ref = [False]
17
    
18
    removed_ref[0] = str_.startswith(prefix)
19
    if removed_ref[0]: return str_[len(prefix):]
20
    else: return str_
21

    
22
def remove_prefixes(prefixes, str_):
23
    for prefix in prefixes: str_ = remove_prefix(prefix, str_)
24
    return str_
25

    
26
def with_prefixes(prefixes, str_): return (p+str_ for p in prefixes)
27

    
28
def remove_suffix(suffix, str_, removed_ref=None):
29
    if removed_ref == None: removed_ref = [False]
30
    
31
    removed_ref[0] = str_.endswith(suffix)
32
    if removed_ref[0]: return str_[:-len(suffix)]
33
    else: return str_
34

    
35
def contains_any(haystack, needles):
36
    for needle in needles:
37
        if haystack.find(needle) >= 0: return True
38
    return False
39

    
40
def overlaps(str0, str1): return str0.find(str1) >= 0 or str1.find(str0) >= 0
41

    
42
##### Unicode
43

    
44
unicode_reader = codecs.getreader('utf_8')
45

    
46
def to_unicode(str_):
47
    if isinstance(str_, unicode): return str_
48
    encodings = ['utf_8', 'latin_1']
49
    for encoding in encodings:
50
        try: return unicode(str_, encoding)
51
        except UnicodeDecodeError, e: pass
52
    raise AssertionError(encoding+' is not a catch-all encoding')
53

    
54
def ustr(val):
55
    '''Like built-in str() but converts to unicode object'''
56
    if not util.is_str(val): val = str(val)
57
    return to_unicode(val)
58

    
59
##### Line endings
60

    
61
def extract_line_ending(line):
62
    '''@return tuple (contents, ending)'''
63
    contents = remove_suffix('\r', remove_suffix('\n', line))
64
    return (contents, line[len(contents):])
65

    
66
def remove_line_ending(line): return extract_line_ending(line)[0]
67

    
68
def ensure_newl(str_): return remove_line_ending(str_)+'\n'
69

    
70
def is_multiline(str_):
71
    newl_idx = str_.find('\n')
72
    return newl_idx >= 0 and newl_idx != len(str_)-1 # has newline before end
73

    
74
def remove_extra_newl(str_):
75
    if is_multiline(str_): return str_
76
    else: return str_.rstrip()
77

    
78
def std_newl(str_): return str_.replace('\r\n', '\n').replace('\r', '\n')
79

    
80
##### Whitespace
81

    
82
def cleanup(str_): return std_newl(str_.strip())
83

    
84
def single_space(str_): return re.sub(r' {2,}', r' ', str_.strip())
85

    
86
def one_line(str_): return re.sub(r'\n *', r' ', cleanup(str_))
87

    
88
##### Control characters
89

    
90
def is_ctrl(char):
91
    '''Whether char is a (non-printable) control character'''
92
    return ord(char) < 32 and not char.isspace()
93

    
94
def strip_ctrl(str_):
95
    '''Strips (non-printable) control characters'''
96
    return ''.join(filter(lambda c: not is_ctrl(c), str_))
(26-26/35)