Skip to content

Commit

Permalink
feat: added more features in strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
Almas-Ali committed May 1, 2024
1 parent 56dfbd0 commit 59ad2c1
Showing 1 changed file with 75 additions and 1 deletion.
76 changes: 75 additions & 1 deletion core/builtin_classes/string_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

0 comments on commit 59ad2c1

Please sign in to comment.