Commit | Line | Data |
---|---|---|
e83d6df7 VS |
1 | public class String |
2 | { | |
3 | string value; | |
4 | ||
5 | fun static String create(string value) | |
6 | { | |
7 | String s; | |
8 | value => s.value; | |
9 | return s; | |
10 | } | |
11 | ||
12 | // helpers | |
13 | ||
14 | // "x".substring(1) errors out (bug?), this doesn't | |
15 | fun static string slice(string input, int index) | |
16 | { | |
17 | if( index == input.length() ) | |
18 | { | |
19 | return ""; | |
20 | } | |
21 | else | |
22 | { | |
23 | return input.substring(index); | |
24 | } | |
25 | } | |
26 | ||
27 | fun static string slice(string input, int start, int end) | |
28 | { | |
29 | if( start == input.length() ) | |
30 | { | |
31 | return ""; | |
32 | } | |
33 | else | |
34 | { | |
35 | return input.substring(start, end - start); | |
36 | } | |
37 | } | |
38 | ||
39 | fun static string join(string parts[], string separator) | |
40 | { | |
41 | if( parts.size() == 0 ) | |
42 | { | |
43 | return ""; | |
44 | } | |
45 | ||
46 | parts[0] => string output; | |
47 | ||
48 | for( 1 => int i; i < parts.size(); i++ ) | |
49 | { | |
50 | output + separator + parts[i] => output; | |
51 | } | |
52 | ||
53 | return output; | |
54 | } | |
55 | ||
aa0ac94f VS |
56 | fun static string[] split(string input, string separator) |
57 | { | |
58 | string output[0]; | |
59 | ||
60 | if( input == "" ) | |
61 | { | |
62 | return output; | |
63 | } | |
64 | ||
65 | 0 => int offset; | |
66 | int index; | |
67 | ||
68 | while( true ) | |
69 | { | |
70 | input.find(separator, offset) => index; | |
71 | ||
72 | if( index == -1 ) | |
73 | { | |
74 | output << input.substring(offset); | |
75 | break; | |
76 | } | |
77 | ||
78 | output << input.substring(offset, index - offset); | |
79 | index + separator.length() => offset; | |
80 | } | |
81 | ||
82 | return output; | |
83 | } | |
84 | ||
674e1c56 VS |
85 | fun static string replaceAll(string input, string pat, string rep) |
86 | { | |
87 | 0 => int offset; | |
88 | input => string output; | |
89 | int index; | |
90 | ||
91 | while( true ) | |
92 | { | |
93 | if( offset >= output.length() ) | |
94 | { | |
95 | break; | |
96 | } | |
97 | ||
98 | output.find(pat, offset) => index; | |
99 | ||
100 | if( index == -1 ) | |
101 | { | |
102 | break; | |
103 | } | |
104 | ||
105 | output.replace(index, pat.length(), rep); | |
106 | index + rep.length() => offset; | |
107 | } | |
108 | ||
109 | return output; | |
110 | } | |
111 | ||
e83d6df7 VS |
112 | fun static string parse(string input) |
113 | { | |
114 | slice(input, 1, input.length() - 1) => string output; | |
674e1c56 VS |
115 | replaceAll(output, "\\\"", "\"") => output; |
116 | replaceAll(output, "\\n", "\n") => output; | |
117 | replaceAll(output, "\\\\", "\\") => output; | |
e83d6df7 VS |
118 | return output; |
119 | } | |
120 | ||
121 | fun static string repr(string input) | |
122 | { | |
123 | input => string output; | |
674e1c56 VS |
124 | replaceAll(output, "\\", "\\\\") => output; |
125 | replaceAll(output, "\n", "\\n") => output; | |
126 | replaceAll(output, "\"", "\\\"") => output; | |
e83d6df7 VS |
127 | return "\"" + output + "\""; |
128 | } | |
129 | } |