Jeremy Hylton | 53d527a | 2002-08-05 18:29:45 +0000 | [diff] [blame] | 1 | def cstring(s, width=70): |
| 2 | """Return C string representation of a Python string. |
| 3 | |
| 4 | width specifies the maximum width of any line of the C string. |
| 5 | """ |
| 6 | L = [] |
| 7 | for l in s.split("\n"): |
| 8 | if len(l) < width: |
| 9 | L.append(r'"%s\n"' % l) |
| 10 | |
| 11 | return "\n".join(L) |
| 12 | |
| 13 | def unindent(s, skipfirst=True): |
| 14 | """Return an unindented version of a docstring. |
| 15 | |
| 16 | Removes indentation on lines following the first one, using the |
| 17 | leading whitespace of the first indented line that is not blank |
| 18 | to determine the indentation. |
| 19 | """ |
| 20 | |
| 21 | lines = s.split("\n") |
| 22 | if skipfirst: |
| 23 | first = lines.pop(0) |
| 24 | L = [first] |
| 25 | else: |
| 26 | L = [] |
| 27 | indent = None |
| 28 | for l in lines: |
| 29 | ls = l.strip() |
| 30 | if ls: |
| 31 | indent = len(l) - len(ls) |
| 32 | break |
| 33 | L += [l[indent:] for l in lines] |
| 34 | |
| 35 | return "\n".join(L) |