|
| 1 | +""" |
| 2 | + stringutils |
| 3 | + ~~~~~~~~~~~ |
| 4 | +
|
| 5 | + A functional string utility library for Python. |
| 6 | +
|
| 7 | + :copyright: (c) 2018 Alex Hunt. |
| 8 | + :license: MIT, see LICENSE.txt for more details. |
| 9 | +""" |
| 10 | + |
| 11 | +import re |
| 12 | + |
| 13 | +def join(strings, sep=', ', insertend=False): |
| 14 | + """ |
| 15 | + Concatenate a list of strings into a single string by a separating |
| 16 | + delimiter. If *insertend* is given and true, the delimiter is also included |
| 17 | + at the end of the string. |
| 18 | + """ |
| 19 | + return sep.join(strings) |
| 20 | + |
| 21 | +def lines(string, keepends=False): |
| 22 | + """ |
| 23 | + Split a string into a list of strings at newline characters. Unless |
| 24 | + *keepends* is given and true, the resulting strings do not have newlines |
| 25 | + included. |
| 26 | + """ |
| 27 | + return string.splitlines(keepends) |
| 28 | + |
| 29 | +def words(string): |
| 30 | + """ |
| 31 | + Split a string into a list of words, which were delimited by one or more |
| 32 | + whitespace characters. |
| 33 | + """ |
| 34 | + return re.split('\s+', string) |
| 35 | + |
| 36 | +def unlines(lines): |
| 37 | + """ |
| 38 | + Join a list of lines into a single string after appending a terminating |
| 39 | + newline character to each. |
| 40 | + """ |
| 41 | + return join(lines, '\n', True) |
| 42 | + |
| 43 | +def unlines_universal(lines): |
| 44 | + """ |
| 45 | + Join a list of lines into a single string after appending a terminating |
| 46 | + CRLF newline sequence to each. |
| 47 | + """ |
| 48 | + return join(lines, '\r\n', True) |
| 49 | + |
| 50 | +def unwords(words): |
| 51 | + """ |
| 52 | + Join a list of words into a single string with separating spaces. |
| 53 | + """ |
| 54 | + return join(words, ' ') |
0 commit comments