Mercurial > projects > dwt-addons
annotate dwtx/jface/util/Util.d @ 70:46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 22 May 2008 01:36:46 +0200 |
parents | e0f0aaf75edd |
children | 5df4896124c7 |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
2 * Copyright (c) 2000, 2007 IBM Corporation and others. |
10 | 3 * All rights reserved. This program and the accompanying materials |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 | |
14 module dwtx.jface.util.Util; | |
15 | |
16 // import java.util.Collections; | |
17 // import java.util.List; | |
18 // import java.util.MissingResourceException; | |
19 // import java.util.ResourceBundle; | |
20 // import java.util.SortedSet; | |
21 // import java.util.TreeSet; | |
22 | |
23 import dwt.dwthelper.utils; | |
24 import dwt.dwthelper.ResourceBundle; | |
25 import tango.core.Exception; | |
26 static import tango.text.Text; | |
27 alias tango.text.Text.Text!(char) StringBuffer; | |
28 private extern(C) int _d_isbaseof(ClassInfo *b, ClassInfo *c); | |
29 | |
30 /** | |
31 * <p> | |
32 * A static class providing utility methods to all of JFace. | |
33 * </p> | |
34 * | |
35 * @since 3.1 | |
36 */ | |
37 public final class Util { | |
38 | |
39 // /** | |
40 // * An unmodifiable, empty, sorted set. This value is guaranteed to never | |
41 // * change and never be <code>null</code>. | |
42 // */ | |
43 // public static final SortedSet EMPTY_SORTED_SET = Collections | |
44 // .unmodifiableSortedSet(new TreeSet()); | |
45 | |
46 /** | |
47 * A common zero-length string. It avoids needing write <code>NON-NLS</code> | |
48 * next to code fragments. It's also a bit clearer to read. | |
49 */ | |
50 public static final String ZERO_LENGTH_STRING = ""; //$NON-NLS-1$ | |
51 | |
52 /** | |
53 * Verifies that the given object is an instance of the given class. | |
54 * | |
55 * @param object | |
56 * The object to check; may be <code>null</code>. | |
57 * @param c | |
58 * The class which the object should be; must not be | |
59 * <code>null</code>. | |
60 */ | |
61 public static final void assertInstance(Object object, ClassInfo c) { | |
62 assertInstance(object, c, false); | |
63 } | |
64 | |
65 /** | |
66 * Verifies the given object is an instance of the given class. It is | |
67 * possible to specify whether the object is permitted to be | |
68 * <code>null</code>. | |
69 * | |
70 * @param object | |
71 * The object to check; may be <code>null</code>. | |
72 * @param c | |
73 * The class which the object should be; must not be | |
74 * <code>null</code>. | |
75 * @param allowNull | |
76 * Whether the object is allowed to be <code>null</code>. | |
77 */ | |
78 private static final void assertInstance(Object object, | |
79 ClassInfo c, bool allowNull) { | |
80 if (object is null && allowNull) { | |
81 return; | |
82 } | |
83 | |
84 if (object is null || c is null) { | |
85 throw new NullPointerException(); | |
86 } else if (!_d_isbaseof( &object.classinfo, &c ) ) { | |
87 throw new IllegalArgumentException(null); | |
88 } | |
89 } | |
90 | |
91 /** | |
92 * Compares two bool values. <code>false</code> is considered to be | |
93 * "less than" <code>true</code>. | |
94 * | |
95 * @param left | |
96 * The left value to compare | |
97 * @param right | |
98 * The right value to compare | |
99 * @return <code>-1</code> if the left is <code>false</code> and the | |
100 * right is <code>true</code>. <code>1</code> if the opposite | |
101 * is true. If they are equal, then it returns <code>0</code>. | |
102 */ | |
103 public static final int compare(bool left, bool right) { | |
104 return left is false ? (right is true ? -1 : 0) : 1; | |
105 } | |
106 | |
107 /** | |
108 * Compares two integer values. | |
109 * | |
110 * @param left | |
111 * The left value to compare | |
112 * @param right | |
113 * The right value to compare | |
114 * @return <code>left - right</code> | |
115 */ | |
116 public static final int compare(int left, int right) { | |
117 return left - right; | |
118 } | |
119 | |
120 /** | |
121 * Compares to comparable objects -- defending against <code>null</code>. | |
122 * | |
123 * @param left | |
124 * The left object to compare; may be <code>null</code>. | |
125 * @param right | |
126 * The right object to compare; may be <code>null</code>. | |
127 * @return The result of the comparison. <code>null</code> is considered | |
128 * to be the least possible value. | |
129 */ | |
130 public static final int compare(Comparable left, | |
131 Comparable right) { | |
132 if (left is null && right is null) { | |
133 return 0; | |
134 } else if (left is null) { | |
135 return -1; | |
136 } else if (right is null) { | |
137 return 1; | |
138 } else { | |
139 return left.compareTo(cast(Object)right); | |
140 } | |
141 } | |
142 | |
143 /** | |
144 * Compares two arrays of comparable objects -- accounting for | |
145 * <code>null</code>. | |
146 * | |
147 * @param left | |
148 * The left array to be compared; may be <code>null</code>. | |
149 * @param right | |
150 * The right array to be compared; may be <code>null</code>. | |
151 * @return The result of the comparison. <code>null</code> is considered | |
152 * to be the least possible value. A shorter array is considered | |
153 * less than a longer array. | |
154 */ | |
155 public static final int compare(Comparable[] left, | |
156 Comparable[] right) { | |
157 if (left is null && right is null) { | |
158 return 0; | |
159 } else if (left is null) { | |
160 return -1; | |
161 } else if (right is null) { | |
162 return 1; | |
163 } else { | |
164 int l = left.length; | |
165 int r = right.length; | |
166 | |
167 if (l !is r) { | |
168 return l - r; | |
169 } | |
170 | |
171 for (int i = 0; i < l; i++) { | |
172 int compareTo = compare(left[i], right[i]); | |
173 | |
174 if (compareTo !is 0) { | |
175 return compareTo; | |
176 } | |
177 } | |
178 | |
179 return 0; | |
180 } | |
181 } | |
16
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
182 public static final int compare(String left, String right) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
183 return left < right; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
184 } |
10 | 185 |
186 // /** | |
187 // * Compares two lists -- account for <code>null</code>. The lists must | |
188 // * contain comparable objects. | |
189 // * | |
190 // * @param left | |
191 // * The left list to compare; may be <code>null</code>. This | |
192 // * list must only contain instances of <code>Comparable</code>. | |
193 // * @param right | |
194 // * The right list to compare; may be <code>null</code>. This | |
195 // * list must only contain instances of <code>Comparable</code>. | |
196 // * @return The result of the comparison. <code>null</code> is considered | |
197 // * to be the least possible value. A shorter list is considered less | |
198 // * than a longer list. | |
199 // */ | |
200 // public static final int compare(SeqView!(Object) left, SeqView!(Object) right) { | |
201 // if (left is null && right is null) { | |
202 // return 0; | |
203 // } else if (left is null) { | |
204 // return -1; | |
205 // } else if (right is null) { | |
206 // return 1; | |
207 // } else { | |
208 // int l = left.size(); | |
209 // int r = right.size(); | |
210 // | |
211 // if (l !is r) { | |
212 // return l - r; | |
213 // } | |
214 // | |
215 // for (int i = 0; i < l; i++) { | |
216 // int compareTo = compare((Comparable) left.get(i), | |
217 // (Comparable) right.get(i)); | |
218 // | |
219 // if (compareTo !is 0) { | |
220 // return compareTo; | |
221 // } | |
222 // } | |
223 // | |
224 // return 0; | |
225 // } | |
226 // } | |
227 | |
228 /** | |
229 * Tests whether the first array ends with the second array. | |
230 * | |
231 * @param left | |
232 * The array to check (larger); may be <code>null</code>. | |
233 * @param right | |
234 * The array that should be a subsequence (smaller); may be | |
235 * <code>null</code>. | |
236 * @param equals | |
237 * Whether the two array are allowed to be equal. | |
238 * @return <code>true</code> if the second array is a subsequence of the | |
239 * array list, and they share end elements. | |
240 */ | |
241 public static final bool endsWith(Object[] left, | |
242 Object[] right, bool equals) { | |
243 if (left is null || right is null) { | |
244 return false; | |
245 } | |
246 | |
247 int l = left.length; | |
248 int r = right.length; | |
249 | |
250 if (r > l || !equals && r is l) { | |
251 return false; | |
252 } | |
253 | |
254 for (int i = 0; i < r; i++) { | |
255 if (!Util.opEquals(left[l - i - 1], right[r - i - 1])) { | |
256 return false; | |
257 } | |
258 } | |
259 | |
260 return true; | |
261 } | |
262 | |
263 /** | |
264 * Checks whether the two objects are <code>null</code> -- allowing for | |
265 * <code>null</code>. | |
266 * | |
267 * @param left | |
268 * The left object to compare; may be <code>null</code>. | |
269 * @param right | |
270 * The right object to compare; may be <code>null</code>. | |
271 * @return <code>true</code> if the two objects are equivalent; | |
272 * <code>false</code> otherwise. | |
273 */ | |
274 public static final bool opEquals(Object left, Object right) { | |
275 return left is null ? right is null : ((right !is null) && left | |
276 .opEquals(right)); | |
277 } | |
16
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
278 public static final bool opEquals(String left, String right) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
279 return left == right; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
280 } |
10 | 281 |
282 /** | |
283 * Tests whether two arrays of objects are equal to each other. The arrays | |
284 * must not be <code>null</code>, but their elements may be | |
285 * <code>null</code>. | |
286 * | |
287 * @param leftArray | |
288 * The left array to compare; may be <code>null</code>, and | |
289 * may be empty and may contain <code>null</code> elements. | |
290 * @param rightArray | |
291 * The right array to compare; may be <code>null</code>, and | |
292 * may be empty and may contain <code>null</code> elements. | |
293 * @return <code>true</code> if the arrays are equal length and the | |
294 * elements at the same position are equal; <code>false</code> | |
295 * otherwise. | |
296 */ | |
297 public static final bool opEquals(Object[] leftArray, | |
298 Object[] rightArray) { | |
299 if (leftArray is rightArray) { | |
300 return true; | |
301 } | |
302 | |
303 if (leftArray is null) { | |
304 return (rightArray is null); | |
305 } else if (rightArray is null) { | |
306 return false; | |
307 } | |
308 | |
309 if (leftArray.length !is rightArray.length) { | |
310 return false; | |
311 } | |
312 | |
313 for (int i = 0; i < leftArray.length; i++) { | |
314 Object left = leftArray[i]; | |
315 Object right = rightArray[i]; | |
316 bool equal = ((left is null) ? (right is null) : (left.opEquals(right))) !is 0; | |
317 if (!equal) { | |
318 return false; | |
319 } | |
320 } | |
321 | |
322 return true; | |
323 } | |
324 | |
16
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
325 public static final bool opEquals(String[] leftArray, String[] rightArray) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
326 if (leftArray.length !is rightArray.length) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
327 return false; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
328 } |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
329 |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
330 for (int i = 0; i < leftArray.length; i++) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
331 String left = leftArray[i]; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
332 String right = rightArray[i]; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
333 if (left != right) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
334 return false; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
335 } |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
336 } |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
337 |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
338 return true; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
339 } |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
340 |
10 | 341 /** |
342 * Provides a hash code based on the given integer value. | |
343 * | |
344 * @param i | |
345 * The integer value | |
346 * @return <code>i</code> | |
347 */ | |
348 public static final hash_t toHash(int i) { | |
349 return i; | |
350 } | |
351 | |
352 /** | |
353 * Provides a hash code for the object -- defending against | |
354 * <code>null</code>. | |
355 * | |
356 * @param object | |
357 * The object for which a hash code is required. | |
358 * @return <code>object.hashCode</code> or <code>0</code> if | |
359 * <code>object</code> if <code>null</code>. | |
360 */ | |
361 public static final hash_t toHash( Object object) { | |
362 return object !is null ? object.toHash() : 0; | |
363 } | |
364 | |
365 /** | |
366 * Computes the hash code for an array of objects, but with defense against | |
367 * <code>null</code>. | |
368 * | |
369 * @param objects | |
370 * The array of objects for which a hash code is needed; may be | |
371 * <code>null</code>. | |
372 * @return The hash code for <code>objects</code>; or <code>0</code> if | |
373 * <code>objects</code> is <code>null</code>. | |
374 */ | |
375 public static final hash_t toHash(Object[] objects) { | |
376 if (objects is null) { | |
377 return 0; | |
378 } | |
379 | |
380 int hashCode = 89; | |
381 for (int i = 0; i < objects.length; i++) { | |
382 final Object object = objects[i]; | |
383 if (object !is null) { | |
384 hashCode = hashCode * 31 + object.toHash(); | |
385 } | |
386 } | |
387 | |
388 return hashCode; | |
389 } | |
16
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
390 public static final hash_t toHash(String str) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
391 return dwt.dwthelper.utils.toHash(str); |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
392 } |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
393 public static final hash_t toHash(String[] objects) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
394 int hashCode = 89; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
395 for (int i = 0; i < objects.length; i++) { |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
396 auto object = objects[i]; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
397 hashCode = hashCode * 31 + toHash(object); |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
398 } |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
399 |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
400 return hashCode; |
e0f0aaf75edd
PopupDialog, bindings and actions
Frank Benoit <benoit@tionex.de>
parents:
10
diff
changeset
|
401 } |
10 | 402 |
403 /** | |
404 * Checks whether the second array is a subsequence of the first array, and | |
405 * that they share common starting elements. | |
406 * | |
407 * @param left | |
408 * The first array to compare (large); may be <code>null</code>. | |
409 * @param right | |
410 * The second array to compare (small); may be <code>null</code>. | |
411 * @param equals | |
412 * Whether it is allowed for the two arrays to be equivalent. | |
413 * @return <code>true</code> if the first arrays starts with the second | |
414 * list; <code>false</code> otherwise. | |
415 */ | |
416 public static final bool startsWith(Object[] left, | |
417 Object[] right, bool equals) { | |
418 if (left is null || right is null) { | |
419 return false; | |
420 } | |
421 | |
422 int l = left.length; | |
423 int r = right.length; | |
424 | |
425 if (r > l || !equals && r is l) { | |
426 return false; | |
427 } | |
428 | |
429 for (int i = 0; i < r; i++) { | |
430 if (!opEquals(left[i], right[i])) { | |
431 return false; | |
432 } | |
433 } | |
434 | |
435 return true; | |
436 } | |
437 | |
438 /** | |
439 * Converts an array into a string representation that is suitable for | |
440 * debugging. | |
441 * | |
442 * @param array | |
443 * The array to convert; may be <code>null</code>. | |
444 * @return The string representation of the array; never <code>null</code>. | |
445 */ | |
446 public static final String toString(Object[] array) { | |
447 if (array is null) { | |
448 return "null"; //$NON-NLS-1$ | |
449 } | |
450 | |
451 final StringBuffer buffer = new StringBuffer(); | |
452 buffer.append('['); | |
453 | |
454 final int length = array.length; | |
455 for (int i = 0; i < length; i++) { | |
456 if (i !is 0) { | |
457 buffer.append(','); | |
458 } | |
459 Object object = array[i]; | |
460 String element = (object is null ) ? "null" : object.toString; | |
461 buffer.append(element); | |
462 } | |
463 buffer.append(']'); | |
464 | |
465 return buffer.toString(); | |
466 } | |
467 | |
468 /** | |
469 * Provides a translation of a particular key from the resource bundle. | |
470 * | |
471 * @param resourceBundle | |
472 * The key to look up in the resource bundle; should not be | |
473 * <code>null</code>. | |
474 * @param key | |
475 * The key to look up in the resource bundle; should not be | |
476 * <code>null</code>. | |
477 * @param defaultString | |
478 * The value to return if the resource cannot be found; may be | |
479 * <code>null</code>. | |
480 * @return The value of the translated resource at <code>key</code>. If | |
481 * the key cannot be found, then it is simply the | |
482 * <code>defaultString</code>. | |
483 */ | |
484 public static final String translateString( | |
485 ResourceBundle resourceBundle, String key, | |
486 String defaultString) { | |
487 if (resourceBundle !is null && key !is null) { | |
488 try { | |
489 String translatedString = resourceBundle.getString(key); | |
490 | |
491 if (translatedString !is null) { | |
492 return translatedString; | |
493 } | |
494 } catch (MissingResourceException eMissingResource) { | |
495 // Such is life. We'll return the key | |
496 } | |
497 } | |
498 | |
499 return defaultString; | |
500 } | |
70
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
501 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
502 /** |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
503 * Foundation replacement for String.replaceAll(*). |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
504 * |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
505 * @param src the starting string. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
506 * @param find the string to find. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
507 * @param replacement the string to replace. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
508 * @return The new string. |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
509 * @since 3.4 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
510 */ |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
511 public static final String replaceAll(String src, String find, String replacement) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
512 final int len = src.length(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
513 final int findLen = find.length(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
514 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
515 int idx = src.indexOf(find); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
516 if (idx < 0) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
517 return src; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
518 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
519 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
520 StringBuffer buf = new StringBuffer(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
521 int beginIndex = 0; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
522 while (idx !is -1 && idx < len) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
523 buf.append(src.substring(beginIndex, idx)); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
524 buf.append(replacement); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
525 |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
526 beginIndex = idx + findLen; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
527 if (beginIndex < len) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
528 idx = src.indexOf(find, beginIndex); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
529 } else { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
530 idx = -1; |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
531 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
532 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
533 if (beginIndex<len) { |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
534 buf.append(src.substring(beginIndex, (idxis-1?len:idx))); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
535 } |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
536 return buf.toString(); |
46a6e0e6ccd4
Merge with d-fied sources of 3.4M7
Frank Benoit <benoit@tionex.de>
parents:
16
diff
changeset
|
537 } |
10 | 538 |
539 /** | |
540 * This class should never be constructed. | |
541 */ | |
542 private this() { | |
543 // Not allowed. | |
544 } | |
545 } |