Ticket #7297: 7297.2.diff

File 7297.2.diff, 2.3 KB (added by Antti Kaihola, 16 years ago)

Added explanation and code examples to smart_split() to illustrate its un-escaping behavior.

  • django/utils/text.py

     
    118118    spaces are converted to underscores; and all non-filename-safe characters
    119119    are removed.
    120120    >>> get_valid_filename("john's portrait in 2004.jpg")
    121     'johns_portrait_in_2004.jpg'
     121    u'johns_portrait_in_2004.jpg'
    122122    """
    123123    s = force_unicode(s).strip().replace(' ', '_')
    124124    return re.sub(r'[^-A-Za-z0-9_.]', '', s)
     
    127127def get_text_list(list_, last_word=ugettext_lazy(u'or')):
    128128    """
    129129    >>> get_text_list(['a', 'b', 'c', 'd'])
    130     'a, b, c or d'
     130    u'a, b, c or d'
    131131    >>> get_text_list(['a', 'b', 'c'], 'and')
    132     'a, b and c'
     132    u'a, b and c'
    133133    >>> get_text_list(['a', 'b'], 'and')
    134     'a and b'
     134    u'a and b'
    135135    >>> get_text_list(['a'])
    136     'a'
     136    u'a'
    137137    >>> get_text_list([])
    138     ''
     138    u''
    139139    """
    140140    if len(list_) == 0: return u''
    141141    if len(list_) == 1: return force_unicode(list_[0])
     
    198198
    199199smart_split_re = re.compile('("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\'|[^\\s]+)')
    200200def smart_split(text):
    201     """
     201    r"""
    202202    Generator that splits a string by spaces, leaving quoted phrases together.
    203203    Supports both single and double quotes, and supports escaping quotes with
    204204    backslashes. In the output, strings will keep their initial and trailing
    205     quote marks.
     205    quote marks. If similar quote marks were backslash-escaped, they are
     206    un-escaped in the output. Double backslashes are always un-escaped.
    206207
    207     >>> list(smart_split('This is "a person\'s" test.'))
    208     ['This', 'is', '"a person\'s"', 'test.']
     208    >>> list(smart_split(r'This is "a person\'s" test.'))
     209    [u'This', u'is', u'"a person\\\'s"', u'test.']
     210    >>> list(smart_split(r"Another 'person\'s' test."))
     211    [u'Another', u"'person's'", u'test.']
     212    >>> list(smart_split(r'A "\"funky\" style" test.'))
     213    [u'A', u'""funky" style"', u'test.']
    209214    """
    210215    text = force_unicode(text)
    211216    for bit in smart_split_re.finditer(text):
     
    218223            yield bit
    219224smart_split = allow_lazy(smart_split, unicode)
    220225
     226# Work-around for http://bugs.python.org/issue1108
     227__doc__ = ''.join((
     228    get_valid_filename.__doc__,
     229    get_text_list.__doc__,
     230    smart_split.__doc__,
     231))
     232
Back to Top