diff options
author | wkj <devnull@localhost> | 2004-04-06 19:06:52 +0000 |
---|---|---|
committer | wkj <devnull@localhost> | 2004-04-06 19:06:52 +0000 |
commit | 7cf289ca89a7416999ae02330236042b0d37e3db (patch) | |
tree | 796d1363a7a53c72c28b199758ee674f1326a510 /src/libhtml/strinttab.c | |
parent | 3e3817f7c86658f60715dd93768eaf8285807985 (diff) | |
download | plan9port-7cf289ca89a7416999ae02330236042b0d37e3db.tar.gz plan9port-7cf289ca89a7416999ae02330236042b0d37e3db.tar.bz2 plan9port-7cf289ca89a7416999ae02330236042b0d37e3db.zip |
Import version of libhtml that might actually work with ANSI C.
Diffstat (limited to 'src/libhtml/strinttab.c')
-rw-r--r-- | src/libhtml/strinttab.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/libhtml/strinttab.c b/src/libhtml/strinttab.c new file mode 100644 index 00000000..7883c044 --- /dev/null +++ b/src/libhtml/strinttab.c @@ -0,0 +1,64 @@ +#include <u.h> +#include <libc.h> +#include <draw.h> +#include <html.h> +#include "impl.h" + +// Do case-insensitive lookup of key[0:keylen] in t[0:n] (key part), +// returning 1 if found, 0 if not. +// Array t must be sorted in increasing lexicographic order of key. +// If found, return corresponding val in *pans. +int +_lookup(StringInt* t, int n, Rune* key, int keylen, int* pans) +{ + int min; + int max; + int try; + int cmpresult; + + min = 0; + max = n - 1; + while(min <= max) { + try = (min + max)/2; + cmpresult = _Strncmpci(key, keylen, t[try].key); + if(cmpresult > 0) + min = try + 1; + else if(cmpresult < 0) + max = try - 1; + else { + *pans = t[try].val; + return 1; + } + } + return 0; +} + +// Return first key in t[0:n] that corresponds to val, +// nil if none. +Rune* +_revlookup(StringInt* t, int n, int val) +{ + int i; + + for(i = 0; i < n; i++) + if(t[i].val == val) + return t[i].key; + return nil; +} + +// Make a StringInt table out of a[0:n], mapping each string +// to its index. Check that entries are in alphabetical order. +StringInt* +_makestrinttab(Rune** a, int n) +{ + StringInt* ans; + int i; + + ans = (StringInt*)emalloc(n * sizeof(StringInt)); + for(i = 0; i < n; i++) { + ans[i].key = a[i]; + ans[i].val = i; + assert(i == 0 || runestrcmp(a[i], a[i - 1]) >= 0); + } + return ans; +} |