Ticket #1919: truncate_words_faster.diff

File truncate_words_faster.diff, 2.0 KB (added by arien <regexbot@…>, 17 years ago)

faster version of the same idea; includes additional tests for trailing whitespace handling

  • django/utils/text.py

     
    3636    return u''.join(_generator())
    3737wrap = allow_lazy(wrap, unicode)
    3838
     39word_re = re.compile(r'\S+')
    3940def truncate_words(s, num):
    4041    "Truncates a string after a certain number of words."
    4142    s = force_unicode(s)
    4243    length = int(num)
    43     words = s.split()
    44     if len(words) > length:
    45         words = words[:length]
    46         if not words[-1].endswith('...'):
    47             words.append('...')
    48     return u' '.join(words)
     44    if length < 1:
     45        return u''
     46    words = 0
     47    for match in word_re.finditer(s):
     48        words += 1
     49        if words == length:
     50            end = match.end()
     51            if len(s) > end:
     52                s = s[:end]
     53                if not s.endswith('...'):
     54                    s += ' ...'
     55            break
     56    return s
    4957truncate_words = allow_lazy(truncate_words, unicode)
    5058
    5159def truncate_html_words(s, num):
  • tests/regressiontests/defaultfilters/tests.py

     
    9898>>> truncatewords(u'A sentence with a few words in it', 'not a number')
    9999u'A sentence with a few words in it'
    100100
     101>>> truncatewords(u'Double-spaced  sentence  with  a  few  words', 2)
     102u'Double-spaced  sentence ...'
     103
     104>>> truncatewords(u'  Two leading spaces for this sentence', 3)
     105u'  Two leading spaces ...'
     106
     107>>> truncatewords(u'  Text with leading and trailing whitespace  ', 10)
     108u'  Text with leading and trailing whitespace  '
     109
     110>>> truncatewords(u'  Text with leading and trailing whitespace  ', 6)
     111u'  Text with leading and trailing whitespace ...'
     112
     113>>> truncatewords(u'  Text with leading and trailing whitespace  ', 1)
     114u'  Text ...'
     115
    101116>>> truncatewords_html(u'<p>one <a href="#">two - three <br>four</a> five</p>', 0)
    102117u''
    103118
Back to Top