mirror of
https://github.com/erg-lang/erg.git
synced 2025-09-29 12:24:45 +00:00
style(python): format with black
This commit is contained in:
parent
454224858b
commit
08ad656240
17 changed files with 208 additions and 36 deletions
|
@ -2,55 +2,72 @@ from _erg_result import Error
|
|||
from _erg_int import Int
|
||||
from _erg_control import then__
|
||||
|
||||
|
||||
class Str(str):
|
||||
def __instancecheck__(cls, obj):
|
||||
return isinstance(obj, str)
|
||||
def try_new(s: str): # -> Result[Nat]
|
||||
|
||||
def try_new(s: str): # -> Result[Nat]
|
||||
if isinstance(s, str):
|
||||
return Str(s)
|
||||
else:
|
||||
return Error("Str can't be other than str")
|
||||
|
||||
def get(self, i: int):
|
||||
if len(self) > i:
|
||||
return Str(self[i])
|
||||
else:
|
||||
return None
|
||||
|
||||
def mutate(self):
|
||||
return StrMut(self)
|
||||
|
||||
def to_int(self):
|
||||
return Int(self) if self.isdigit() else None
|
||||
|
||||
def contains(self, s):
|
||||
return s in self
|
||||
|
||||
def __add__(self, other):
|
||||
return then__(str.__add__(self, other), Str)
|
||||
|
||||
def __radd__(self, other):
|
||||
return then__(str.__add__(other, self), Str)
|
||||
|
||||
def __mul__(self, other):
|
||||
return then__(str.__mul__(self, other), Str)
|
||||
|
||||
def __mod__(self, other):
|
||||
return then__(str.__mod__(other, self), Str)
|
||||
|
||||
class StrMut(): # Inherits Str
|
||||
|
||||
class StrMut: # Inherits Str
|
||||
value: Str
|
||||
|
||||
def __init__(self, s: str):
|
||||
self.value = s
|
||||
|
||||
def __repr__(self):
|
||||
return self.value.__repr__()
|
||||
|
||||
def __str__(self):
|
||||
return self.value.__str__()
|
||||
|
||||
def __hash__(self):
|
||||
return self.value.__hash__()
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Str):
|
||||
return self.value == other
|
||||
else:
|
||||
return self.value == other.value
|
||||
|
||||
def __ne__(self, other):
|
||||
if isinstance(other, Str):
|
||||
return self.value != other
|
||||
else:
|
||||
return self.value != other.value
|
||||
|
||||
def try_new(s: str):
|
||||
if isinstance(s, str):
|
||||
self = StrMut()
|
||||
|
@ -58,8 +75,10 @@ class StrMut(): # Inherits Str
|
|||
return self
|
||||
else:
|
||||
return Error("Str! can't be other than str")
|
||||
|
||||
def clear(self):
|
||||
self.value = ""
|
||||
|
||||
def pop(self):
|
||||
if len(self.value) > 0:
|
||||
last = self.value[-1]
|
||||
|
@ -67,11 +86,14 @@ class StrMut(): # Inherits Str
|
|||
return last
|
||||
else:
|
||||
return Error("Can't pop from empty `Str!`")
|
||||
|
||||
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:]
|
||||
self.value = self.value[:idx] + self.value[idx + 1 :]
|
||||
return char
|
||||
|
||||
def insert(self, idx: int, s: str):
|
||||
self.value = self.value[:idx] + s + self.value[idx:]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue