Skip to content

Commit 06977b5

Browse files
committed
Implement handle_backspace()
1 parent 24c77d5 commit 06977b5

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

bogo/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@
2828
process_key, \
2929
process_sequence, \
3030
get_telex_definition, \
31-
get_vni_definition
31+
get_vni_definition, \
32+
handle_backspace

bogo/core.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)