This commit is contained in:
blueloveTH 2024-01-15 14:43:52 +08:00
parent d8d25894c7
commit 7a65de16c0
4 changed files with 8 additions and 9 deletions

View File

@ -59,7 +59,7 @@ class struct:
def hex(self) -> str: ... def hex(self) -> str: ...
@staticmethod @staticmethod
def from_hex(s: str) -> 'struct': ... def fromhex(s: str) -> 'struct': ...
def read_char(self, offset=0) -> int: ... def read_char(self, offset=0) -> int: ...
def read_uchar(self, offset=0) -> int: ... def read_uchar(self, offset=0) -> int: ...

View File

@ -143,7 +143,7 @@ class _Unpickler:
# generic object # generic object
cls = _find_class(o[0]) cls = _find_class(o[0])
if getattr(cls, '__struct__', False): if getattr(cls, '__struct__', False):
inst = cls.from_struct(struct.from_hex(o[1])) inst = cls.from_struct(struct.fromhex(o[1]))
self.tag(index, inst) self.tag(index, inst)
return inst return inst
else: else:

View File

@ -47,17 +47,16 @@ namespace pkpy{
vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){ vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){
const C99Struct& self = _CAST(C99Struct&, args[0]); const C99Struct& self = _CAST(C99Struct&, args[0]);
SStream ss; SStream ss;
ss << "0x";
for(int i=0; i<self.size; i++) ss.write_hex((unsigned char)self.p[i]); for(int i=0; i<self.size; i++) ss.write_hex((unsigned char)self.p[i]);
return VAR(ss.str()); return VAR(ss.str());
}); });
// @staticmethod // @staticmethod
vm->bind_func<1>(type, "from_hex", [](VM* vm, ArgsView args){ vm->bind_func<1>(type, "fromhex", [](VM* vm, ArgsView args){
const Str& s = CAST(Str&, args[0]); const Str& s = CAST(Str&, args[0]);
if(s.size<4 || s[0]!='0' || s[1]!='x' || s.size%2!=0) vm->ValueError("invalid hex string"); if(s.size<2 || s.size%2!=0) vm->ValueError("invalid hex string");
C99Struct buffer(s.size/2-1, false); C99Struct buffer(s.size/2, false);
for(int i=2; i<s.size; i+=2){ for(int i=0; i<s.size; i+=2){
char c = 0; char c = 0;
if(s[i]>='0' && s[i]<='9') c += s[i]-'0'; if(s[i]>='0' && s[i]<='9') c += s[i]-'0';
else if(s[i]>='A' && s[i]<='F') c += s[i]-'A'+10; else if(s[i]>='A' && s[i]<='F') c += s[i]-'A'+10;
@ -66,7 +65,7 @@ namespace pkpy{
if(s[i+1]>='0' && s[i+1]<='9') c += s[i+1]-'0'; if(s[i+1]>='0' && s[i+1]<='9') c += s[i+1]-'0';
else if(s[i+1]>='A' && s[i+1]<='F') c += s[i+1]-'A'+10; else if(s[i+1]>='A' && s[i+1]<='F') c += s[i+1]-'A'+10;
else vm->ValueError(fmt("invalid hex char: '", s[i+1], "'")); else vm->ValueError(fmt("invalid hex char: '", s[i+1], "'"));
buffer.p[i/2-1] = c; buffer.p[i/2] = c;
} }
return VAR_T(C99Struct, std::move(buffer)); return VAR_T(C99Struct, std::move(buffer));
}, {}, BindType::STATICMETHOD); }, {}, BindType::STATICMETHOD);

View File

@ -68,7 +68,7 @@ for i in range(67):
s.write_char(i, i) s.write_char(i, i)
s_hex = s.hex() s_hex = s.hex()
s_r = c.struct.from_hex(s_hex) s_r = c.struct.fromhex(s_hex)
assert (s == s_r and s is not s_r), (s_hex, s_r.hex()) assert (s == s_r and s is not s_r), (s_hex, s_r.hex())
assert s_hex == s_r.hex() assert s_hex == s_r.hex()