1 /** 2 * 3 * 4 * License: BSD 3-clause 5 */ 6 module libguess_d.guess.common; 7 8 9 private static import libguess_d.dfa; 10 private static import libguess_d.encoding; 11 12 pure nothrow @safe @nogc 13 private void common_first_check(const char[] input, ref libguess_d.encoding.libguess_encoding rv) 14 15 do 16 { 17 static import libguess_d.dfa; 18 19 rv = libguess_d.dfa.check_UTF16_BOM(input); 20 } 21 22 /** 23 * 24 * 25 * Params: 26 * input = input string 27 * 28 * Returns: Character encoding or null 29 */ 30 pure nothrow @trusted @nogc 31 libguess_d.encoding.libguess_encoding guess_common(libguess_d.encoding.supported_lang lang)(const char[] input) 32 33 do 34 { 35 static import libguess_d.dfa; 36 static import libguess_d.encoding; 37 38 if (input.length == 0) { 39 return libguess_d.encoding.libguess_encoding.undefined; 40 } 41 42 static if (lang == libguess_d.encoding.supported_lang.ar) { 43 libguess_d.dfa.guess_dfa[3] order = [libguess_d.encoding.utf8, libguess_d.encoding.iso8859_6, libguess_d.encoding.cp1256]; 44 } else static if (lang == libguess_d.encoding.supported_lang.gr) { 45 libguess_d.dfa.guess_dfa[3] order = [libguess_d.encoding.utf8, libguess_d.encoding.iso8859_7, libguess_d.encoding.cp1253]; 46 } else static if (lang == libguess_d.encoding.supported_lang.hw) { 47 libguess_d.dfa.guess_dfa[3] order = [libguess_d.encoding.utf8, libguess_d.encoding.iso8859_8, libguess_d.encoding.cp1255]; 48 } else static if (lang == libguess_d.encoding.supported_lang.tr) { 49 libguess_d.dfa.guess_dfa[3] order = [libguess_d.encoding.utf8, libguess_d.encoding.iso8859_9, libguess_d.encoding.cp1254]; 50 } else { 51 static assert(0); 52 } 53 54 libguess_d.encoding.libguess_encoding rv = libguess_d.encoding.libguess_encoding.undefined; 55 56 .common_first_check(input, rv); 57 58 if (rv != libguess_d.encoding.libguess_encoding.undefined) { 59 return rv; 60 } 61 62 for (size_t i = 0; i < input.length; i++) { 63 ubyte c = cast(ubyte)(input[i]); 64 65 rv = libguess_d.dfa.dfa_process(order, c); 66 67 if (rv != libguess_d.encoding.libguess_encoding.undefined) { 68 return rv; 69 } 70 71 if (libguess_d.dfa.dfa_none(order)) { 72 /* we ran out the possibilities */ 73 return libguess_d.encoding.libguess_encoding.undefined; 74 } 75 } 76 77 rv = libguess_d.dfa.dfa_top(order); 78 79 return (rv != libguess_d.encoding.libguess_encoding.undefined) ? (rv) : (libguess_d.encoding.libguess_encoding.undefined); 80 } 81 82 alias guess_ar = .guess_common!(libguess_d.encoding.supported_lang.ar); 83 alias guess_gr = .guess_common!(libguess_d.encoding.supported_lang.gr); 84 alias guess_hw = .guess_common!(libguess_d.encoding.supported_lang.hw); 85 alias guess_tr = .guess_common!(libguess_d.encoding.supported_lang.tr);