e r l a n g : c o o k b o o k

/ Erlang.StringTabs

This Web


WebHome 
WebChanges 
TOC
NewRecipe 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Erlang Links


Erlang.org
Erlang Wiki
ErlMan
Erlang Wiki
The Jungerl
Erlang-fr.org
Joe Armstrong
Lambda the Ultimate

Erlang Web Ring


[Prev]: Joe Armstrong's Page
[Next]: Joe Armstrong's Page

Expanding and Compressing Tabs

Problem

You want to convert spaces in a string to tabs, or vice versa.

Solution

You can define this by searching for tabs and expanding them as they're found:
-define(TABSTOPS,4).

expand_tabs(Str) ->
    expand_tabs("", Str).

expand_tabs(Accum, Str) ->
    Tabloc = string:chr(Str, $\t),
    case Tabloc of
        0 -> Accum ++ Str;
        _ -> UpToTab = string:substr(Str, 1, Tabloc - 1),
             AfterTab = string:substr(Str, Tabloc + 1),
             Extra = (Tabloc - 1) rem ?TABSTOPS,
             SpaceCount = case Extra of
                              0 -> ?TABSTOPS;
                              _ -> ?TABSTOPS - Extra
                          end,
             expand_tabs(Accum ++ UpToTab ++
               string:chars($ , SpaceCount), AfterTab)
    end.


This looks like this in action:
1> cookbook:expand_tabs("12345\t678\t9").             
"12345   678 9"
2> cookbook:expand_tabs("this is\ta test.").          
"this is a test."
3> cookbook:expand_tabs("this is \ta test.").
"this is     a test."
4> cookbook:expand_tabs("this is  \ta test.").
"this is     a test."
5> cookbook:expand_tabs("this is   \ta test.").
"this is     a test."
6> cookbook:expand_tabs("this is    \ta test.").
"this is     a test."
7> cookbook:expand_tabs("this is     \ta test.").
"this is         a test."

Discussion

Python includes an expandtabs() method, but Erlang doesn't. This is probably because the tab/space distinction is very important in Python source code, but not so in Erlang. Still, it's a useful thing to do sometimes. The tricky thing about tab expansion is that a tab doesn't translate directly into a fixed number of spaces; you have to calculate the number of spaces for each tab to reach the next tabstop. Tabstops divide the string into equal partitions of the length specified, and the action of a tab character is to move forward to the next tabstop. For example, if your tabstop is 4 and you have the string "12345\t678\t", the first tab converts to 3 spaces and the 2nd converts to one space, e.g. "12345 678 ".

Contributors

Based on work by GordonWeakliem and NoelWelsh.

-- BrentAFulgham - 20 Aug 2004

CookbookForm
TopicType: Recipe
ParentTopic: StringRecipes
TopicOrder: 080

 
 
Copyright © 2004 by the contributing authors. All material on the Erlang Cookbook web site is the property of the contributing authors.
This material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest