1 module hunt.sql.util.Utils; 2 3 import std.conv; 4 import std.algorithm.searching; 5 import hunt.collection; 6 import std.file; 7 import std.stdio; 8 import std.string; 9 10 int search(T)(T[] ts, T t) 11 { 12 if(!ts.canFind(t)) 13 return -1; 14 foreach(size_t idx, T tm ; ts) 15 { 16 if(tm == t) 17 return cast(int)idx; 18 } 19 return -1; 20 } 21 22 class Utils 23 { 24 public static string hex_t(long hash) { 25 byte[] bytes = new byte[8]; 26 27 bytes[7] = cast(byte) (hash ); 28 bytes[6] = cast(byte) (hash >>> 8); 29 bytes[5] = cast(byte) (hash >>> 16); 30 bytes[4] = cast(byte) (hash >>> 24); 31 bytes[3] = cast(byte) (hash >>> 32); 32 bytes[2] = cast(byte) (hash >>> 40); 33 bytes[1] = cast(byte) (hash >>> 48); 34 bytes[0] = cast(byte) (hash >>> 56); 35 36 char[] chars = new char[18]; 37 chars[0] = 'T'; 38 chars[1] = '_'; 39 for (int i = 0; i < 8; ++i) { 40 byte b = bytes[i]; 41 42 int a = b & 0xFF; 43 int b0 = a >> 4; 44 int b1 = a & 0xf; 45 46 chars[i * 2 + 2] = cast(char) (b0 + (b0 < 10 ? 48 : 55)); 47 chars[i * 2 + 3] = cast(char) (b1 + (b1 < 10 ? 48 : 55)); 48 } 49 50 return to!string(chars); 51 } 52 53 public static void putLong(byte[] b, int off, long val) { 54 b[off + 7] = cast(byte) (val >>> 0); 55 b[off + 6] = cast(byte) (val >>> 8); 56 b[off + 5] = cast(byte) (val >>> 16); 57 b[off + 4] = cast(byte) (val >>> 24); 58 b[off + 3] = cast(byte) (val >>> 32); 59 b[off + 2] = cast(byte) (val >>> 40); 60 b[off + 1] = cast(byte) (val >>> 48); 61 b[off + 0] = cast(byte) (val >>> 56); 62 } 63 64 public static bool equals(Object a, Object b) { 65 return (a == b) || (a !is null && a.opEquals(b)); 66 } 67 68 69 public static bool matches(string pattern,string input) 70 { 71 import std.regex; 72 auto res = matchFirst(input , regex(pattern)); 73 if (!res.empty) 74 { 75 if(res.front.length == input.length) 76 return true; 77 } 78 79 return false; 80 } 81 82 public static void loadFromFile(string path, Set!string set) { 83 try { 84 85 auto reader = File(path,"rb"); 86 foreach (line ; reader.byLine() ) { 87 88 auto keywords = toLower(strip(cast(string)line)); 89 90 if (keywords.length == 0) { 91 continue; 92 } 93 set.add(keywords); 94 } 95 } catch (Exception ex) { 96 // skip 97 } finally { 98 } 99 } 100 101 } 102