gmio_core: optimize gmio_clocale_isspace() with table of chars

This commit is contained in:
Hugues Delorme 2015-05-07 15:55:54 +02:00
parent 53bd9d08a6
commit 0702632c95
2 changed files with 36 additions and 0 deletions

View File

@ -21,6 +21,7 @@
/*! Returns non-zero if \p c is a space (for C-locale), zero otherwise */
GMIO_INLINE int gmio_clocale_isspace(char c)
{
#if 0
return c == 0x20 /* space (SPC) */
|| c == 0x09 /* horizontal tab (TAB) */
|| c == 0x0a /* newline (LF) */
@ -28,6 +29,28 @@ GMIO_INLINE int gmio_clocale_isspace(char c)
|| c == 0x0c /* feed (FF) */
|| c == 0x0d /* carriage return (CR) */
;
/* return c == 0x20 || ((uint8_t)(c - 0x09) < 5); */
#else
static const unsigned char space_chars[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
return space_chars[c];
#endif
}
/*! Returns true if \p c1 compare equals to \p c2

View File

@ -23,6 +23,7 @@
#include "../src/gmio_core/internal/fast_atof.h"
#include "../src/gmio_core/internal/safe_cast.h"
#include "../src/gmio_core/internal/string_parse.h"
#include "../src/gmio_core/internal/string_utils.h"
#include "stream_buffer.h"
#include "utils.h"
@ -202,6 +203,17 @@ const char* test_internal__string_parse()
return NULL;
}
const char* test_internal__string_utils()
{
UTEST_ASSERT(gmio_clocale_isspace(' '));
UTEST_ASSERT(gmio_clocale_isspace('\t'));
UTEST_ASSERT(gmio_clocale_isspace('\n'));
UTEST_ASSERT(gmio_clocale_isspace('\r'));
UTEST_ASSERT(!gmio_clocale_isspace('a'));
UTEST_ASSERT(!gmio_clocale_isspace('A'));
return NULL;
}
const char* all_tests()
{
UTEST_SUITE_START();
@ -210,6 +222,7 @@ const char* all_tests()
UTEST_RUN(test_internal__fast_atof);
UTEST_RUN(test_internal__safe_cast);
UTEST_RUN(test_internal__string_parse);
UTEST_RUN(test_internal__string_utils);
return NULL;
}