blueloveTH 2 ani în urmă
părinte
comite
7a65de16c0
4 a modificat fișierele cu 8 adăugiri și 9 ștergeri
  1. 1 1
      include/typings/c.pyi
  2. 1 1
      python/pickle.py
  3. 5 6
      src/cffi.cpp
  4. 1 1
      tests/80_c.py

+ 1 - 1
include/typings/c.pyi

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

+ 1 - 1
python/pickle.py

@@ -143,7 +143,7 @@ class _Unpickler:
         # generic object
         cls = _find_class(o[0])
         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)
             return inst
         else:

+ 5 - 6
src/cffi.cpp

@@ -47,17 +47,16 @@ namespace pkpy{
         vm->bind_method<0>(type, "hex", [](VM* vm, ArgsView args){
             const C99Struct& self = _CAST(C99Struct&, args[0]);
             SStream ss;
-            ss << "0x";
             for(int i=0; i<self.size; i++) ss.write_hex((unsigned char)self.p[i]);
             return VAR(ss.str());
         });
 
         // @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]);
-            if(s.size<4 || s[0]!='0' || s[1]!='x' || s.size%2!=0) vm->ValueError("invalid hex string");
-            C99Struct buffer(s.size/2-1, false);
-            for(int i=2; i<s.size; i+=2){
+            if(s.size<2 || s.size%2!=0) vm->ValueError("invalid hex string");
+            C99Struct buffer(s.size/2, false);
+            for(int i=0; i<s.size; i+=2){
                 char c = 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;
@@ -66,7 +65,7 @@ namespace pkpy{
                 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 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));
         }, {}, BindType::STATICMETHOD);

+ 1 - 1
tests/80_c.py

@@ -68,7 +68,7 @@ for i in range(67):
     s.write_char(i, i)
 
 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_hex == s_r.hex()