Generic binary search lookup in some keyword table. `word' to be searched must be NUL-terminated C string. `table' is array of const char * of `size' sorted alphabetically. Returns word found (i.e., pointer value in table) or 0. */
| 52 | Returns word found (i.e., pointer value in table) or 0. |
| 53 | */ |
| 54 | const char *is_keyword(const char *word, |
| 55 | const char *table[], unsigned size) |
| 56 | { |
| 57 | int i = 0, j = size; |
| 58 | while (i < j) { |
| 59 | int k = (i + j) >> 1 /* / 2 */; |
| 60 | int cmp = strcmp(word, table[k]); |
| 61 | if (!cmp) |
| 62 | return table[k]; |
| 63 | if (cmp < 0) j = k; else i = k + 1; |
| 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | /* Must be called right after a file is opened as stdin. |
| 69 | Will attempt to remove any UTF-8 unicode signature (byte-order mark, BOM) |
no outgoing calls
no test coverage detected