blob: 1c45ab3b865bf8e8cb2b3d3c65438d3709168b4f [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
Guido van Rossumfac38b71991-04-07 13:42:19 +000082# Join words with spaces between them
83def join(words):
84 res = ''
85 for w in words:
86 res = res + (' ' + w)
87 return res[1:]
88
89# Join fields with separator
90def joinfields(words, sep):
91 res = ''
92 for w in words:
93 res = res + (sep + w)
94 return res[len(sep):]
95
Guido van Rossumc6360141990-10-13 19:23:40 +000096# Find substring
97index_error = 'substring not found in string.index'
98def index(s, sub):
99 n = len(sub)
Guido van Rossum66a07c01990-12-26 15:39:06 +0000100 for i in range(len(s) + 1 - n):
Guido van Rossumc6360141990-10-13 19:23:40 +0000101 if sub = s[i:i+n]: return i
102 raise index_error, (s, sub)
103
104# Convert string to integer
105atoi_error = 'non-numeric argument to string.atoi'
106def atoi(str):
107 s = str
108 if s[:1] in '+-': s = s[1:]
109 if not s: raise atoi_error, str
110 for c in s:
111 if c not in digits: raise atoi_error, str
112 return eval(str)
113
114# Left-justify a string
115def ljust(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000116 n = width - len(s)
117 if n <= 0: return s
118 return s + ' '*n
Guido van Rossumc6360141990-10-13 19:23:40 +0000119
120# Right-justify a string
121def rjust(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000122 n = width - len(s)
123 if n <= 0: return s
124 return ' '*n + s
Guido van Rossumc6360141990-10-13 19:23:40 +0000125
126# Center a string
127def center(s, width):
Guido van Rossumfac38b71991-04-07 13:42:19 +0000128 n = width - len(s)
129 if n <= 0: return s
130 half = n/2
131 if n%2 and width%2:
132 # This ensures that center(center(s, i), j) = center(s, j)
133 half = half+1
134 return ' '*half + s + ' '*(n-half)
Guido van Rossumc6360141990-10-13 19:23:40 +0000135
136# Zero-fill a number, e.g., (12, 3) --> '012' and (-3, 3) --> '-03'
137# Decadent feature: the argument may be a string or a number
138# (Use of this is deprecated; it should be a string as with ljust c.s.)
139def zfill(x, width):
140 if type(x) = type(''): s = x
141 else: s = `x`
142 n = len(s)
143 if n >= width: return s
144 sign = ''
Guido van Rossum333c2e01991-08-16 13:29:03 +0000145 if s[0] in ('-', '+'):
146 sign, s = s[0], s[1:]
Guido van Rossumc6360141990-10-13 19:23:40 +0000147 return sign + '0'*(width-n) + s