mirror of
https://github.com/janet-lang/janet
synced 2024-11-19 23:24:49 +00:00
Work on add locales.
Need to be careful not to mess with %j formatter, or in some other places.
This commit is contained in:
parent
91a583db27
commit
2e2f8abfc0
@ -349,6 +349,26 @@ JANET_CORE_FN(janet_cfun_lcm, "(math/lcm x y)",
|
|||||||
return janet_wrap_number(janet_lcm(x, y));
|
return janet_wrap_number(janet_lcm(x, y));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JANET_CORE_FN(janet_cfun_frexp, "(math/frexp x)",
|
||||||
|
"Returns a tuple of (mantissa, exponent) from number.") {
|
||||||
|
janet_fixarity(argc, 1);
|
||||||
|
double x = janet_getnumber(argv, 0);
|
||||||
|
int exp;
|
||||||
|
x = frexp(x, &exp);
|
||||||
|
Janet *result = janet_tuple_begin(2);
|
||||||
|
result[0] = janet_wrap_number(x);
|
||||||
|
result[1] = janet_wrap_number((double) exp);
|
||||||
|
return janet_wrap_tuple(janet_tuple_end(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
JANET_CORE_FN(janet_cfun_ldexp, "(math/ldexp m e)",
|
||||||
|
"Creates a new number from a mantissa and an exponent.") {
|
||||||
|
janet_fixarity(argc, 2);
|
||||||
|
double x = janet_getnumber(argv, 0);
|
||||||
|
int32_t y = janet_getinteger(argv, 1);
|
||||||
|
return janet_wrap_number(ldexp(x, y));
|
||||||
|
}
|
||||||
|
|
||||||
/* Module entry point */
|
/* Module entry point */
|
||||||
void janet_lib_math(JanetTable *env) {
|
void janet_lib_math(JanetTable *env) {
|
||||||
JanetRegExt math_cfuns[] = {
|
JanetRegExt math_cfuns[] = {
|
||||||
@ -395,6 +415,8 @@ void janet_lib_math(JanetTable *env) {
|
|||||||
JANET_CORE_REG("math/next", janet_nextafter),
|
JANET_CORE_REG("math/next", janet_nextafter),
|
||||||
JANET_CORE_REG("math/gcd", janet_cfun_gcd),
|
JANET_CORE_REG("math/gcd", janet_cfun_gcd),
|
||||||
JANET_CORE_REG("math/lcm", janet_cfun_lcm),
|
JANET_CORE_REG("math/lcm", janet_cfun_lcm),
|
||||||
|
JANET_CORE_REG("math/frexp", janet_cfun_frexp),
|
||||||
|
JANET_CORE_REG("math/ldexp", janet_cfun_ldexp),
|
||||||
JANET_REG_END
|
JANET_REG_END
|
||||||
};
|
};
|
||||||
janet_core_cfuns_ext(env, NULL, math_cfuns);
|
janet_core_cfuns_ext(env, NULL, math_cfuns);
|
||||||
|
@ -38,6 +38,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
|
#include <locale.h>
|
||||||
|
|
||||||
#ifdef JANET_BSD
|
#ifdef JANET_BSD
|
||||||
#include <sys/sysctl.h>
|
#include <sys/sysctl.h>
|
||||||
@ -1891,6 +1892,42 @@ JANET_CORE_FN(os_mktime,
|
|||||||
#define j_symlink symlink
|
#define j_symlink symlink
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
JANET_CORE_FN(os_setlocale,
|
||||||
|
"(os/setlocale category &opt locale)",
|
||||||
|
"Set the system locale, which affects how dates, currencies, and numbers are formatted. "
|
||||||
|
"Passing nil to locale will return the current locale.") {
|
||||||
|
janet_arity(argc, 1, 2);
|
||||||
|
const char *locale = janet_optcstring(argv, argc, 1, NULL);
|
||||||
|
int category_int = 0;
|
||||||
|
if (janet_keyeq(argv[0], "all")) {
|
||||||
|
category_int |= LC_ALL_MASK;
|
||||||
|
} else if (janet_keyeq(argv[0], "collate")) {
|
||||||
|
category_int |= LC_COLLATE_MASK;
|
||||||
|
} else if (janet_keyeq(argv[0], "ctype")) {
|
||||||
|
category_int |= LC_CTYPE_MASK;
|
||||||
|
} else if (janet_keyeq(argv[0], "monetary")) {
|
||||||
|
category_int |= LC_MONETARY_MASK;
|
||||||
|
} else if (janet_keyeq(argv[0], "numeric")) {
|
||||||
|
category_int |= LC_NUMERIC_MASK;
|
||||||
|
} else if (janet_keyeq(argv[0], "time")) {
|
||||||
|
category_int |= LC_TIME_MASK;
|
||||||
|
} else {
|
||||||
|
janet_panicf("expected one of :all, :collate, :ctype, :monetary, :numeric, or :time, got %v", argv[0]);
|
||||||
|
}
|
||||||
|
if (locale == NULL) {
|
||||||
|
const char *old = setlocale(category_int, NULL);
|
||||||
|
if (old == NULL) return janet_wrap_nil();
|
||||||
|
return janet_cstringv(old);
|
||||||
|
}
|
||||||
|
locale_t loc = newlocale(category_int, locale, 0);
|
||||||
|
if (loc == 0) {
|
||||||
|
janet_panicf("failed to set locale - %s", strerror(errno));
|
||||||
|
}
|
||||||
|
uselocale(loc);
|
||||||
|
freelocale(loc);
|
||||||
|
return janet_wrap_nil();
|
||||||
|
}
|
||||||
|
|
||||||
JANET_CORE_FN(os_link,
|
JANET_CORE_FN(os_link,
|
||||||
"(os/link oldpath newpath &opt symlink)",
|
"(os/link oldpath newpath &opt symlink)",
|
||||||
"Create a link at newpath that points to oldpath and returns nil. "
|
"Create a link at newpath that points to oldpath and returns nil. "
|
||||||
@ -2688,6 +2725,7 @@ void janet_lib_os(JanetTable *env) {
|
|||||||
JANET_CORE_REG("os/strftime", os_strftime),
|
JANET_CORE_REG("os/strftime", os_strftime),
|
||||||
JANET_CORE_REG("os/sleep", os_sleep),
|
JANET_CORE_REG("os/sleep", os_sleep),
|
||||||
JANET_CORE_REG("os/isatty", os_isatty),
|
JANET_CORE_REG("os/isatty", os_isatty),
|
||||||
|
JANET_CORE_REG("os/setlocale", os_setlocale),
|
||||||
|
|
||||||
/* env functions */
|
/* env functions */
|
||||||
JANET_CORE_REG("os/environ", os_environ),
|
JANET_CORE_REG("os/environ", os_environ),
|
||||||
|
@ -380,6 +380,13 @@ static int print_jdn_one(struct pretty *S, Janet x, int depth) {
|
|||||||
case JANET_NUMBER:
|
case JANET_NUMBER:
|
||||||
janet_buffer_ensure(S->buffer, S->buffer->count + BUFSIZE, 2);
|
janet_buffer_ensure(S->buffer, S->buffer->count + BUFSIZE, 2);
|
||||||
int count = snprintf((char *) S->buffer->data + S->buffer->count, BUFSIZE, "%.17g", janet_unwrap_number(x));
|
int count = snprintf((char *) S->buffer->data + S->buffer->count, BUFSIZE, "%.17g", janet_unwrap_number(x));
|
||||||
|
/* fix locale issues with commas */
|
||||||
|
for (int i = 0; i < count; i++) {
|
||||||
|
char c = S->buffer->data[S->buffer->count + i];
|
||||||
|
if (c == ',' || c == '\'') {
|
||||||
|
S->buffer->data[S->buffer->count + i] = '.';
|
||||||
|
}
|
||||||
|
}
|
||||||
S->buffer->count += count;
|
S->buffer->count += count;
|
||||||
break;
|
break;
|
||||||
case JANET_SYMBOL:
|
case JANET_SYMBOL:
|
||||||
|
Loading…
Reference in New Issue
Block a user