Add some Str! methods

This commit is contained in:
Shunsuke Shibayama 2023-02-01 13:37:41 +09:00
parent 4184c5d1f2
commit e44ba71aa5
2 changed files with 32 additions and 4 deletions

View file

@ -984,6 +984,34 @@ impl Context {
);
str_mut_mutable.register_builtin_erg_impl(PROC_UPDATE, t, Immutable, Public);
str_mut.register_trait(mono(MUT_STR), str_mut_mutable);
let t = pr_met(
ref_mut(mono(MUT_STR), None),
vec![kw("s", Str)],
None,
vec![],
NoneType,
);
str_mut.register_builtin_py_impl(PROC_PUSH, t, Immutable, Public, Some(FUNC_PUSH));
let t = pr0_met(ref_mut(mono(MUT_STR), None), Str);
str_mut.register_builtin_py_impl(PROC_POP, t, Immutable, Public, Some(FUNC_POP));
let t = pr0_met(ref_mut(mono(MUT_STR), None), NoneType);
str_mut.register_builtin_py_impl(PROC_CLEAR, t, Immutable, Public, Some(FUNC_CLEAR));
let t = pr_met(
ref_mut(mono(MUT_STR), None),
vec![kw("idx", Nat), kw("s", Str)],
None,
vec![],
NoneType,
);
str_mut.register_builtin_py_impl(PROC_INSERT, t, Immutable, Public, Some(FUNC_INSERT));
let t = pr_met(
ref_mut(mono(MUT_STR), None),
vec![kw("idx", Nat)],
None,
vec![],
Str,
);
str_mut.register_builtin_py_impl(PROC_REMOVE, t, Immutable, Public, Some(FUNC_REMOVE));
/* File! */
let mut file_mut = Self::builtin_mono_class(MUT_FILE, 2);
let mut file_mut_readable = Self::builtin_methods(Some(mono(MUT_READABLE)), 1);

View file

@ -52,11 +52,11 @@ class StrMut(): # Inherits Str
return last
else:
return Error("Can't pop from empty `Str!`")
def push(self, c: str):
self.value += c
def push(self, s: str):
self.value += s
def remove(self, idx: int):
char = self.value[idx]
self.value = self.value[:idx] + self.value[idx+1:]
return char
def insert(self, idx: int, c: str):
self.value = self.value[:idx] + c + self.value[idx:]
def insert(self, idx: int, s: str):
self.value = self.value[:idx] + s + self.value[idx:]