1 module tame.misc; 2 3 import std.traits; 4 5 /// Returns: true if an aray has an element 6 bool hasElement(T)(T[] array, T element) { 7 bool r; 8 foreach (cur; array) { 9 if (cur == element) { 10 r = true; 11 break; 12 } 13 } 14 return r; 15 } 16 /// 17 unittest { 18 assert([0, 1, 2].hasElement(2)); 19 assert(![0, 1, 2].hasElement(4)); 20 } 21 22 /// Returns: true if a string is a number 23 auto isNum(string s, bool allowDecimalPoint = true) { 24 if (!s.length) 25 return false; 26 bool hasDecimalPoint = !allowDecimalPoint; 27 if (s[0] == '-') 28 s = s[1 .. $]; 29 foreach (c; s) { 30 if (c == '.' && !hasDecimalPoint) { 31 hasDecimalPoint = true; 32 } else if (c < '0' || c > '9') 33 return false; 34 } 35 return true; 36 } 37 38 /// Returns: true if all characters in a string are alphabets, uppercase, lowercase, or both 39 auto isAlphabet(string s) { 40 foreach (c; s) { 41 if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z')) 42 return false; 43 } 44 return true; 45 } 46 /// 47 unittest { 48 assert("aBcDEf".isAlphabet == true); 49 assert("ABCd_".isAlphabet == false); 50 assert("ABC12".isAlphabet == false); 51 } 52 53 /// Returns: true if the string starts with a white character 54 bool startsWithWhite(S)(S s) if (isArray!S) { 55 import std.ascii; 56 57 return s.length && s[0].isWhite; 58 }