diff --git a/include/pocketpy/obj.h b/include/pocketpy/obj.h index e4aa3e92..ac66d73b 100644 --- a/include/pocketpy/obj.h +++ b/include/pocketpy/obj.h @@ -61,11 +61,12 @@ struct Bytes{ return _data != rhs._data; } - std::string str() const noexcept { return std::string(_data.begin(), _data.end()); } + Str str() const noexcept { return Str(_data.data(), _data.size()); } Bytes() : _data(), _ok(false) {} Bytes(std::vector&& data) : _data(std::move(data)), _ok(true) {} - Bytes(const std::string& data) : _data(data.begin(), data.end()), _ok(true) {} + Bytes(const Str& data) : _data(data.begin(), data.end()), _ok(true) {} + Bytes(std::string_view sv): _data(sv.begin(), sv.end()), _ok(true) {} operator bool() const noexcept { return _ok; } }; diff --git a/libpocketpy.dylib b/libpocketpy.dylib deleted file mode 100755 index e53981b6..00000000 Binary files a/libpocketpy.dylib and /dev/null differ diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 35d26daa..4a180cf3 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -916,7 +916,8 @@ void init_builtins(VM* _vm) { _vm->bind__hash__(_vm->tp_bytes, [](VM* vm, PyObject* obj) { const Bytes& self = _CAST(Bytes&, obj); - return (i64)std::hash()(self.str()); + std::string_view view(self.data(), self.size()); + return (i64)std::hash()(view); }); _vm->bind__repr__(_vm->tp_bytes, [](VM* vm, PyObject* obj) { diff --git a/tests/10_bytes.py b/tests/10_bytes.py index f982fa1e..f1a0f0c9 100644 --- a/tests/10_bytes.py +++ b/tests/10_bytes.py @@ -3,3 +3,6 @@ assert a.encode() == b'12345' assert b'\xff\xee' != b'1234' assert b'\xff\xee' == b'\xff\xee' + +a = '测试123' +assert a == a.encode().decode() \ No newline at end of file