mirror of
https://github.com/pocketpy/pocketpy
synced 2026-06-21 07:57:10 +08:00
Merge 794936abda3832cb4b8ca4fbf9b9e9ba55937565 into 8c4ed3bc3423f56d907e80ab4dc42dab1267afc3
This commit is contained in:
commit
5b0c543c78
@ -7,6 +7,8 @@
|
|||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
void c11_sbuf__ctor(c11_sbuf* self) {
|
void c11_sbuf__ctor(c11_sbuf* self) {
|
||||||
@ -53,21 +55,26 @@ void c11_sbuf__write_f64(c11_sbuf* self, double val, int precision) {
|
|||||||
char b[32];
|
char b[32];
|
||||||
int size;
|
int size;
|
||||||
if(precision < 0) {
|
if(precision < 0) {
|
||||||
int prec = 17 - 1; // std::numeric_limits<double>::max_digits10 == 17
|
// Pick the shortest decimal width that round-trips to the same binary double.
|
||||||
size = snprintf(b, sizeof(b), "%.*g", prec, val);
|
for(int prec = 15; prec <= 17; prec++) {
|
||||||
|
size = snprintf(b, sizeof(b), "%.*g", prec, val);
|
||||||
|
double parsed = strtod(b, NULL);
|
||||||
|
if(memcmp(&parsed, &val, sizeof(double)) == 0) break;
|
||||||
|
}
|
||||||
|
bool all_is_digit = true;
|
||||||
|
for(int i = 1; i < size; i++) {
|
||||||
|
if(!isdigit(b[i])) {
|
||||||
|
all_is_digit = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c11_sbuf__write_cstr(self, b);
|
||||||
|
if(all_is_digit) c11_sbuf__write_cstr(self, ".0");
|
||||||
} else {
|
} else {
|
||||||
int prec = precision;
|
int prec = precision;
|
||||||
size = snprintf(b, sizeof(b), "%.*f", prec, val);
|
size = snprintf(b, sizeof(b), "%.*f", prec, val);
|
||||||
|
c11_sbuf__write_cstr(self, b);
|
||||||
}
|
}
|
||||||
c11_sbuf__write_cstr(self, b);
|
|
||||||
bool all_is_digit = true;
|
|
||||||
for(int i = 1; i < size; i++) {
|
|
||||||
if(!isdigit(b[i])) {
|
|
||||||
all_is_digit = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(all_is_digit) c11_sbuf__write_cstr(self, ".0");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void c11_sbuf__write_sv(c11_sbuf* self, c11_sv sv) {
|
void c11_sbuf__write_sv(c11_sbuf* self, c11_sv sv) {
|
||||||
|
|||||||
@ -102,7 +102,7 @@ static bool json__write_namedict_kv(py_Name k, py_Ref v, void* ctx_) {
|
|||||||
static bool json__write_object(c11_sbuf* buf, py_TValue* obj, int indent, int depth) {
|
static bool json__write_object(c11_sbuf* buf, py_TValue* obj, int indent, int depth) {
|
||||||
switch(obj->type) {
|
switch(obj->type) {
|
||||||
case tp_NoneType: c11_sbuf__write_cstr(buf, "null"); return true;
|
case tp_NoneType: c11_sbuf__write_cstr(buf, "null"); return true;
|
||||||
case tp_int: c11_sbuf__write_int(buf, obj->_i64); return true;
|
case tp_int: c11_sbuf__write_i64(buf, obj->_i64); return true;
|
||||||
case tp_float: {
|
case tp_float: {
|
||||||
if(dmath_isnan(obj->_f64)) {
|
if(dmath_isnan(obj->_f64)) {
|
||||||
c11_sbuf__write_cstr(buf, "NaN");
|
c11_sbuf__write_cstr(buf, "NaN");
|
||||||
|
|||||||
@ -64,6 +64,7 @@ assert f'{a:010f}' == '010.000000'
|
|||||||
assert f'{a:010.2f}' == '0000010.00'
|
assert f'{a:010.2f}' == '0000010.00'
|
||||||
assert f'{a:.2f}' == '10.00'
|
assert f'{a:.2f}' == '10.00'
|
||||||
assert f'{a:.5f}' == '10.00000'
|
assert f'{a:.5f}' == '10.00000'
|
||||||
|
assert f'{2.5:.0f}' == '2'
|
||||||
|
|
||||||
b = '123'
|
b = '123'
|
||||||
assert f'{b:10}' == '123 '
|
assert f'{b:10}' == '123 '
|
||||||
|
|||||||
@ -48,6 +48,9 @@ _j = json.dumps(c)
|
|||||||
_c = json.loads(_j)
|
_c = json.loads(_j)
|
||||||
assert c == _c
|
assert c == _c
|
||||||
|
|
||||||
|
assert json.dumps(9223372036854775807) == '9223372036854775807'
|
||||||
|
assert json.loads(json.dumps(0.1 + 0.2)) == 0.1 + 0.2
|
||||||
|
|
||||||
d = True
|
d = True
|
||||||
_j = json.dumps(d)
|
_j = json.dumps(d)
|
||||||
_d = json.loads(_j)
|
_d = json.loads(_j)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user