From 6c11e23f7a562214c4681ca9a78c893be27824ba Mon Sep 17 00:00:00 2001 From: Roman Tsisyk <roman@tarantool.org> Date: Tue, 20 Jun 2017 11:27:41 +0300 Subject: [PATCH] Move fpconv from lua/utils.h to util.h No semantic changes. Needed for #128 --- src/lua/utils.c | 24 --------------------- src/lua/utils.h | 28 ------------------------- src/main.cc | 1 + src/trivia/util.h | 35 +++++++++++++++++++++++++++++++ src/util.c | 23 ++++++++++++++++++++ third_party/lua-cjson/lua_cjson.c | 2 ++ 6 files changed, 61 insertions(+), 52 deletions(-) diff --git a/src/lua/utils.c b/src/lua/utils.c index f4990e4524..cc66b2c6ed 100644 --- a/src/lua/utils.c +++ b/src/lua/utils.c @@ -653,29 +653,6 @@ luaL_convertfield(struct lua_State *L, struct luaL_serializer *cfg, int idx, lua_typename(L, lua_type(L, idx))); } -const char *precision_fmts[] = { - "%.0lg", "%.1lg", "%.2lg", "%.3lg", "%.4lg", "%.5lg", "%.6lg", "%.7lg", - "%.8lg", "%.9lg", "%.10lg", "%.11lg", "%.12lg", "%.13lg", "%.14lg" -}; - -static void -fpconv_init() -{ - char buf[8]; - - snprintf(buf, sizeof(buf), "%g", 0.5); - - /* Failing this test might imply the platform has a buggy dtoa - * implementation or wide characters */ - assert(buf[0] == '0' && buf[2] == '5' && buf[3] == 0); - - /* - * Currently Tarantool doesn't support user locales (see main()). - * Just check that locale decimal point is '.'. - */ - assert(buf[1] == '.'); -} - /** * A helper to register a single type metatable. */ @@ -1005,7 +982,6 @@ tarantool_lua_utils_init(struct lua_State *L) lua_setfield(L, -2, "__newindex"); luaL_array_metatable_ref = luaL_ref(L, LUA_REGISTRYINDEX); - fpconv_init(); return 0; } diff --git a/src/lua/utils.h b/src/lua/utils.h index 1d27b31695..d6a073eb88 100644 --- a/src/lua/utils.h +++ b/src/lua/utils.h @@ -341,34 +341,6 @@ luaL_checkfield(struct lua_State *L, struct luaL_serializer *cfg, int idx, luaL_convertfield(L, cfg, idx, field); } -enum { FPCONV_G_FMT_BUFSIZE = 32 }; - -extern const char *precision_fmts[]; - -/** - * @brief Locale-independent printf("%.(precision)lg") - * @sa snprintf() - */ -static inline int -fpconv_g_fmt(char *str, double num, int precision) -{ - if (precision <= 0 || precision > 14) - precision = 14; - - const char *fmt = precision_fmts[precision]; - return snprintf(str, FPCONV_G_FMT_BUFSIZE, fmt, num); -} - -/** - * @brief Locale-independent strtod. - * @sa strtod() - */ -static inline double -fpconv_strtod(const char *nptr, char **endptr) -{ - return strtod(nptr, endptr); -} - void luaL_register_type(struct lua_State *L, const char *type_name, const struct luaL_Reg *methods); diff --git a/src/main.cc b/src/main.cc index 64a6631a2e..86680a66d5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -526,6 +526,7 @@ main(int argc, char **argv) setlocale(LC_CTYPE, "en_US.UTF-8") == NULL && setlocale(LC_CTYPE, "en_US.utf8") == NULL) fprintf(stderr, "Failed to set locale to C.UTF-8\n"); + fpconv_check(); if (argc > 1 && access(argv[1], R_OK) != 0) { if (argc == 2 && argv[1][0] != '-') { diff --git a/src/trivia/util.h b/src/trivia/util.h index b80267d812..2ea8a97568 100644 --- a/src/trivia/util.h +++ b/src/trivia/util.h @@ -34,6 +34,7 @@ #include <stdarg.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <unistd.h> #include <inttypes.h> @@ -384,6 +385,40 @@ abspath(const char *filename); char * int2str(long long int val); +void +fpconv_check(void); + +enum { + FPCONV_G_FMT_BUFSIZE = 32, + FPCONV_G_FMT_MAX_PRECISION = 14 +}; + +extern const char *precision_fmts[]; + +/** + * @brief Locale-independent printf("%.(precision)lg") + * @sa snprintf() + */ +static inline int +fpconv_g_fmt(char *str, double num, int precision) +{ + if (precision <= 0 || precision > FPCONV_G_FMT_MAX_PRECISION) + precision = FPCONV_G_FMT_MAX_PRECISION; + + const char *fmt = precision_fmts[precision]; + return snprintf(str, FPCONV_G_FMT_BUFSIZE, fmt, num); +} + +/** + * @brief Locale-independent strtod. + * @sa strtod() + */ +static inline double +fpconv_strtod(const char *nptr, char **endptr) +{ + return strtod(nptr, endptr); +} + /** * Check that @a str is valid utf-8 sequence and can be printed * unescaped. diff --git a/src/util.c b/src/util.c index 04bb88a0a9..dbb7ad148b 100644 --- a/src/util.c +++ b/src/util.c @@ -261,3 +261,26 @@ utf8_check_printable(const char *start, size_t length) } return 1; } + +const char *precision_fmts[] = { + "%.0lg", "%.1lg", "%.2lg", "%.3lg", "%.4lg", "%.5lg", "%.6lg", "%.7lg", + "%.8lg", "%.9lg", "%.10lg", "%.11lg", "%.12lg", "%.13lg", "%.14lg" +}; + +void +fpconv_check() +{ + char buf[8]; + + snprintf(buf, sizeof(buf), "%g", 0.5); + + /* Failing this test might imply the platform has a buggy dtoa + * implementation or wide characters */ + assert(buf[0] == '0' && buf[2] == '5' && buf[3] == 0); + + /* + * Currently Tarantool doesn't support user locales (see main()). + * Just check that locale decimal point is '.'. + */ + assert(buf[1] == '.'); +} diff --git a/third_party/lua-cjson/lua_cjson.c b/third_party/lua-cjson/lua_cjson.c index f0aa22e1b2..aa8217dfb8 100644 --- a/third_party/lua-cjson/lua_cjson.c +++ b/third_party/lua-cjson/lua_cjson.c @@ -36,6 +36,8 @@ * difficult to know object/array sizes ahead of time. */ +#include "trivia/util.h" + #include <assert.h> #include <string.h> #include <math.h> -- GitLab