mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-24 21:40:16 +00:00
complete sstream
This commit is contained in:
parent
0811f23b71
commit
f0d82a19ee
@ -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
|
||||
|
||||
@ -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
|
||||
|
||||
@ -29,7 +29,7 @@ struct VoidP {
|
||||
|
||||
Str hex() const {
|
||||
SStream ss;
|
||||
ss.write_hex(ptr);
|
||||
ss.write_ptr(ptr);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
|
||||
@ -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){
|
||||
|
||||
@ -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<uintptr_t>(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__");
|
||||
|
||||
@ -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();
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user