Add an unsigned int/string pair type, modelled on the value string of
wireshark
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user