mirror of https://github.com/python/cpython.git
Martin von Löwis <martin@loewis.home.cs.tu-berlin.de>:
This patch delegates more string functions to string object methods, uses the varargs delegation syntax, and stops using stringold. Closes SourceForge patch #100712.
This commit is contained in:
parent
fcc95a4d04
commit
046d27215f
|
@ -125,9 +125,6 @@ def join(words, sep = ' '):
|
||||||
return sep.join(words)
|
return sep.join(words)
|
||||||
joinfields = join
|
joinfields = join
|
||||||
|
|
||||||
# for a little bit of speed
|
|
||||||
_apply = apply
|
|
||||||
|
|
||||||
# Find substring, raise exception if not found
|
# Find substring, raise exception if not found
|
||||||
def index(s, *args):
|
def index(s, *args):
|
||||||
"""index(s, sub [,start [,end]]) -> int
|
"""index(s, sub [,start [,end]]) -> int
|
||||||
|
@ -135,7 +132,7 @@ def index(s, *args):
|
||||||
Like find but raises ValueError when the substring is not found.
|
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
|
# Find last substring, raise exception if not found
|
||||||
def rindex(s, *args):
|
def rindex(s, *args):
|
||||||
|
@ -144,7 +141,7 @@ def rindex(s, *args):
|
||||||
Like rfind but raises ValueError when the substring is not found.
|
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
|
# Count non-overlapping occurrences of substring
|
||||||
def count(s, *args):
|
def count(s, *args):
|
||||||
|
@ -155,7 +152,7 @@ def count(s, *args):
|
||||||
interpreted as in slice notation.
|
interpreted as in slice notation.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return _apply(s.count, args)
|
return s.count(*args)
|
||||||
|
|
||||||
# Find substring, return -1 if not found
|
# Find substring, return -1 if not found
|
||||||
def find(s, *args):
|
def find(s, *args):
|
||||||
|
@ -168,7 +165,7 @@ def find(s, *args):
|
||||||
Return -1 on failure.
|
Return -1 on failure.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return _apply(s.find, args)
|
return s.find(*args)
|
||||||
|
|
||||||
# Find last substring, return -1 if not found
|
# Find last substring, return -1 if not found
|
||||||
def rfind(s, *args):
|
def rfind(s, *args):
|
||||||
|
@ -181,7 +178,7 @@ def rfind(s, *args):
|
||||||
Return -1 on failure.
|
Return -1 on failure.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
return _apply(s.rfind, args)
|
return s.rfind(*args)
|
||||||
|
|
||||||
# for a bit of speed
|
# for a bit of speed
|
||||||
_float = float
|
_float = float
|
||||||
|
@ -239,9 +236,7 @@ def ljust(s, width):
|
||||||
never truncated.
|
never truncated.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
n = width - len(s)
|
return s.ljust(width)
|
||||||
if n <= 0: return s
|
|
||||||
return s + ' '*n
|
|
||||||
|
|
||||||
# Right-justify a string
|
# Right-justify a string
|
||||||
def rjust(s, width):
|
def rjust(s, width):
|
||||||
|
@ -252,9 +247,7 @@ def rjust(s, width):
|
||||||
never truncated.
|
never truncated.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
n = width - len(s)
|
return s.rjust(width)
|
||||||
if n <= 0: return s
|
|
||||||
return ' '*n + s
|
|
||||||
|
|
||||||
# Center a string
|
# Center a string
|
||||||
def center(s, width):
|
def center(s, width):
|
||||||
|
@ -265,13 +258,7 @@ def center(s, width):
|
||||||
truncated.
|
truncated.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
n = width - len(s)
|
return s.center(width)
|
||||||
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)
|
|
||||||
|
|
||||||
# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
|
# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
|
||||||
# Decadent feature: the argument may be a string or a number
|
# Decadent feature: the argument may be a string or a number
|
||||||
|
@ -302,15 +289,7 @@ def expandtabs(s, tabsize=8):
|
||||||
column, and the tabsize (default 8).
|
column, and the tabsize (default 8).
|
||||||
|
|
||||||
"""
|
"""
|
||||||
res = line = ''
|
return s.expandtabs(tabsize)
|
||||||
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
|
|
||||||
|
|
||||||
# Character translation through look-up table.
|
# Character translation through look-up table.
|
||||||
def translate(s, table, deletions=""):
|
def translate(s, table, deletions=""):
|
||||||
|
@ -380,16 +359,6 @@ def replace(s, old, new, maxsplit=-1):
|
||||||
return s.replace(old, new, maxsplit)
|
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,
|
# Try importing optional built-in module "strop" -- if it exists,
|
||||||
# it redefines some string operations that are 100-1000 times faster.
|
# it redefines some string operations that are 100-1000 times faster.
|
||||||
# It also defines values for whitespace, lowercase and uppercase
|
# It also defines values for whitespace, lowercase and uppercase
|
||||||
|
|
Loading…
Reference in New Issue