Project

General

Profile

« Previous | Next » 

Revision 4937

strings.py: remove_prefix(), remove_suffix(): Added require param to raise aan exception if the string does not have the given prefix/suffix

View differences:

strings.py
40 40
    if str_ == '': return []
41 41
    else: return str_.split(sep)
42 42

  
43
def remove_prefix(prefix, str_, removed_ref=None):
43
def remove_prefix(prefix, str_, removed_ref=None, require=False):
44 44
    if removed_ref == None: removed_ref = [False]
45 45
    
46 46
    removed_ref[0] = str_.startswith(prefix)
47 47
    if removed_ref[0]: return str_[len(prefix):]
48
    elif require: raise Exception(urepr(str_)+' needs '+urepr(prefix)+' prefix')
48 49
    else: return str_
49 50

  
50 51
def remove_prefixes(prefixes, str_):
......
53 54

  
54 55
def with_prefixes(prefixes, str_): return (p+str_ for p in prefixes)
55 56

  
56
def remove_suffix(suffix, str_, removed_ref=None):
57
def remove_suffix(suffix, str_, removed_ref=None, require=False):
57 58
    if removed_ref == None: removed_ref = [False]
58 59
    
59 60
    removed_ref[0] = str_.endswith(suffix)
60 61
    if removed_ref[0]: return str_[:-len(suffix)]
62
    elif require: raise Exception(urepr(str_)+' needs '+urepr(suffix)+' suffix')
61 63
    else: return str_
62 64

  
63 65
def contains_any(haystack, needles):

Also available in: Unified diff