|
| 1 | +from typing import Optional |
| 2 | +from typing import Union |
| 3 | +from collections import Counter |
| 4 | + |
| 5 | +class Slice: |
| 6 | + """Represents a slice of a base string from startIndex to endIndex.""" |
| 7 | + |
| 8 | + def __init__(self, base: str, startIndex: int = 0, endIndex: Optional[int] = None) -> None: |
| 9 | + self.base = base |
| 10 | + self.base = base |
| 11 | + self.startIndex = startIndex |
| 12 | + self.endIndex = endIndex if endIndex is not None else len(base) |
| 13 | + |
| 14 | + assert 0 <= self.startIndex <= self.endIndex <= len(base), "Invalid start or end index" |
| 15 | + |
| 16 | + def __len__(self) -> int: |
| 17 | + return self.endIndex - self.startIndex |
| 18 | + |
| 19 | + def __getitem__(self, index: int) -> str: |
| 20 | + if not (0 <= index < len(self)): |
| 21 | + raise IndexError("Index out of range") |
| 22 | + return self.base[self.startIndex + index] |
| 23 | + |
| 24 | + def subSequence(self, start: int, end: int) -> 'Slice': |
| 25 | + return Slice(self.base, self.startIndex + start, self.startIndex + end) |
| 26 | + |
| 27 | + def trim(self) -> 'Slice': |
| 28 | + start, end = 0, len(self) |
| 29 | + while start < end and self[start].isspace(): |
| 30 | + start += 1 |
| 31 | + while start < end and self[end - 1].isspace(): |
| 32 | + end -= 1 |
| 33 | + return self.subSequence(start, end) if start > 0 or end < len(self) else self |
| 34 | + |
| 35 | + def __str__(self) -> str: |
| 36 | + return self.base[self.startIndex:self.endIndex] |
| 37 | + |
| 38 | + def sameAs(self, other: Union['Slice', str]) -> bool: |
| 39 | + if isinstance(other, Slice): |
| 40 | + return str(self) == str(other) |
| 41 | + elif isinstance(other, str): |
| 42 | + if len(self) != len(other): |
| 43 | + return False |
| 44 | + for i in range(len(self)): |
| 45 | + if self[i] != other[i]: |
| 46 | + return False |
| 47 | + return True |
| 48 | + return False |
| 49 | + |
| 50 | + def indexOf(self, lookingFor: str, startOffset: int = 0) -> int: |
| 51 | + result = self.base.find(lookingFor, self.startIndex + startOffset, self.endIndex) |
| 52 | + return -1 if result == -1 else result - self.startIndex |
| 53 | + |
| 54 | + def unixLine(self, count: int) -> 'Slice': |
| 55 | + assert count > 0, "Count must be positive" |
| 56 | + lineStart = 0 |
| 57 | + for i in range(1, count): |
| 58 | + lineStart = self.indexOf('\n', lineStart) |
| 59 | + assert lineStart >= 0, f"This string has only {i - 1} lines, not {count}" |
| 60 | + lineStart += 1 |
| 61 | + lineEnd = self.indexOf('\n', lineStart) |
| 62 | + return Slice(self.base, self.startIndex + lineStart, self.endIndex if lineEnd == -1 else self.startIndex + lineEnd) |
| 63 | + |
| 64 | + def __eq__(self, other: object) -> bool: |
| 65 | + if self is other: |
| 66 | + return True |
| 67 | + if isinstance(other, Slice): |
| 68 | + return self.sameAs(other) |
| 69 | + return False |
| 70 | + |
| 71 | + def __hash__(self) -> int: |
| 72 | + h = 0 |
| 73 | + for i in range(len(self)): |
| 74 | + h = 31 * h + ord(self[i]) |
| 75 | + return h |
| 76 | + |
| 77 | + def replaceSelfWith(self, s: str) -> str: |
| 78 | + return self.base[:self.startIndex] + s + self.base[self.endIndex:] |
| 79 | + |
| 80 | + def count(self, char: str) -> int: |
| 81 | + return Counter(self.base[self.startIndex:self.endIndex])[char] |
| 82 | + |
| 83 | + def baseLineAtOffset(self, index: int) -> int: |
| 84 | + return 1 + Slice(self.base, 0, index).count('\n') |
0 commit comments