]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Add function to expand tabs.
authorGuido van Rossum <guido@python.org>
Fri, 27 Mar 1992 15:13:31 +0000 (15:13 +0000)
committerGuido van Rossum <guido@python.org>
Fri, 27 Mar 1992 15:13:31 +0000 (15:13 +0000)
Lib/string.py
Lib/stringold.py

index f7d4af6b1e1a390145c585240071ef0190ee3542..cfb977fcc3f1f2f9e6fda571f375ca37f81fb93b 100644 (file)
@@ -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
index f7d4af6b1e1a390145c585240071ef0190ee3542..cfb977fcc3f1f2f9e6fda571f375ca37f81fb93b 100644 (file)
@@ -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