OLD | NEW |
| (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 /** | |
6 * General purpose string manipulation utilities. | |
7 */ | |
8 class StringUtils { | |
9 /** | |
10 * Returns either [str], or if [str] is null, the value of [defaultStr]. | |
11 */ | |
12 static String defaultString(String str, [String defaultStr='']) { | |
13 return str === null ? defaultStr : str; | |
14 } | |
15 | |
16 /** Parse string to a double, and handle null intelligently */ | |
17 static double parseDouble(String str, [double ifNull = null]) { | |
18 return (str == null) ? ifNull : Math.parseDouble(str); | |
19 } | |
20 | |
21 /** Parse string to a int, and handle null intelligently */ | |
22 static int parseInt(String str, [int ifNull = null]) { | |
23 return (str == null) ? ifNull : Math.parseInt(str); | |
24 } | |
25 | |
26 /** Parse bool to a double, and handle null intelligently */ | |
27 // TODO(jacobr): corelib should have a boolean parsing method | |
28 static bool parseBool(String str, [bool ifNull = null]) { | |
29 assert(str == null || str == 'true' || str == 'false'); | |
30 return (str == null) ? ifNull : (str == 'true'); | |
31 } | |
32 } | |
OLD | NEW |