Files
libucore/src/val_str.c
T
2014-09-12 01:11:39 +02:00

65 lines
1.2 KiB
C

#include <string.h>
#include <limits.h>
#include "ucore/val_str.h"
#include "ucore/saturating_math.h"
const char *uc_val_2_str(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_val_2_str_bs(unsigned int val,
const struct UCValStr *array,
size_t array_len)
{
size_t i = array_len;
const struct UCValStr *start = array;
const char *found = NULL;
while (i != 0) {
const struct UCValStr *curr;
curr = &start[i / 2]; //middle
if (curr->val == val) {
found = curr->str;
break;
}
if (curr->val < val) {
start = curr + 1; //right half
i--;
}
i = i / 2;
}
return found;
}
const char *uc_str_2_str(const char *val, const struct UCStrStr *array)
{
const char *found = NULL;
while (array->val) {
if (strcmp(val, array->val) == 0) {
found = array->str;
break;
}
array++;
}
return found;
}