Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c20d7a0

Browse files
committed
Refactor strip_email_quotes
Also removes long_substr, which fixes the licesing issue mentioned in #13039.
1 parent 9e003eb commit c20d7a0

2 files changed

Lines changed: 16 additions & 41 deletions

File tree

IPython/utils/tests/test_text.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,19 +165,6 @@ def test_dollar_formatter():
165165
nt.assert_equal(s, "$HOME")
166166

167167

168-
def test_long_substr():
169-
data = ['hi']
170-
nt.assert_equal(text.long_substr(data), 'hi')
171-
172-
173-
def test_long_substr2():
174-
data = ['abc', 'abd', 'abf', 'ab']
175-
nt.assert_equal(text.long_substr(data), 'ab')
176-
177-
def test_long_substr_empty():
178-
data = []
179-
nt.assert_equal(text.long_substr(data), '')
180-
181168
def test_strip_email():
182169
src = """\
183170
>> >>> def f(x):

IPython/utils/text.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import os
1212
import re
13+
import string
1314
import sys
1415
import textwrap
1516
from string import Formatter
@@ -405,22 +406,6 @@ def wrap_paragraphs(text, ncols=80):
405406
return out_ps
406407

407408

408-
def long_substr(data):
409-
"""Return the longest common substring in a list of strings.
410-
411-
Credit: http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python
412-
"""
413-
substr = ''
414-
if len(data) > 1 and len(data[0]) > 0:
415-
for i in range(len(data[0])):
416-
for j in range(len(data[0])-i+1):
417-
if j > len(substr) and all(data[0][i:i+j] in x for x in data):
418-
substr = data[0][i:i+j]
419-
elif len(data) == 1:
420-
substr = data[0]
421-
return substr
422-
423-
424409
def strip_email_quotes(text):
425410
"""Strip leading email quotation characters ('>').
426411
@@ -447,27 +432,30 @@ def strip_email_quotes(text):
447432
In [4]: strip_email_quotes('> > text\\n> > more\\n> more...')
448433
Out[4]: '> text\\n> more\\nmore...'
449434
450-
So if any line has no quote marks ('>') , then none are stripped from any
435+
So if any line has no quote marks ('>'), then none are stripped from any
451436
of them ::
452-
437+
453438
In [5]: strip_email_quotes('> > text\\n> > more\\nlast different')
454439
Out[5]: '> > text\\n> > more\\nlast different'
455440
"""
456441
lines = text.splitlines()
457-
matches = set()
458-
for line in lines:
459-
prefix = re.match(r'^(\s*>[ >]*)', line)
460-
if prefix:
461-
matches.add(prefix.group(1))
442+
strip_len = 0
443+
444+
for characters in zip(*lines):
445+
# Check if all characters in this position are the same
446+
if len(set(characters)) > 1:
447+
break
448+
prefix_char = characters[0][0]
449+
450+
if prefix_char in string.whitespace or prefix_char == ">":
451+
strip_len += 1
462452
else:
463453
break
464-
else:
465-
prefix = long_substr(list(matches))
466-
if prefix:
467-
strip = len(prefix)
468-
text = '\n'.join([ ln[strip:] for ln in lines])
454+
455+
text = "\n".join([ln[strip_len:] for ln in lines])
469456
return text
470457

458+
471459
def strip_ansi(source):
472460
"""
473461
Remove ansi escape codes from text.

0 commit comments

Comments
 (0)