1from collections
import Counter
2from typing
import Optional, Union
6 """Represents a slice of a base string from startIndex to endIndex."""
9 self, base: str, startIndex: int = 0, endIndex: Optional[int] =
None
14 self.
endIndex = endIndex
if endIndex
is not None else len(base)
18 ),
"Invalid start or end index"
24 if not (0 <= index < len(self)):
25 raise IndexError(
"Index out of range")
32 start, end = 0, len(self)
33 while start < end
and self[start].isspace():
35 while start < end
and self[end - 1].isspace():
37 return self.
subSequence(start, end)
if start > 0
or end < len(self)
else self
42 def sameAs(self, other: Union[
"Slice", str]) -> bool:
43 if isinstance(other, Slice):
44 return str(self) == str(other)
45 elif isinstance(other, str):
46 if len(self) != len(other):
48 return all(self[i] == other[i]
for i
in range(len(self)))
51 def indexOf(self, lookingFor: str, startOffset: int = 0) -> int:
52 result = self.
base.find(
55 return -1
if result == -1
else result - self.
startIndex
58 assert count > 0,
"Count must be positive"
60 for i
in range(1, count):
61 lineStart = self.
indexOf(
"\n", lineStart)
62 assert lineStart >= 0, f
"This string has only {i - 1} lines, not {count}"
64 lineEnd = self.
indexOf(
"\n", lineStart)
71 def __eq__(self, other: object) -> bool:
74 if isinstance(other, Slice):
80 for i
in range(len(self)):
81 h = 31 * h + ord(self[i])
87 def count(self, char: str) -> int:
94 if len(prefix) > len(self):
96 return all(self[i] == prefix[i]
for i
in range(len(prefix)))
"Slice" subSequence(self, int start, int end)
int count(self, str char)
int indexOf(self, str lookingFor, int startOffset=0)
str __getitem__(self, int index)
"Slice" unixLine(self, int count)
None __init__(self, str base, int startIndex=0, Optional[int] endIndex=None)
bool starts_with(self, str prefix)
int baseLineAtOffset(self, int index)
bool sameAs(self, Union["Slice", str] other)
str replaceSelfWith(self, str s)
bool __eq__(self, object other)