return sep.join(words)
joinfields = join
-# for a little bit of speed
-_apply = apply
-
# Find substring, raise exception if not found
def index(s, *args):
"""index(s, sub [,start [,end]]) -> int
Like find but raises ValueError when the substring is not found.
"""
- return _apply(s.index, args)
+ return s.index(*args)
# Find last substring, raise exception if not found
def rindex(s, *args):
Like rfind but raises ValueError when the substring is not found.
"""
- return _apply(s.rindex, args)
+ return s.rindex(*args)
# Count non-overlapping occurrences of substring
def count(s, *args):
interpreted as in slice notation.
"""
- return _apply(s.count, args)
+ return s.count(*args)
# Find substring, return -1 if not found
def find(s, *args):
Return -1 on failure.
"""
- return _apply(s.find, args)
+ return s.find(*args)
# Find last substring, return -1 if not found
def rfind(s, *args):
Return -1 on failure.
"""
- return _apply(s.rfind, args)
+ return s.rfind(*args)
# for a bit of speed
_float = float
never truncated.
"""
- n = width - len(s)
- if n <= 0: return s
- return s + ' '*n
+ return s.ljust(width)
# Right-justify a string
def rjust(s, width):
never truncated.
"""
- n = width - len(s)
- if n <= 0: return s
- return ' '*n + s
+ return s.rjust(width)
# Center a string
def center(s, width):
truncated.
"""
- n = width - len(s)
- if n <= 0: return s
- half = n/2
- if n%2 and width%2:
- # This ensures that center(center(s, i), j) = center(s, j)
- half = half+1
- return ' '*half + s + ' '*(n-half)
+ return s.center(width)
# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
# Decadent feature: the argument may be a string or a number
column, and the tabsize (default 8).
"""
- res = line = ''
- for c in s:
- if c == '\t':
- c = ' '*(tabsize - len(line) % tabsize)
- line = line + c
- if c == '\n':
- res = res + line
- line = ''
- return res + line
+ return s.expandtabs(tabsize)
# Character translation through look-up table.
def translate(s, table, deletions=""):
return s.replace(old, new, maxsplit)
-# XXX: transitional
-#
-# If string objects do not have methods, then we need to use the old string.py
-# library, which uses strop for many more things than just the few outlined
-# below.
-try:
- ''.upper
-except AttributeError:
- from stringold import *
-
# Try importing optional built-in module "strop" -- if it exists,
# it redefines some string operations that are 100-1000 times faster.
# It also defines values for whitespace, lowercase and uppercase