New implementation method for case conversion.

This commit is contained in:
Guido van Rossum 1992-01-14 18:31:29 +00:00
parent 49d6dc4123
commit a61ff7b160
2 changed files with 16 additions and 20 deletions

View File

@ -12,34 +12,32 @@
octdigits = '01234567' octdigits = '01234567'
# Case conversion helpers # Case conversion helpers
_caseswap = {} _idmap = ''
for i in range(26): for i in range(256): _idmap = _idmap + chr(i)
_caseswap[lowercase[i]] = uppercase[i] _lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z')+1:]
_caseswap[uppercase[i]] = lowercase[i] _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
_swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
del i del i
# convert UPPER CASE letters to lower case # convert UPPER CASE letters to lower case
def lower(s): def lower(s):
res = '' res = ''
for c in s: for c in s:
if 'A' <= c <= 'Z': c = _caseswap[c] res = res + _lower[ord(c)]
res = res + c
return res return res
# Convert lower case letters to UPPER CASE # Convert lower case letters to UPPER CASE
def upper(s): def upper(s):
res = '' res = ''
for c in s: for c in s:
if 'a' <= c <= 'z': c = _caseswap[c] res = res + _upper[ord(c)]
res = res + c
return res return res
# Swap lower case letters and UPPER CASE # Swap lower case letters and UPPER CASE
def swapcase(s): def swapcase(s):
res = '' res = ''
for c in s: for c in s:
if 'a' <= c <= 'z' or 'A' <= c <= 'Z': c = _caseswap[c] res = res + _swapcase[ord(c)]
res = res + c
return res return res
# Strip leading and trailing tabs and spaces # Strip leading and trailing tabs and spaces

View File

@ -12,34 +12,32 @@
octdigits = '01234567' octdigits = '01234567'
# Case conversion helpers # Case conversion helpers
_caseswap = {} _idmap = ''
for i in range(26): for i in range(256): _idmap = _idmap + chr(i)
_caseswap[lowercase[i]] = uppercase[i] _lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z')+1:]
_caseswap[uppercase[i]] = lowercase[i] _upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
_swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
del i del i
# convert UPPER CASE letters to lower case # convert UPPER CASE letters to lower case
def lower(s): def lower(s):
res = '' res = ''
for c in s: for c in s:
if 'A' <= c <= 'Z': c = _caseswap[c] res = res + _lower[ord(c)]
res = res + c
return res return res
# Convert lower case letters to UPPER CASE # Convert lower case letters to UPPER CASE
def upper(s): def upper(s):
res = '' res = ''
for c in s: for c in s:
if 'a' <= c <= 'z': c = _caseswap[c] res = res + _upper[ord(c)]
res = res + c
return res return res
# Swap lower case letters and UPPER CASE # Swap lower case letters and UPPER CASE
def swapcase(s): def swapcase(s):
res = '' res = ''
for c in s: for c in s:
if 'a' <= c <= 'z' or 'A' <= c <= 'Z': c = _caseswap[c] res = res + _swapcase[ord(c)]
res = res + c
return res return res
# Strip leading and trailing tabs and spaces # Strip leading and trailing tabs and spaces