summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-01-14 18:31:29 (GMT)
committerGuido van Rossum <guido@python.org>1992-01-14 18:31:29 (GMT)
commita61ff7b1602d0f94340244bf4504d9eb0faf4ac7 (patch)
treeccaa2d79bd66ff4e9c493a69f3c5788997b03027 /Lib
parent49d6dc4123173ab77ac220c3298a6db621fd6d4e (diff)
downloadcpython-a61ff7b1602d0f94340244bf4504d9eb0faf4ac7.zip
cpython-a61ff7b1602d0f94340244bf4504d9eb0faf4ac7.tar.gz
cpython-a61ff7b1602d0f94340244bf4504d9eb0faf4ac7.tar.bz2
New implementation method for case conversion.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/string.py18
-rw-r--r--Lib/stringold.py18
2 files changed, 16 insertions, 20 deletions
diff --git a/Lib/string.py b/Lib/string.py
index 1412048..f7d4af6 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -12,34 +12,32 @@ hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
# Case conversion helpers
-_caseswap = {}
-for i in range(26):
- _caseswap[lowercase[i]] = uppercase[i]
- _caseswap[uppercase[i]] = lowercase[i]
+_idmap = ''
+for i in range(256): _idmap = _idmap + chr(i)
+_lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z')+1:]
+_upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
+_swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
del i
# convert UPPER CASE letters to lower case
def lower(s):
res = ''
for c in s:
- if 'A' <= c <= 'Z': c = _caseswap[c]
- res = res + c
+ res = res + _lower[ord(c)]
return res
# Convert lower case letters to UPPER CASE
def upper(s):
res = ''
for c in s:
- if 'a' <= c <= 'z': c = _caseswap[c]
- res = res + c
+ res = res + _upper[ord(c)]
return res
# Swap lower case letters and UPPER CASE
def swapcase(s):
res = ''
for c in s:
- if 'a' <= c <= 'z' or 'A' <= c <= 'Z': c = _caseswap[c]
- res = res + c
+ res = res + _swapcase[ord(c)]
return res
# Strip leading and trailing tabs and spaces
diff --git a/Lib/stringold.py b/Lib/stringold.py
index 1412048..f7d4af6 100644
--- a/Lib/stringold.py
+++ b/Lib/stringold.py
@@ -12,34 +12,32 @@ hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
# Case conversion helpers
-_caseswap = {}
-for i in range(26):
- _caseswap[lowercase[i]] = uppercase[i]
- _caseswap[uppercase[i]] = lowercase[i]
+_idmap = ''
+for i in range(256): _idmap = _idmap + chr(i)
+_lower = _idmap[:ord('A')] + lowercase + _idmap[ord('Z')+1:]
+_upper = _idmap[:ord('a')] + uppercase + _idmap[ord('z')+1:]
+_swapcase = _upper[:ord('A')] + lowercase + _upper[ord('Z')+1:]
del i
# convert UPPER CASE letters to lower case
def lower(s):
res = ''
for c in s:
- if 'A' <= c <= 'Z': c = _caseswap[c]
- res = res + c
+ res = res + _lower[ord(c)]
return res
# Convert lower case letters to UPPER CASE
def upper(s):
res = ''
for c in s:
- if 'a' <= c <= 'z': c = _caseswap[c]
- res = res + c
+ res = res + _upper[ord(c)]
return res
# Swap lower case letters and UPPER CASE
def swapcase(s):
res = ''
for c in s:
- if 'a' <= c <= 'z' or 'A' <= c <= 'Z': c = _caseswap[c]
- res = res + c
+ res = res + _swapcase[ord(c)]
return res
# Strip leading and trailing tabs and spaces