OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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 import 'package:charcode/ascii.dart'; |
| 6 |
| 7 /** |
| 8 * Returns a list of the words from which the given camel case [string] is |
| 9 * composed. |
| 10 * |
| 11 * 'getCamelWords' => ['get', 'Camel', 'Words'] |
| 12 * 'getHTMLText' => ['get', 'HTML', 'Text'] |
| 13 */ |
| 14 List<String> getCamelWords(String string) { |
| 15 if (string == null || string.isEmpty) { |
| 16 return const <String>[]; |
| 17 } |
| 18 List<String> parts = <String>[]; |
| 19 bool wasLowerCase = false; |
| 20 bool wasUpperCase = false; |
| 21 int wordStart = 0; |
| 22 for (int i = 0; i < string.length; i++) { |
| 23 int c = string.codeUnitAt(i); |
| 24 var newLowerCase = isLowerCase(c); |
| 25 var newUpperCase = isUpperCase(c); |
| 26 // myWord |
| 27 // | ^ |
| 28 if (wasLowerCase && newUpperCase) { |
| 29 parts.add(string.substring(wordStart, i)); |
| 30 wordStart = i; |
| 31 } |
| 32 // myHTMLText |
| 33 // | ^ |
| 34 if (wasUpperCase && |
| 35 newUpperCase && |
| 36 i + 1 < string.length && |
| 37 isLowerCase(string.codeUnitAt(i + 1))) { |
| 38 parts.add(string.substring(wordStart, i)); |
| 39 wordStart = i; |
| 40 } |
| 41 wasLowerCase = newLowerCase; |
| 42 wasUpperCase = newUpperCase; |
| 43 } |
| 44 parts.add(string.substring(wordStart)); |
| 45 return parts; |
| 46 } |
| 47 |
| 48 /** |
| 49 * Return `true` if the given [string] is either `null` or empty. |
| 50 */ |
| 51 bool isEmpty(String string) => string == null || string.isEmpty; |
| 52 |
| 53 /** |
| 54 * Return `true` if the given [character] is a lowercase ASCII character. |
| 55 */ |
| 56 bool isLowerCase(int character) => character >= $a && character <= $z; |
| 57 |
| 58 /** |
| 59 * Return `true` if the given [character] is an uppercase ASCII character. |
| 60 */ |
| 61 bool isUpperCase(int character) => character >= $A && character <= $Z; |
| 62 |
| 63 /** |
| 64 * If the given [string] starts with the text to [remove], then return the |
| 65 * portion of the string after the text to remove. Otherwise, return the |
| 66 * original string unmodified. |
| 67 */ |
| 68 String removeStart(String string, String remove) { |
| 69 if (isEmpty(string) || isEmpty(remove)) { |
| 70 return string; |
| 71 } |
| 72 if (string.startsWith(remove)) { |
| 73 return string.substring(remove.length); |
| 74 } |
| 75 return string; |
| 76 } |
OLD | NEW |