Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
Make changes to PerCharacterEscaper, uses __escape_code_point
Browse files Browse the repository at this point in the history
  • Loading branch information
Trickybrain committed Mar 8, 2024
1 parent 34db5b8 commit 236bd6b
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions python/selfie-lib/selfie_lib/PerCharacterEscaper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List, Optional
from typing import List


class PerCharacterEscaper:
Expand All @@ -12,16 +12,12 @@ def __init__(
self.__escaped_code_points = escaped_code_points
self.__escaped_by_code_points = escaped_by_code_points

def __first_offset_needing_escape(
self, input_string: str, escape_code_point: Optional[int] = None
) -> int:
if escape_code_point is None:
escape_code_point = self.__escape_code_point
def __first_offset_needing_escape(self, input_string: str) -> int:
length = len(input_string)
for offset in range(length):
codepoint = ord(input_string[offset])
if (
codepoint == escape_code_point
codepoint == self.__escape_code_point
or codepoint in self.__escaped_code_points
):
return offset
Expand All @@ -45,41 +41,43 @@ def escape(self, input_string: str) -> str:
return "".join(result)

def unescape(self, input_string: str) -> str:
if input_string.endswith(
chr(self.__escape_code_point)
) and not input_string.endswith(chr(self.__escape_code_point) * 2):
raise ValueError(
f"Escape character '{chr(self.__escape_code_point)}' can't be the last character in a string."
)

no_escapes = self.__first_offset_needing_escape(
input_string, self.__escape_code_point
)
if no_escapes == -1:
if not input_string:
return input_string
else:
result = []
result.append(input_string[:no_escapes])
skip_next = False
for i in range(no_escapes, len(input_string)):
if skip_next:
skip_next = False
continue
codepoint = ord(input_string[i])
if codepoint == self.__escape_code_point and (i + 1) < len(
input_string
):
next_codepoint = ord(input_string[i + 1])
if next_codepoint in self.__escaped_by_code_points:
idx = self.__escaped_by_code_points.index(next_codepoint)
result.append(chr(self.__escaped_code_points[idx]))
skip_next = True

result = []
i = 0

while i < len(input_string):
if ord(input_string[i]) == self.__escape_code_point:
if i + 1 < len(input_string):
next_char = input_string[i + 1]
next_codepoint = ord(next_char)

if next_codepoint == self.__escape_code_point:
result.append(chr(next_codepoint))
i += 2
else:
result.append(input_string[i + 1])
skip_next = True
try:
idx = self.__escaped_by_code_points.index(next_codepoint)
result.append(chr(self.__escaped_code_points[idx]))
i += 2
continue
except ValueError:
result.append(next_char)
i += 2
else:
result.append(chr(codepoint))
return "".join(result)
raise ValueError(
f"Escape character '{chr(self.__escape_code_point)}' can't be the last character in a string."
)
else:
result.append(input_string[i])
i += 1

processed_string = "".join(result)
if processed_string == input_string:
return input_string
else:
return processed_string

@classmethod
def self_escape(cls, escape_policy):
Expand Down

0 comments on commit 236bd6b

Please sign in to comment.