From 59ad2c1dfc3303a294209754a99868b265671981 Mon Sep 17 00:00:00 2001 From: "Md. Almas Ali" Date: Wed, 1 May 2024 17:52:02 +0600 Subject: [PATCH] feat: added more features in strings. --- core/builtin_classes/string_object.py | 76 ++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/core/builtin_classes/string_object.py b/core/builtin_classes/string_object.py index 8d51bb2..e16ac8b 100644 --- a/core/builtin_classes/string_object.py +++ b/core/builtin_classes/string_object.py @@ -68,6 +68,80 @@ def count(ctx): string = ctx.symbol_table.get("string") if len(string.value) == 0: return res.failure( - RTError(string.pos_start, string.pos_end, "Cannot count an empty string", string.context) + RTError(string.pos_start, string.pos_end, + "Cannot count an empty string", string.context) ) return res.success(Number(self.value.count(string.value))) + + @args(["string", "value"], [String(""), String("")]) + @method + def replace(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + value = ctx.symbol_table.get("value") + return res.success(String(self.value.replace(string.value, value.value))) + + @args(["string"], [String("")]) + @method + def find(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + return res.success(Number(self.value.find(string.value))) + + @args(["string"], [String("")]) + @method + def startswith(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + return res.success(Boolean(self.value.startswith(string.value))) + + @args(["string"], [String("")]) + @method + def endswith(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + return res.success(Boolean(self.value.endswith(string.value))) + + @args(["string"], [String("")]) + @method + def split(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + return res.success(Array([String(i) for i in self.value.split(string.value)])) + + @args(["string"], [String("")]) + @method + def join(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + return res.success(String(string.value.join(self.value))) + + @args(["string"], [String("")]) + @method + def strip(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + return res.success(String(self.value.strip(string.value))) + + @args(["string"], [String("")]) + @method + def lstrip(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + return res.success(String(self.value.lstrip(string.value))) + + @args(["string"], [String("")]) + @method + def rstrip(ctx): + res = RTResult() + self = ctx.symbol_table.get("this") + string = ctx.symbol_table.get("string") + return res.success(String(self.value.rstrip(string.value)))