blob: 3790357f1b5f50a54a8c3a5fe919c68443b2f96d [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# module 'string' -- A collection of string operations
2
3# XXX Some of these operations are incredibly slow and should be built in
4
5# Some strings for ctype-style character classification
6whitespace = ' \t\n'
7lowercase = 'abcdefghijklmnopqrstuvwxyz'
8uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
9letters = lowercase + uppercase
10digits = '0123456789'
11hexdigits = digits + 'abcdef' + 'ABCDEF'
12octdigits = '01234567'
13
14# Case conversion helpers
15_caseswap = {}
16for i in range(26):
17 _caseswap[lowercase[i]] = uppercase[i]
18 _caseswap[uppercase[i]] = lowercase[i]
19del i
20
21# convert UPPER CASE letters to lower case
22def lower(s):
23 res = ''
24 for c in s:
25 if 'A' <= c <= 'Z': c = _caseswap[c]
26 res = res + c
27 return res
28
29# Convert lower case letters to UPPER CASE
30def upper(s):
31 res = ''
32 for c in s:
33 if 'a' <= c <= 'z': c = _caseswap[c]
34 res = res + c
35 return res
36
37# Swap lower case letters and UPPER CASE
38def swapcase(s):
39 res = ''
40 for c in s:
41 if 'a' <= c <= 'z' or 'A' <= c <= 'Z': c = _caseswap[c]
42 res = res + c
43 return res
44
45# Strip leading and trailing tabs and spaces
46def strip(s):
47 i, j = 0, len(s)
48 while i < j and s[i] in whitespace: i = i+1
49 while i < j and s[j-1] in whitespace: j = j-1
50 return s[i:j]
51
52# Split a string into a list of space/tab-separated words
53# NB: split(s) is NOT the same as splitfields(s, ' ')!
54def split(s):
55 res = []
56 i, n = 0, len(s)
57 while i < n:
58 while i < n and s[i] in whitespace: i = i+1
59 if i = n: break
60 j = i
61 while j < n and s[j] not in whitespace: j = j+1
62 res.append(s[i:j])
63 i = j
64 return res
65
66# Split a list into fields separated by a given string
67# NB: splitfields(s, ' ') is NOT the same as split(s)!
68def splitfields(s, sep):
69 res = []
70 ns = len(s)
71 nsep = len(sep)
72 i = j = 0
73 while j+nsep <= ns:
74 if s[j:j+nsep] = sep:
75 res.append(s[i:j])
76 i = j = j + nsep
77 else:
78 j = j + 1
79 res.append(s[i:])
80 return res
81
82# Find substring
83index_error = 'substring not found in string.index'
84def index(s, sub):
85 n = len(sub)
Guido van Rossum66a07c01990-12-26 15:39:06 +000086 for i in range(len(s) + 1 - n):
Guido van Rossumc6360141990-10-13 19:23:40 +000087 if sub = s[i:i+n]: return i
88 raise index_error, (s, sub)
89
90# Convert string to integer
91atoi_error = 'non-numeric argument to string.atoi'
92def atoi(str):
93 s = str
94 if s[:1] in '+-': s = s[1:]
95 if not s: raise atoi_error, str
96 for c in s:
97 if c not in digits: raise atoi_error, str
98 return eval(str)
99
100# Left-justify a string
101def ljust(s, width):
102 n = len(s)
103 if n >= width: return s
104 return s + ' '*(width-n)
105
106# Right-justify a string
107def rjust(s, width):
108 n = len(s)
109 if n >= width: return s
110 return ' '*(width-n) + s
111
112# Center a string
113def center(s, width):
114 n = len(s)
115 if n >= width: return s
116 return ' '*((width-n)/2) + s + ' '*(width -(width-n)/2)
117
118# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
119# Decadent feature: the argument may be a string or a number
120# (Use of this is deprecated; it should be a string as with ljust c.s.)
121def zfill(x, width):
122 if type(x) = type(''): s = x
123 else: s = `x`
124 n = len(s)
125 if n >= width: return s
126 sign = ''
127 if s[0] = '-':
128 sign, s = '-', s[1:]
129 return sign + '0'*(width-n) + s