@@ -41,43 +41,40 @@ def escape(self, input_string: str) -> str:
4141            return  "" .join (result )
4242
4343    def  unescape (self , input_string : str ) ->  str :
44-         if  not  input_string :
45-             return  input_string 
46- 
47-         result  =  []
48-         i  =  0 
49- 
50-         while  i  <  len (input_string ):
51-             if  ord (input_string [i ]) ==  self .__escape_code_point :
52-                 if  i  +  1  <  len (input_string ):
53-                     next_char  =  input_string [i  +  1 ]
54-                     next_codepoint  =  ord (next_char )
55- 
56-                     if  next_codepoint  ==  self .__escape_code_point :
57-                         result .append (chr (next_codepoint ))
58-                         i  +=  2 
59-                     else :
60-                         try :
61-                             idx  =  self .__escaped_by_code_points .index (next_codepoint )
62-                             result .append (chr (self .__escaped_code_points [idx ]))
63-                             i  +=  2 
64-                             continue 
65-                         except  ValueError :
66-                             result .append (next_char )
67-                             i  +=  2 
68-                 else :
69-                     raise  ValueError (
70-                         f"Escape character '{ chr (self .__escape_code_point )}  
71-                     )
72-             else :
73-                 result .append (input_string [i ])
74-                 i  +=  1 
44+         if  input_string .endswith (
45+             chr (self .__escape_code_point )
46+         ) and  not  input_string .endswith (chr (self .__escape_code_point ) *  2 ):
47+             raise  ValueError (
48+                 "Escape character '{}' can't be the last character in a string." .format (
49+                     chr (self .__escape_code_point )
50+                 )
51+             )
7552
76-         processed_string  =  "" . join ( result )
77-         if  processed_string  ==  input_string :
53+         no_escapes  =  self . __first_offset_needing_escape ( input_string )
54+         if  no_escapes  ==  - 1 :
7855            return  input_string 
7956        else :
80-             return  processed_string 
57+             result  =  [input_string [:no_escapes ]]
58+             skip_next  =  False 
59+             for  i  in  range (no_escapes , len (input_string )):
60+                 if  skip_next :
61+                     skip_next  =  False 
62+                     continue 
63+                 codepoint  =  ord (input_string [i ])
64+                 if  codepoint  ==  self .__escape_code_point  and  (i  +  1 ) <  len (
65+                     input_string 
66+                 ):
67+                     next_codepoint  =  ord (input_string [i  +  1 ])
68+                     if  next_codepoint  in  self .__escaped_by_code_points :
69+                         idx  =  self .__escaped_by_code_points .index (next_codepoint )
70+                         result .append (chr (self .__escaped_code_points [idx ]))
71+                         skip_next  =  True 
72+                     else :
73+                         result .append (input_string [i  +  1 ])
74+                         skip_next  =  True 
75+                 else :
76+                     result .append (chr (codepoint ))
77+             return  "" .join (result )
8178
8279    @classmethod  
8380    def  self_escape (cls , escape_policy ):
0 commit comments