summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1992-03-27 15:13:31 (GMT)
committerGuido van Rossum <guido@python.org>1992-03-27 15:13:31 (GMT)
commit6ff2e90c518113433b3ecf34b8c17d434079a414 (patch)
tree27fc2e75130fb3fe3bf3853b4400e121877ba803 /Lib
parentcb7ce349e26e5c513966ff6ec6002e2f696b81f1 (diff)
downloadcpython-6ff2e90c518113433b3ecf34b8c17d434079a414.zip
cpython-6ff2e90c518113433b3ecf34b8c17d434079a414.tar.gz
cpython-6ff2e90c518113433b3ecf34b8c17d434079a414.tar.bz2
Add function to expand tabs.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/string.py13
-rw-r--r--Lib/stringold.py13
2 files changed, 26 insertions, 0 deletions
diff --git a/Lib/string.py b/Lib/string.py
index f7d4af6..cfb977f 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 f7d4af6..cfb977f 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