Files
libucore/include/ucore/val_str.h
T
2014-09-12 01:11:40 +02:00

54 lines
1.3 KiB
C

#ifndef UC_VAL_STR_H_
#define UC_VAL_STR_H_
#include <stddef.h>
struct UCValStr {
unsigned int val;
const char *str;
};
struct UCStrStr {
const char *val;
const char *str;
};
/** Helper macro to define an entry in a UCValStr array */
#define UC_VS_ENTRY(constant) {constant, #constant}
#define UC_VS_TERMINATOR {-1, NULL}
#define UC_SS_TERMINATOR {NULL, NULL}
/** find @val in @array.
* the last .str in @array must be a null pointer.
* (does a linear search)
*
* @param val value to find
* @param array array to search
* @return the string associated with @val or null
*/
const char *uc_val_2_str(unsigned int val, const struct UCValStr *array);
/** Find @val in @array.
* @array must be sorted on val and must not be empty
* (does a binary search)
*
* @param val value to find
* @param array array to search
* @return the string associated with @val or NULL
*/
const char *uc_val_2_str_bs(unsigned int val,
const struct UCValStr *array,
size_t array_len);
/** find @val in @array.
* the last .val in @array must be a null pointer.
* (does a linear search)
*
* @param val value to find, using strcmp
* @param array array to search
* @return the string associated with @val or null
*/
const char *uc_str_2_str(const char *restrict val, const struct UCStrStr *array);
#endif