]> granicus.if.org Git - python/commitdiff
SF #1149508: ensure textwrap handles hyphenated numbers correctly,
authorGreg Ward <gward@python.net>
Sat, 5 Mar 2005 02:53:17 +0000 (02:53 +0000)
committerGreg Ward <gward@python.net>
Sat, 5 Mar 2005 02:53:17 +0000 (02:53 +0000)
eg. "2004-03-04" is not broken across lines.  (Merged from 2.4 branch.)

Lib/test/test_textwrap.py
Lib/textwrap.py
Misc/NEWS

index 17fd1cf04fb467e7ba9534a2333234c238447837..a21b7ce9b98978fa0a5afcf6174e572543961e2c 100644 (file)
@@ -165,6 +165,24 @@ What a mess!
                         ["this-is-a-useful-feature-for-reformatting-",
                          "posts-from-tim-peters'ly"])
 
+    def test_hyphenated_numbers(self):
+        # Test that hyphenated numbers (eg. dates) are not broken like words.
+        text = ("Python 1.0.0 was released on 1994-01-26.  Python 1.0.1 was\n"
+                "released on 1994-02-15.")
+
+        self.check_wrap(text, 30, ['Python 1.0.0 was released on',
+                                   '1994-01-26.  Python 1.0.1 was',
+                                   'released on 1994-02-15.'])
+        self.check_wrap(text, 40, ['Python 1.0.0 was released on 1994-01-26.',
+                                   'Python 1.0.1 was released on 1994-02-15.'])
+
+        text = "I do all my shopping at 7-11."
+        self.check_wrap(text, 25, ["I do all my shopping at",
+                                   "7-11."])
+        self.check_wrap(text, 27, ["I do all my shopping at",
+                                   "7-11."])
+        self.check_wrap(text, 29, ["I do all my shopping at 7-11."])
+
     def test_em_dash(self):
         # Test text with em-dashes
         text = "Em-dashes should be written -- thus."
index 8e3d8cf594531c6db38576c4f8b9cce8aade7fcb..4576ef2a3ff32a8b8f6b934d2cabebee740edf7c 100644 (file)
@@ -78,9 +78,10 @@ class TextWrapper:
     # splits into
     #   Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option!
     # (after stripping out empty strings).
-    wordsep_re = re.compile(r'(\s+|'                  # any whitespace
-                            r'[^\s\w]*\w{2,}-(?=\w{2,})|' # hyphenated words
-                            r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))')   # em-dash
+    wordsep_re = re.compile(
+        r'(\s+|'                                  # any whitespace
+        r'[^\s\w]*\w+[a-zA-Z]-(?=\w+[a-zA-Z])|'   # hyphenated words
+        r'(?<=[\w\!\"\'\&\.\,\?])-{2,}(?=\w))')   # em-dash
 
     # XXX this is not locale- or charset-aware -- string.lowercase
     # is US-ASCII only (and therefore English-only)
index 59c630dd00fc3c735364aca6817ab96bd3476bd2..86d3677d8753c282b8b4868cc87ed01ead5bf0cd 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -206,6 +206,10 @@ Library
 - ``UserString.MutableString`` now supports negative indices in
   ``__setitem__`` and ``__delitem__``
 
+- Bug #1149508: ``textwrap`` now handles hyphenated numbers (eg. "2004-03-05")
+  correctly.
+
+
 Build
 -----