From: Guido van Rossum Date: Fri, 27 Mar 1992 15:13:31 +0000 (+0000) Subject: Add function to expand tabs. X-Git-Tag: v0.9.8~459 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6ff2e90c518113433b3ecf34b8c17d434079a414;p=thirdparty%2FPython%2Fcpython.git Add function to expand tabs. --- diff --git a/Lib/string.py b/Lib/string.py index f7d4af6b1e1a..cfb977fcc3f1 100644 --- a/Lib/string.py +++ b/Lib/string.py @@ -143,3 +143,16 @@ def zfill(x, width): if s[0] in ('-', '+'): sign, s = s[0], s[1:] return sign + '0'*(width-n) + s + +# Expand tabs in a string. +# Doesn't take non-printing chars into account, but does understand \n. +def expandtabs(s, tabsize): + res = line = '' + for c in s: + if c == '\t': + c = ' '*(tabsize - len(line)%tabsize) + line = line + c + if c == '\n': + res = res + line + line = '' + return res + line diff --git a/Lib/stringold.py b/Lib/stringold.py index f7d4af6b1e1a..cfb977fcc3f1 100644 --- a/Lib/stringold.py +++ b/Lib/stringold.py @@ -143,3 +143,16 @@ def zfill(x, width): if s[0] in ('-', '+'): sign, s = s[0], s[1:] return sign + '0'*(width-n) + s + +# Expand tabs in a string. +# Doesn't take non-printing chars into account, but does understand \n. +def expandtabs(s, tabsize): + res = line = '' + for c in s: + if c == '\t': + c = ' '*(tabsize - len(line)%tabsize) + line = line + c + if c == '\n': + res = res + line + line = '' + return res + line