From f0d82a19ee57e707160425033b667350de40f473 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 13 Jun 2024 13:22:29 +0800 Subject: [PATCH] complete sstream --- include/pocketpy/common/sstream.h | 2 ++ include/pocketpy/common/str.hpp | 10 ++++-- include/pocketpy/interpreter/cffi.hpp | 2 +- src/common/sstream.c | 27 +++++++++++++++ src/common/str.cpp | 48 --------------------------- src/pocketpy.cpp | 19 +++++++++-- 6 files changed, 54 insertions(+), 54 deletions(-) diff --git a/include/pocketpy/common/sstream.h b/include/pocketpy/common/sstream.h index e2e119a3..b1e37cd0 100644 --- a/include/pocketpy/common/sstream.h +++ b/include/pocketpy/common/sstream.h @@ -51,6 +51,8 @@ void pkpy_SStream__write_Str(pkpy_SStream* self, const pkpy_Str*); void pkpy_SStream__write_sv(pkpy_SStream* self, c11_string); void pkpy_SStream__write_cstr(pkpy_SStream* self, const char*); void pkpy_SStream__write_cstrn(pkpy_SStream* self, const char*, int); +void pkpy_SStream__write_hex(pkpy_SStream* self, unsigned char, bool non_zero); +void pkpy_SStream__write_ptr(pkpy_SStream* self, void*); void pkpy_SStream__write_any(pkpy_SStream* self, const char* fmt, const pkpy_AnyStr* args, int n); // Submit the stream and return the final string. The stream becomes invalid after this call diff --git a/include/pocketpy/common/str.hpp b/include/pocketpy/common/str.hpp index 25dbee7a..9e529ee6 100644 --- a/include/pocketpy/common/str.hpp +++ b/include/pocketpy/common/str.hpp @@ -353,9 +353,13 @@ struct SStream: pkpy_SStream { return *this; } - void write_hex(unsigned char, bool non_zero = false); - void write_hex(void*); - void write_hex(i64); + void write_hex(unsigned char val, bool non_zero = false){ + pkpy_SStream__write_hex(this, val, non_zero); + } + + void write_ptr(void* p){ + pkpy_SStream__write_ptr(this, p); + } }; #ifdef _S diff --git a/include/pocketpy/interpreter/cffi.hpp b/include/pocketpy/interpreter/cffi.hpp index 7dd5362b..7c9d8b9e 100644 --- a/include/pocketpy/interpreter/cffi.hpp +++ b/include/pocketpy/interpreter/cffi.hpp @@ -29,7 +29,7 @@ struct VoidP { Str hex() const { SStream ss; - ss.write_hex(ptr); + ss.write_ptr(ptr); return ss.str(); } diff --git a/src/common/sstream.c b/src/common/sstream.c index df20c129..8fee789a 100644 --- a/src/common/sstream.c +++ b/src/common/sstream.c @@ -76,6 +76,33 @@ void pkpy_SStream__write_cstrn(pkpy_SStream* self, const char* str, int n) { c11_vector__extend(char, &self->data, str, n); } +void pkpy_SStream__write_hex(pkpy_SStream* self, unsigned char c, bool non_zero) { + unsigned char high = c >> 4; + unsigned char low = c & 0xf; + if(non_zero) { + if(high) pkpy_SStream__write_char(self, PK_HEX_TABLE[high]); + if(high || low) pkpy_SStream__write_char(self, PK_HEX_TABLE[low]); + } else { + pkpy_SStream__write_char(self, PK_HEX_TABLE[high]); + pkpy_SStream__write_char(self, PK_HEX_TABLE[low]); + } +} + +void pkpy_SStream__write_ptr(pkpy_SStream* self, void* p) { + if(p == NULL) { + pkpy_SStream__write_cstr(self, "0x0"); + return; + } + pkpy_SStream__write_cstr(self, "0x"); + uintptr_t p_t = (uintptr_t)(p); + bool non_zero = true; + for(int i = sizeof(void*) - 1; i >= 0; i--) { + unsigned char cpnt = (p_t >> (i * 8)) & 0xff; + pkpy_SStream__write_hex(self, cpnt, non_zero); + if(cpnt != 0) non_zero = false; + } +} + void pkpy_SStream__write_any(pkpy_SStream* self, const char* fmt, const pkpy_AnyStr* args, int n){ int i = 0; while(*fmt){ diff --git a/src/common/str.cpp b/src/common/str.cpp index 451f5818..2d14136b 100644 --- a/src/common/str.cpp +++ b/src/common/str.cpp @@ -54,54 +54,6 @@ StrName StrName::get(std::string_view s) { return StrName(index); } -void SStream::write_hex(unsigned char c, bool non_zero) { - unsigned char high = c >> 4; - unsigned char low = c & 0xf; - if(non_zero) { - if(high) (*this) << PK_HEX_TABLE[high]; - if(high || low) (*this) << PK_HEX_TABLE[low]; - } else { - (*this) << PK_HEX_TABLE[high]; - (*this) << PK_HEX_TABLE[low]; - } -} - -void SStream::write_hex(void* p) { - if(p == nullptr) { - (*this) << "0x0"; - return; - } - (*this) << "0x"; - uintptr_t p_t = reinterpret_cast(p); - bool non_zero = true; - for(int i = sizeof(void*) - 1; i >= 0; i--) { - unsigned char cpnt = (p_t >> (i * 8)) & 0xff; - write_hex(cpnt, non_zero); - if(cpnt != 0) non_zero = false; - } -} - -void SStream::write_hex(i64 val) { - if(val == 0) { - (*this) << "0x0"; - return; - } - if(val < 0) { - (*this) << "-"; - val = -val; - } - (*this) << "0x"; - bool non_zero = true; - for(int i = 56; i >= 0; i -= 8) { - unsigned char cpnt = (val >> i) & 0xff; - write_hex(cpnt, non_zero); - if(cpnt != 0) non_zero = false; - } -} - -#undef PK_STR_ALLOCATE -#undef PK_STR_COPY_INIT - // unary operators const StrName __repr__ = StrName::get("__repr__"); const StrName __str__ = StrName::get("__str__"); diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 4b523275..23d3596a 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -296,7 +296,22 @@ void __init_builtins(VM* _vm) { _vm->bind_func(_vm->builtins, "hex", 1, [](VM* vm, ArgsView args) { SStream ss; - ss.write_hex(CAST(i64, args[0])); + i64 val = CAST(i64, args[0]); + if(val == 0) { + ss << "0x0"; + return VAR(ss.str()); + } + if(val < 0) { + ss << "-"; + val = -val; + } + ss << "0x"; + bool non_zero = true; + for(int i = 56; i >= 0; i -= 8) { + unsigned char cpnt = (val >> i) & 0xff; + ss.write_hex(cpnt, non_zero); + if(cpnt != 0) non_zero = false; + } return VAR(ss.str()); }); @@ -353,7 +368,7 @@ void __init_builtins(VM* _vm) { assert(!is_tagged(obj)); SStream ss; ss << "<" << _type_name(vm, vm->_tp(obj)) << " object at "; - ss.write_hex(obj.get()); + ss.write_ptr(obj.get()); ss << ">"; return ss.str(); });