Skip to content

Commit fc50592

Browse files
support true em-dashes
1 parent 8acb4cf commit fc50592

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎Lib/test/test_textwrap.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,41 @@ def test_em_dash(self):
269269
"he", " ", "was", " ", "gone"]
270270
self.check_split(text, expect)
271271

272+
def test_unicode_em_dash(self):
273+
# Test text with Unicode em-dashes
274+
text = "Em-dashes should be written \u2014 thus."
275+
self.check_wrap(text, 25,
276+
["Em-dashes should be",
277+
"written \u2014 thus."])
278+
279+
# Probe the boundaries of the em-dash. Parallels ASCII tests
280+
# but widths - 1 since len('\u2014') = len('--') - 1
281+
self.check_wrap(text, 28,
282+
["Em-dashes should be written",
283+
"\u2014 thus."])
284+
expect = ["Em-dashes should be written \u2014",
285+
"thus."]
286+
self.check_wrap(text, 29, expect)
287+
self.check_wrap(text, 34, expect)
288+
self.check_wrap(text, 35,
289+
["Em-dashes should be written \u2014 thus."])
290+
291+
# Tests for adjacent glyphs not needed for Unicode em-dash
292+
# because unlike adjacent hypens, not meaningful or common.
293+
294+
# All of the above behaviour could be deduced by probing the
295+
# _split() method. Note mixed real and simulated em-dashes.
296+
text = "Here's an \u2014 em-dash and\u2014here's another---and another! And--more!"
297+
expect = ["Here's", " ", "an", " ", "\u2014", " ", "em-", "dash", " ",
298+
"and", "\u2014", "here's", " ", "another", "---",
299+
"and", " ", "another!", " ", "And", "--", "more!"]
300+
301+
self.check_split(text, expect)
302+
303+
text = "and then\u2014bam!\u2014he was gone"
304+
expect = ["and", " ", "then", "\u2014", "bam!", "\u2014",
305+
"he", " ", "was", " ", "gone"]
306+
self.check_split(text, expect)
272307

273308
def test_unix_options (self):
274309
# Test that Unix-style command-line options are wrapped correctly.

‎Lib/textwrap.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,21 @@ class TextWrapper:
8181
wordsep_re = re.compile(r'''
8282
( # any whitespace
8383
%(ws)s+
84-
| # em-dash between words
84+
| # ASCII em-dash between words
8585
(?<=%(wp)s) -{2,} (?=\w)
86+
| # Unicode em-dash between words
87+
(?<=%(wp)s) \u2014 (?=\w)
8688
| # word, possibly hyphenated
8789
%(nws)s+? (?:
8890
# hyphenated word
8991
-(?: (?<=%(lt)s{2}-) | (?<=%(lt)s-%(lt)s-))
9092
(?= %(lt)s -? %(lt)s)
9193
| # end of word
9294
(?=%(ws)s|\Z)
93-
| # em-dash
95+
| # ASCII em-dash
9496
(?<=%(wp)s) (?=-{2,}\w)
97+
| # Unicode em-dash
98+
(?<=%(wp)s) (?=\u2014\w)
9599
)
96100
)''' % {'wp': word_punct, 'lt': letter,
97101
'ws': whitespace, 'nws': nowhitespace},

0 commit comments

Comments
 (0)