OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * General purpose string manipulation utilities. | 6 * General purpose string manipulation utilities. |
7 */ | 7 */ |
8 class StringUtils { | 8 class StringUtils { |
9 /** | 9 /** |
10 * Returns either [str], or if [str] is null, the value of [defaultStr]. | 10 * Returns either [str], or if [str] is null, the value of [defaultStr]. |
11 */ | 11 */ |
12 static String defaultString(String str, [String defaultStr='']) { | 12 static String defaultString(String str, [String defaultStr='']) { |
13 return str === null ? defaultStr : str; | 13 return str == null ? defaultStr : str; |
14 } | 14 } |
15 | 15 |
16 /** Parse string to a double, and handle null intelligently */ | 16 /** Parse string to a double, and handle null intelligently */ |
17 static double parseDouble(String str, [double ifNull = null]) { | 17 static double parseDouble(String str, [double ifNull = null]) { |
18 return (str == null) ? ifNull : double.parse(str); | 18 return (str == null) ? ifNull : double.parse(str); |
19 } | 19 } |
20 | 20 |
21 /** Parse string to a int, and handle null intelligently */ | 21 /** Parse string to a int, and handle null intelligently */ |
22 static int parseInt(String str, [int ifNull = null]) { | 22 static int parseInt(String str, [int ifNull = null]) { |
23 return (str == null) ? ifNull : int.parse(str); | 23 return (str == null) ? ifNull : int.parse(str); |
24 } | 24 } |
25 | 25 |
26 /** Parse bool to a double, and handle null intelligently */ | 26 /** Parse bool to a double, and handle null intelligently */ |
27 // TODO(jacobr): corelib should have a boolean parsing method | 27 // TODO(jacobr): corelib should have a boolean parsing method |
28 static bool parseBool(String str, [bool ifNull = null]) { | 28 static bool parseBool(String str, [bool ifNull = null]) { |
29 assert(str == null || str == 'true' || str == 'false'); | 29 assert(str == null || str == 'true' || str == 'false'); |
30 return (str == null) ? ifNull : (str == 'true'); | 30 return (str == null) ? ifNull : (str == 'true'); |
31 } | 31 } |
32 } | 32 } |
OLD | NEW |