language: JavaScript (rhino) (rhino-1.7R4)
date: 619 days 12 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
var _Detab = function(text) {
// attacklab: Detab's completely rewritten for speed.
// In perl we could fix it by anchoring the regexp with \G.
// In javascript we're less fortunate.
 
        // expand first n-1 tabs
        text = text.replace(/\t(?=\t)/g,"    "); // attacklab: g_tab_width
 
        // replace the nth with two sentinels
        text = text.replace(/\t/g,"~A~B");
 
        // use the sentinel to anchor our regex so it doesn't explode
        text = text.replace(/~B(.+?)~A/g,
                function(wholeMatch,m1,m2) {
                        var leadingText = m1;
                        var numSpaces = 4 - leadingText.length % 4;  // attacklab: g_tab_width
 
                        // there *must* be a better way to do this:
                        for (var i=0; i<numSpaces; i++) leadingText+=" ";
 
                        return leadingText;
                }
        );
 
        // clean up sentinels
        text = text.replace(/~A/g,"    ");  // attacklab: g_tab_width
        text = text.replace(/~B/g,"");
 
        return text;
}
 
print (_Detab("Where\tis pancakes house?") + " " + _Detab("Where\tis pancakes house?").length);