Skip to content

Commit

Permalink
Move fpconv from lua/utils.h to util.h
Browse files Browse the repository at this point in the history
No semantic changes.

Needed for #128
  • Loading branch information
rtsisyk committed Jun 20, 2017
1 parent c744774 commit 6c11e23
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 52 deletions.
24 changes: 0 additions & 24 deletions src/lua/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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;
}

28 changes: 0 additions & 28 deletions src/lua/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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] != '-') {
Expand Down
35 changes: 35 additions & 0 deletions src/trivia/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] == '.');
}
2 changes: 2 additions & 0 deletions third_party/lua-cjson/lua_cjson.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 6c11e23

Please sign in to comment.