Add an unsigned int/string pair type, modelled on the value string of

wireshark
This commit is contained in:
Nils O. Selåsdal
2014-02-04 01:37:27 +01:00
parent 5756ccfbef
commit dfe219b649
4 changed files with 159 additions and 1 deletions
+47
View File
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <limits.h>
#include "ucore/val_str.h"
#include "ucore/saturating_math.h"
const char *uc_vs_search(unsigned int val, const struct UCValStr *array)
{
const char *found = NULL;
while (array->str) {
if (array->val == val) {
found = array->str;
break;
}
array++;
}
return found;
}
const char *uc_vs_bsearch(unsigned int val,
const struct UCValStr *array,
size_t array_len)
{
size_t i = array_len;
const struct UCValStr *start = array;
while (i != 0) {
const struct UCValStr *curr;
curr = start + i / 2;
if (curr->val == val) {
return curr->str;
}
if (curr->val < val) {
start = curr + 1;
i--;
}
i = i / 2;
}
return NULL;
}