Merge pull request #443 from tp-m/more-string-funcs
Add more string funcs: to_upper(), to_lower(), contains() and underscorify()
This commit is contained in:
commit
2afc1dd497
|
@ -32,3 +32,4 @@ Nirbheek Chauhan
|
||||||
Nicolas Schneider
|
Nicolas Schneider
|
||||||
Luke Adams
|
Luke Adams
|
||||||
Rogiel Sulzbach
|
Rogiel Sulzbach
|
||||||
|
Tim-Philipp Müller
|
||||||
|
|
|
@ -2033,6 +2033,12 @@ class Interpreter():
|
||||||
return obj.strip()
|
return obj.strip()
|
||||||
elif method_name == 'format':
|
elif method_name == 'format':
|
||||||
return self.format_string(obj, args)
|
return self.format_string(obj, args)
|
||||||
|
elif method_name == 'to_upper':
|
||||||
|
return obj.upper()
|
||||||
|
elif method_name == 'to_lower':
|
||||||
|
return obj.lower()
|
||||||
|
elif method_name == 'underscorify':
|
||||||
|
return re.sub(r'[^a-zA-Z0-9]', '_', obj)
|
||||||
elif method_name == 'split':
|
elif method_name == 'split':
|
||||||
if len(posargs) > 1:
|
if len(posargs) > 1:
|
||||||
raise InterpreterException('Split() must have at most one argument.')
|
raise InterpreterException('Split() must have at most one argument.')
|
||||||
|
@ -2043,12 +2049,14 @@ class Interpreter():
|
||||||
return obj.split(s)
|
return obj.split(s)
|
||||||
else:
|
else:
|
||||||
return obj.split()
|
return obj.split()
|
||||||
elif method_name == 'startswith' or method_name == 'endswith':
|
elif method_name == 'startswith' or method_name == 'contains' or method_name == 'endswith':
|
||||||
s = posargs[0]
|
s = posargs[0]
|
||||||
if not isinstance(s, str):
|
if not isinstance(s, str):
|
||||||
raise InterpreterException('Argument must be a string.')
|
raise InterpreterException('Argument must be a string.')
|
||||||
if method_name == 'startswith':
|
if method_name == 'startswith':
|
||||||
return obj.startswith(s)
|
return obj.startswith(s)
|
||||||
|
elif method_name == 'contains':
|
||||||
|
return obj.find(s) >= 0
|
||||||
return obj.endswith(s)
|
return obj.endswith(s)
|
||||||
elif method_name == 'to_int':
|
elif method_name == 'to_int':
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -41,4 +41,41 @@ if long.endswith(prefix)
|
||||||
error('Not suffix.')
|
error('Not suffix.')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
if not long.contains(prefix)
|
||||||
|
error('Does not contain prefix')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if not long.contains(suffix)
|
||||||
|
error('Does not contain suffix')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if not long.contains('bcd')
|
||||||
|
error('Does not contain middle part')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if long.contains('dc')
|
||||||
|
error('Broken contains')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if long.to_upper() != 'ABCDE'
|
||||||
|
error('Broken to_upper')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if long.to_upper().to_lower() != long
|
||||||
|
error('Broken to_lower')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if 'struct stat.st_foo'.underscorify() != 'struct_stat_st_foo'
|
||||||
|
error('Broken underscorify')
|
||||||
|
endif
|
||||||
|
|
||||||
|
if '#include <foo/bar.h>'.underscorify() != '_include__foo_bar_h_'
|
||||||
|
error('Broken underscorify')
|
||||||
|
endif
|
||||||
|
|
||||||
|
# case should not change, space should be replaced, numbers are ok too
|
||||||
|
if 'Do SomeThing 09'.underscorify() != 'Do_SomeThing_09'
|
||||||
|
error('Broken underscorify')
|
||||||
|
endif
|
||||||
|
|
||||||
assert('3'.to_int() == 3, 'String int conversion does not work.')
|
assert('3'.to_int() == 3, 'String int conversion does not work.')
|
||||||
|
|
Loading…
Reference in New Issue