add lower and upper for str

This commit is contained in:
blueloveTH 2023-06-10 01:10:50 +08:00
parent e5030cd27c
commit 712450f881
2 changed files with 22 additions and 0 deletions

View File

@ -487,6 +487,16 @@ inline void init_builtins(VM* _vm) {
return VAR(Str(p));
});
_vm->bind_method<0>("str", "lower", [](VM* vm, ArgsView args) {
const Str& self = _CAST(Str&, args[0]);
return VAR(self.lower());
});
_vm->bind_method<0>("str", "upper", [](VM* vm, ArgsView args) {
const Str& self = _CAST(Str&, args[0]);
return VAR(self.upper());
});
/************ list ************/
_vm->bind_constructor<2>("list", [](VM* vm, ArgsView args) {
return vm->py_list(args[1]);

View File

@ -210,6 +210,18 @@ struct Str{
return Str(copy);
}
Str lower() const{
std::string copy(data, size);
std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c){ return std::tolower(c); });
return Str(copy);
}
Str upper() const{
std::string copy(data, size);
std::transform(copy.begin(), copy.end(), copy.begin(), [](unsigned char c){ return std::toupper(c); });
return Str(copy);
}
Str escape(bool single_quote=true) const {
std::stringstream ss;
ss << (single_quote ? '\'' : '"');