@@ -487,3 +487,29 @@ def atomic_check(action):
487487 accent .remove_accent_char (comps [1 ][- 1 ])) # ơ, ư
488488
489489 return any (map (atomic_check , action_list ))
490+
491+
492+ def handle_backspace (converted_string , raw_sequence ):
493+ """
494+ Returns a new converted_string and a new raw_sequence
495+ after a backspace.
496+ """
497+ # I can't find a simple explanation for this, so
498+ # I hope this example can help clarify it:
499+ #
500+ # handle_backspace(thương, thuwongw) -> (thươn, thuwonw)
501+ # handle_backspace(thươn, thuwonw) -> (thươ, thuwow)
502+ # handle_backspace(thươ, thuwow) -> (thư, thuw)
503+ # handle_backspace(thươ, thuw) -> (th, th)
504+ #
505+ # The algorithm for handle_backspace was contributed by @hainp.
506+
507+ deleted_char = converted_string [- 1 ]
508+ converted_string = converted_string [:- 1 ]
509+
510+ index = raw_sequence .rfind (deleted_char )
511+ raw_sequence = raw_sequence [:- 2 ] if index < 0 else \
512+ raw_sequence [:index ] + \
513+ raw_sequence [(index + 1 ):]
514+
515+ return converted_string , raw_sequence
0 commit comments