Skip to content
Snippets Groups Projects
Commit 7587bd04 authored by Jan Včelák's avatar Jan Včelák :rocket:
Browse files

util/tolower: remove magic and add knot_strtolower()

parent 4c15545e
No related branches found
No related tags found
No related merge requests found
......@@ -17,7 +17,7 @@
#include <config.h>
#include "util/tolower.h"
const uint8_t char_table[CHAR_TABLE_SIZE] = {
const uint8_t char_table[KNOT_CHAR_TABLE_SIZE] = {
'\x00',
'\x01',
'\x02',
......
......@@ -28,32 +28,46 @@
#define _KNOT_TOLOWER_H_
#include <stdint.h>
#include <stdlib.h>
/*! \brief Size of the character conversion table. */
#define KNOT_CHAR_TABLE_SIZE 256
enum {
/*! \brief Size of the character conversion table. */
CHAR_TABLE_SIZE = KNOT_CHAR_TABLE_SIZE
};
#define KNOT_CHAR_TABLE_SIZE (UINT8_MAX + 1)
/*! \brief Character table mapping uppercase letters to lowercase. */
extern const uint8_t char_table[CHAR_TABLE_SIZE];
extern const uint8_t char_table[KNOT_CHAR_TABLE_SIZE];
/*!
* \brief Converts ASCII character to lowercase.
* \brief Converts binary character to lowercase.
*
* \param c ASCII character code.
* \param c Character code.
*
* \return \a c converted to lowercase (or \a c if not applicable).
*/
static inline uint8_t knot_tolower(uint8_t c) {
#if KNOT_CHAR_TABLE_SIZE < 256
assert(c < CHAR_TABLE_SIZE);
#endif
return char_table[c];
}
/*!
* \brief Convert binary data to lowercase (if lowercase equivalent exists).
*
* \param data Binary input string.
* \param size Size of the input string
*
* \return Lowercase representation of the input string.
*/
static inline uint8_t *knot_strtolower(const uint8_t *data, size_t size)
{
uint8_t *result = (uint8_t *)malloc(size);
if (!result)
return NULL;
for (size_t i = 0; i < size; ++i) {
result[i] = knot_tolower(data[i]);
}
return result;
}
#endif /* _KNOT_TOLOWER_H_ */
/*! @} */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment