Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(323)

Side by Side Diff: samples/total/client/StringUtils.dart

Issue 10635015: Delete proxy and total samples, which have bit-rotted. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « samples/total/client/SpreadsheetPresenter.dart ('k') | samples/total/client/Style.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4
5 class StringUtils {
6
7 static final String _ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
8 static final String _BASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstu vwxyz0123456789+/";
9
10 // FIXME: replace getters below with static const fields.
11 static int get COMMA() { return _c(","); }
12 static int get E_LOWER() { return _c("e"); }
13 static int get E_UPPER() { return _c("E"); }
14 static int get MINUS() { return _c("-"); }
15 static int get NEWLINE() { return _c("\n"); }
16 static int get NINE() { return _c("9"); }
17 static int get PERIOD() { return _c("."); }
18 static int get PLUS() { return _c("+"); }
19 static int get QUOTE() { return _c("\""); }
20 static int get SEMICOLON() { return _c(";"); }
21 static int get ZERO() { return _c("0"); }
22
23 static String base64Encode(String data) {
24 StringBuffer sb = new StringBuffer();
25 int length = data.length;
26 for (int i = 0; i < length; i += 3) {
27 int c0 = data.charCodeAt(i) & 0xff;
28 int c1 = i + 1 < length ? data.charCodeAt(i + 1) & 0xff : 0;
29 int c2 = i + 2 < length ? data.charCodeAt(i + 2) & 0xff : 0;
30
31 int x0 = c0 >> 2;
32 int x1 = ((c0 << 4) | (c1 >> 4)) & 0x3f;
33 int x2 = ((c1 << 2) | (c2 >> 6)) & 0x3f;
34 int x3 = c2 & 0x3f;
35
36 sb.add(_BASE64[x0]);
37 sb.add(_BASE64[x1]);
38 sb.add(_BASE64[x2]);
39 sb.add(_BASE64[x3]);
40 }
41 return sb.toString();
42 }
43
44 // Return 'A', ..., 'Z', 'AA, 'AB', ..., 'ZZ', 'AAA', ...
45 // 'A' = 0, 'Z' = 25, 'AA' = 26, etc.
46 static String columnString(int c) {
47 if (c <= 0) {
48 throw new RuntimeException("c <= 0");
49 }
50
51 List<int> x = new List<int>(4);
52 int i = 0;
53 while (c > 0) {
54 c -= 1;
55 x[i++] = c % 26;
56 c ~/= 26;
57 }
58 StringBuffer sb = new StringBuffer();
59 while (i-- > 0) {
60 sb.add(_ALPHABET[x[i]]);
61 }
62 return sb.toString();
63 }
64
65 // Return a negative integer if s1 is lexically before s2, a positive integer if
66 // s1 is lexically after s2, or 0 if s1 and s2 are equal.
67 static int compare(String s1, String s2) {
68 int pos1 = 0;
69 int pos2 = 0;
70 int length = Math.min(s1.length, s2.length);
71 while (pos1 < length) {
72 int result;
73 if ((result = s1.charCodeAt(pos1++) - s2.charCodeAt(pos2++)) != 0) {
74 return result;
75 }
76 }
77 return s1.length - s2.length;
78 }
79
80 static String escapeStringLiteral(String s) {
81 StringBuffer sb = new StringBuffer();
82 int last = 0;
83 for (int i = 0; i < s.length; i++) {
84 switch (s[i]) {
85 case "\\":
86 sb.add(s.substring(last, i));
87 sb.add("\\\\");
88 last = i + 1;
89 break;
90 case "\"":
91 sb.add(s.substring(last, i));
92 sb.add("\\\"");
93 last = i + 1;
94 break;
95 }
96 }
97 sb.add(s.substring(last, s.length));
98 return sb.toString();
99 }
100
101 static bool isDigit(int s) => s >= ZERO && s <= NINE;
102
103 // Returns true if the given string matches ^[+-]?[0-9]*\.?[0-9]*([eE][+-]?[0- 9]+)?$
104 static bool isNumeric(String value) {
105 int len = value.length;
106 if (len == 0) {
107 return false;
108 }
109 bool gotDecimalPoint = false;
110 bool gotDigit = false;
111 int i = 0;
112 // Allow optional + or -
113 if (value.charCodeAt(i) == PLUS || value.charCodeAt(i) == MINUS) {
114 i++;
115 }
116 // Consume digits and "."'s
117 while (i < len && (isDigit(value.charCodeAt(i)) || value.charCodeAt(i) == PE RIOD)) {
118 if (value.charCodeAt(i) == PERIOD) {
119 if (gotDecimalPoint) {
120 return false;
121 }
122 gotDecimalPoint = true;
123 } else {
124 gotDigit = true;
125 }
126 i++;
127 }
128 // There must be at least one digit
129 if (!gotDigit) {
130 return false;
131 }
132 if (i < len && (value.charCodeAt(i) == E_UPPER
133 || value.charCodeAt(i) == E_LOWER)) {
134 i++;
135 // Allow optional + or -
136 if (i < len && (value.charCodeAt(i) == PLUS || value.charCodeAt(i) == MINU S)) {
137 i++;
138 }
139 bool gotExponent = false;
140 while (i < len && isDigit(value.charCodeAt(i))) {
141 gotExponent = true;
142 i++;
143 }
144 // There must be at least one digit in the exponent
145 if (!gotExponent) {
146 return false;
147 }
148 }
149 // The entire input must be consumed
150 if (i < len) {
151 return false;
152 }
153 return true;
154 }
155
156 // Return 1, 2, 3, ...
157 static String rowString(int r) {
158 if (r <= 0) {
159 throw new RuntimeException("r <= 0");
160 }
161 return r.toString();
162 }
163
164 /**
165 * Split a delimited string of the form "Stuff","Stuff,With,Commas",UnquotedSt uff
166 * Delimiters inside quotes are ignored.
167 */
168 static List<String> split(String s, int splitChar) {
169 List<String> out = new List<String>();
170 bool inQuote = false;
171 int start = 0;
172 for (int i = 0; i < s.length; i++) {
173 if (s.charCodeAt(i) == QUOTE) {
174 inQuote = !inQuote;
175 } else if ((s.charCodeAt(i) == splitChar) && !inQuote) {
176 out.add(s.substring(start, i));
177 start = i + 1;
178 }
179 }
180 out.add(s.substring(start, s.length));
181 return out;
182 }
183
184 /**
185 * Remove a pair of quotes from the ends of a string, if present.
186 */
187 static String stripQuotes(String s) {
188 int len = s.length;
189 if (len > 1 && s.charCodeAt(0) == QUOTE && s.charCodeAt(len - 1) == QUOTE) {
190 return s.substring(1, len - 1);
191 }
192 return s;
193 }
194
195 // Return "X", "X.Y", or "X.YZ", stripping trailing zeroes.
196 static String twoDecimals(double value) {
197 String v = value.toStringAsFixed(2);
198 if (v.endsWith(".00")) {
199 v = v.substring(0, v.length - 3);
200 } else if (v.endsWith("0")) {
201 v = v.substring(0, v.length - 1);
202 }
203 return v;
204 }
205
206 static String twoDigits(int x) {
207 String s = x.toString();
208 if (x < 10) {
209 return "0${s}";
210 } else if (x < 100) {
211 return s;
212 } else {
213 throw new RuntimeException("x >= 100");
214 }
215 }
216
217 static int _c(String s) => s.charCodeAt(0);
218 }
OLDNEW
« no previous file with comments | « samples/total/client/SpreadsheetPresenter.dart ('k') | samples/total/client/Style.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698