From 3de540447a0658e7d0756eab245722b9ceed9fd7 Mon Sep 17 00:00:00 2001 From: albertexye Date: Tue, 26 Mar 2024 19:18:14 -0400 Subject: [PATCH] Sort names returned by the built-in dir method --- include/pocketpy/str.h | 4 ++++ src/pocketpy.cpp | 2 +- src/str.cpp | 4 ++++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/pocketpy/str.h b/include/pocketpy/str.h index b29dc96d..582a8737 100644 --- a/include/pocketpy/str.h +++ b/include/pocketpy/str.h @@ -126,6 +126,10 @@ struct StrName { static uint32_t _pesudo_random_index; }; +struct StrNameComparator { + bool operator()(const StrName& a, const StrName& b) const; +}; + struct SStream{ PK_ALWAYS_PASS_BY_POINTER(SStream) // pod_vector is allocated by pool64 so the buffer can be moved into Str without a copy diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index a7cd21b1..97c6fa5e 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -294,7 +294,7 @@ void init_builtins(VM* _vm) { }); _vm->bind_func<1>(_vm->builtins, "dir", [](VM* vm, ArgsView args) { - std::set names; + std::set names; if(!is_tagged(args[0]) && args[0]->is_attr_valid()){ auto keys = args[0]->attr().keys(); names.insert(keys.begin(), keys.end()); diff --git a/src/str.cpp b/src/str.cpp index b0308ffb..a6d0ca22 100644 --- a/src/str.cpp +++ b/src/str.cpp @@ -406,6 +406,10 @@ int utf8len(unsigned char c, bool suppress){ return _r_interned().find(index) != _r_interned().end(); } + bool StrNameComparator::operator()(const StrName& a, const StrName& b) const { + return a._r_interned()[a.index] < b._r_interned()[b.index]; + } + Str SStream::str(){ // after this call, the buffer is no longer valid buffer.reserve(buffer.size() + 1); // allocate one more byte for '\0'