summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorHye-Shik Chang <hyeshik@gmail.com>2004-06-04 03:18:12 (GMT)
committerHye-Shik Chang <hyeshik@gmail.com>2004-06-04 03:18:12 (GMT)
commit5f5125997beba34f79faa0751c6fa1fa74d97ae2 (patch)
tree1042d481bc2d29356afc29dd7f16c81c7aee7b32 /Lib
parent0701bd64aaf5eacafa7b18f0e67273ed75943a8b (diff)
downloadcpython-5f5125997beba34f79faa0751c6fa1fa74d97ae2.zip
cpython-5f5125997beba34f79faa0751c6fa1fa74d97ae2.tar.gz
cpython-5f5125997beba34f79faa0751c6fa1fa74d97ae2.tar.bz2
Add iswide() and width() method for UserString according as the
addition to unicode objects.
Diffstat (limited to 'Lib')
-rwxr-xr-xLib/UserString.py4
-rw-r--r--Lib/test/string_tests.py25
-rwxr-xr-xLib/test/test_userstring.py3
3 files changed, 31 insertions, 1 deletions
diff --git a/Lib/UserString.py b/Lib/UserString.py
index e8e0fed..914f3ce 100755
--- a/Lib/UserString.py
+++ b/Lib/UserString.py
@@ -126,6 +126,10 @@ class UserString:
def upper(self): return self.__class__(self.data.upper())
def zfill(self, width): return self.__class__(self.data.zfill(width))
+ # the following methods are defined for unicode objects only:
+ def iswide(self): return self.data.iswide()
+ def width(self): return self.data.width()
+
class MutableString(UserString):
"""mutable string objects
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 860c1f2..edccb8f 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -695,3 +695,28 @@ class MixinStrUserStringTest:
self.checkraises(TypeError, 'xyz', 'decode', 42)
self.checkraises(TypeError, 'xyz', 'encode', 42)
+
+
+class MixinUnicodeUserStringTest:
+ # Additional tests that only work with
+ # unicode compatible object, i.e. unicode and UserString
+
+ def test_iswide(self):
+ self.checkequal(False, u'', 'iswide')
+ self.checkequal(False, u'\x1f', 'iswide') # Neutral
+ self.checkequal(False, u'\x20', 'iswide') # Narrow
+ self.checkequal(True, u'\u2329', 'iswide') # Wide
+ self.checkequal(False, u'\uff64', 'iswide') # Half
+ self.checkequal(True, u'\u3000', 'iswide') # Full
+ self.checkequal(False, u'\u2460', 'iswide') # Ambiguous
+ self.checkequal(True, u'\ud55c\uae00', 'iswide')
+ self.checkequal(False, u'\ud55c\u2606\uae00', 'iswide')
+
+ def test_width(self):
+ self.checkequal(0, u'', 'width')
+ self.checkequal(4, u'abcd', 'width')
+ self.checkequal(2, u'\u0187\u01c9', 'width')
+ self.checkequal(3, u'\u2460\u2329', 'width')
+ self.checkequal(3, u'\u2329\u2460', 'width')
+ self.checkequal(4, u'\ud55c\uae00', 'width')
+ self.checkequal(5, u'\ud55c\u2606\uae00', 'width')
diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py
index 990199e..b70081f 100755
--- a/Lib/test/test_userstring.py
+++ b/Lib/test/test_userstring.py
@@ -11,7 +11,8 @@ class UserStringTest(
string_tests.CommonTest,
string_tests.MixinStrUnicodeUserStringTest,
string_tests.MixinStrStringUserStringTest,
- string_tests.MixinStrUserStringTest
+ string_tests.MixinStrUserStringTest,
+ string_tests.MixinUnicodeUserStringTest
):
type2test = UserString