mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-09 21:20:17 +00:00
Sort names returned by the built-in dir method
This commit is contained in:
parent
665fb04b8f
commit
3de540447a
@ -126,6 +126,10 @@ struct StrName {
|
|||||||
static uint32_t _pesudo_random_index;
|
static uint32_t _pesudo_random_index;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct StrNameComparator {
|
||||||
|
bool operator()(const StrName& a, const StrName& b) const;
|
||||||
|
};
|
||||||
|
|
||||||
struct SStream{
|
struct SStream{
|
||||||
PK_ALWAYS_PASS_BY_POINTER(SStream)
|
PK_ALWAYS_PASS_BY_POINTER(SStream)
|
||||||
// pod_vector<T> is allocated by pool64 so the buffer can be moved into Str without a copy
|
// pod_vector<T> is allocated by pool64 so the buffer can be moved into Str without a copy
|
||||||
|
|||||||
@ -294,7 +294,7 @@ void init_builtins(VM* _vm) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_func<1>(_vm->builtins, "dir", [](VM* vm, ArgsView args) {
|
_vm->bind_func<1>(_vm->builtins, "dir", [](VM* vm, ArgsView args) {
|
||||||
std::set<StrName> names;
|
std::set<StrName, StrNameComparator> names;
|
||||||
if(!is_tagged(args[0]) && args[0]->is_attr_valid()){
|
if(!is_tagged(args[0]) && args[0]->is_attr_valid()){
|
||||||
auto keys = args[0]->attr().keys();
|
auto keys = args[0]->attr().keys();
|
||||||
names.insert(keys.begin(), keys.end());
|
names.insert(keys.begin(), keys.end());
|
||||||
|
|||||||
@ -406,6 +406,10 @@ int utf8len(unsigned char c, bool suppress){
|
|||||||
return _r_interned().find(index) != _r_interned().end();
|
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(){
|
Str SStream::str(){
|
||||||
// after this call, the buffer is no longer valid
|
// after this call, the buffer is no longer valid
|
||||||
buffer.reserve(buffer.size() + 1); // allocate one more byte for '\0'
|
buffer.reserve(buffer.size() + 1); // allocate one more byte for '\0'
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user