summaryrefslogtreecommitdiff
path: root/sources/scalac/util/Strings.java
blob: 8d5c1d85c50af799199f4747d98cff65cdcb97e7 (plain) (blame)
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public abstract class Strings {

    //########################################################################
    // Strings constants

    final public static String[] EMPTY_ARRAY = new String[0];

    //########################################################################
    // Strings interface

    /** The line separator */
    public static String EOL = System.getProperty("line.separator", "\n");

    /** Returns a string where all tabs have been replaced by white
     * spaces to make the corresponding fields the same width.
     */
    public static String format(List strings) {
        List[] lines = new List[strings.size()];
        List widths = new ArrayList();
        int height = 0;
        for (Iterator iterator = strings.iterator(); iterator.hasNext(); ) {
            String string = (String)iterator.next();
            List line = lines[height++] = new ArrayList();
            for (int last = 0; last < string.length(); ) {
                int next = string.indexOf('\t', last);
                if (next < 0) next = string.length();
                String substring = string.substring(last, next);
                int index = line.size();
                if (index == widths.size()) widths.add(new Integer(0));
                int width = ((Integer)widths.get(index)).intValue();
                widths.set(
                    index, new Integer(Math.max(width, substring.length())));
                line.add(substring);
                last = next + 1;
            }
        }
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < lines.length; i++) {
            List line = lines[i];
            for (int j = 0; true; j++) {
                String string = (String)line.get(j);
                buffer.append(string);
                if (j == line.size() - 1) break;
                int width = ((Integer)widths.get(j)).intValue();
                for (int k = string.length(); k<width; k++) buffer.append(' ');
            }
            buffer.append(EOL);
        }
        return buffer.toString();
    }

    /** Returns the first char of the string or -1 if the string is empty. */
    public static int firstChar(String string) {
        return string.length() == 0 ? -1 : string.charAt(0);
    }

    /** Returns the last char of the string or -1 if the string is empty. */
    public static int lastChar(String string) {
        return string.length() == 0 ? -1 : string.charAt(string.length() - 1);
    }

    /** Returns a copy of the string, with leading whitespace omitted. */
    public static String trimLeading(String string) {
        for (int i = 0; i < string.length(); i++)
            if (string.charAt(i) > ' ') return string.substring(i);
        return "";
    }

    /** Returns a copy of the string, with trailing whitespace omitted. */
    public static String trimTrailing(String string) {
        for (int i = string.length() - 1; i >= 0; i--)
            if (string.charAt(i) > ' ') return string.substring(0, i + 1);
        return "";
    }

    /** Returns the stack trace of the exception */
    public static String stackTrace(Throwable exception) {
        StringWriter buffer = new StringWriter();
        PrintWriter writer = new PrintWriter(buffer);
        exception.printStackTrace(writer);
        writer.close();
        return buffer.toString();
    }

    //########################################################################
}