Selfie
Loading...
Searching...
No Matches
Slice.py
Go to the documentation of this file.
1from collections import Counter
2from typing import Optional, Union
3
4
5class Slice:
6 """Represents a slice of a base string from startIndex to endIndex."""
7
9 self, base: str, startIndex: int = 0, endIndex: Optional[int] = None
10 ) -> None:
11 self.base = base
12 self.base = base
13 self.startIndex = startIndex
14 self.endIndex = endIndex if endIndex is not None else len(base)
15
16 assert (
17 0 <= self.startIndex <= self.endIndex <= len(base)
18 ), "Invalid start or end index"
19
20 def __len__(self) -> int:
21 return self.endIndex - self.startIndex
22
23 def __getitem__(self, index: int) -> str:
24 if not (0 <= index < len(self)):
25 raise IndexError("Index out of range")
26 return self.base[self.startIndex + index]
27
28 def subSequence(self, start: int, end: int) -> "Slice":
29 return Slice(self.base, self.startIndex + start, self.startIndex + end)
30
31 def trim(self) -> "Slice":
32 start, end = 0, len(self)
33 while start < end and self[start].isspace():
34 start += 1
35 while start < end and self[end - 1].isspace():
36 end -= 1
37 return self.subSequence(start, end) if start > 0 or end < len(self) else self
38
39 def __str__(self) -> str:
40 return self.base[self.startIndex : self.endIndex]
41
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):
47 return False
48 return all(self[i] == other[i] for i in range(len(self)))
49 return False
50
51 def indexOf(self, lookingFor: str, startOffset: int = 0) -> int:
52 result = self.base.find(
53 lookingFor, self.startIndex + startOffset, self.endIndex
54 )
55 return -1 if result == -1 else result - self.startIndex
56
57 def unixLine(self, count: int) -> "Slice":
58 assert count > 0, "Count must be positive"
59 lineStart = 0
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}"
63 lineStart += 1
64 lineEnd = self.indexOf("\n", lineStart)
65 return Slice(
66 self.base,
67 self.startIndex + lineStart,
68 self.endIndex if lineEnd == -1 else self.startIndex + lineEnd,
69 )
70
71 def __eq__(self, other: object) -> bool:
72 if self is other:
73 return True
74 if isinstance(other, Slice):
75 return self.sameAs(other)
76 return False
77
78 def __hash__(self) -> int:
79 h = 0
80 for i in range(len(self)):
81 h = 31 * h + ord(self[i])
82 return h
83
84 def replaceSelfWith(self, s: str) -> str:
85 return self.base[: self.startIndex] + s + self.base[self.endIndex :]
86
87 def count(self, char: str) -> int:
88 return Counter(self.base[self.startIndex : self.endIndex])[char]
89
90 def baseLineAtOffset(self, index: int) -> int:
91 return 1 + Slice(self.base, 0, index).count("\n")
92
93 def starts_with(self, prefix: str) -> bool:
94 if len(prefix) > len(self):
95 return False
96 return all(self[i] == prefix[i] for i in range(len(prefix)))
"Slice" subSequence(self, int start, int end)
Definition Slice.py:28
int count(self, str char)
Definition Slice.py:87
int indexOf(self, str lookingFor, int startOffset=0)
Definition Slice.py:51
"Slice" trim(self)
Definition Slice.py:31
str __getitem__(self, int index)
Definition Slice.py:23
int __len__(self)
Definition Slice.py:20
"Slice" unixLine(self, int count)
Definition Slice.py:57
None __init__(self, str base, int startIndex=0, Optional[int] endIndex=None)
Definition Slice.py:10
int __hash__(self)
Definition Slice.py:78
bool starts_with(self, str prefix)
Definition Slice.py:93
int baseLineAtOffset(self, int index)
Definition Slice.py:90
bool sameAs(self, Union["Slice", str] other)
Definition Slice.py:42
str replaceSelfWith(self, str s)
Definition Slice.py:84
bool __eq__(self, object other)
Definition Slice.py:71
str __str__(self)
Definition Slice.py:39