Add a string/string mapper struct

This commit is contained in:
Nils O. Selåsdal
2014-02-04 20:51:47 +01:00
parent 39290eefb7
commit 0f25a0f565
3 changed files with 53 additions and 6 deletions
+20 -3
View File
@@ -1,4 +1,4 @@
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "ucore/val_str.h"
#include "ucore/saturating_math.h"
@@ -25,13 +25,15 @@ const char *uc_val_2_str_bs(unsigned int val,
{
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) {
return curr->str;
found = curr->str;
break;
}
if (curr->val < val) {
@@ -42,6 +44,21 @@ const char *uc_val_2_str_bs(unsigned int val,
i = i / 2;
}
return NULL;
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;
}