@@ -235,51 +235,54 @@ ldml_processor::process_event(
235235 default :
236236 // all other VKs
237237 {
238- // adapted from kmx_processor.cpp
239-
240- /* * a copy of the current/changed context, for transform use */
241- std::u32string ctxtstr;
242-
243- // Construct a context buffer of all the KM_KBP_BT_CHAR items
244- // Extract the context into 'ctxt' for transforms to process
245- if (!!transforms) {
246- // if no transforms, no reason to do this extraction (ctxt will remain empty)
247- (void )context_to_string (state, ctxtstr);
248- }
249-
250238 // Look up the key
251239 const std::u16string str = keys.lookup (vk, modifier_state);
252240
253241 if (str.empty ()) {
254- // not found, so pass the keystroke on to the Engine
242+ // no key was found, so pass the keystroke on to the Engine
255243 state->actions ().push_invalidate_context ();
256244 state->actions ().push_emit_keystroke ();
257245 break ; // ----- commit and exit
258246 }
259- // found the correct string - push it into the context and actions
260- emit_text (state, str);
261- // Now process transforms
262- // Process the transforms
247+
248+ /* *
249+ * a copy of the current/changed context, for transform use.
250+ *
251+ */
252+ std::u32string ctxtstr;
253+
254+ if (!!transforms) {
255+ (void )context_to_string (state, ctxtstr);
256+ } // else, don't bother populating
257+
258+ // found a string - push it into the context and actions
259+ // we convert it here instead of using the emit_text() overload
260+ // so that we don't have to reconvert it inside the transform code.
261+ const std::u32string str32 = kmx::u16string_to_u32string (str);
262+ emit_text (state, str32);
263+
264+ // Now, process the actual transforms
263265 if (!!transforms) {
264- const std::u32string str32 = kmx::u16string_to_u32string (str);
265- // add the newly added char to ctxt
266+ // add the newly added char to ctxtstr
266267 ctxtstr.append (str32);
267268
269+ /* * the output buffer for transforms */
268270 std::u32string outputString;
269271
270- // check if the context matched, and if so how much (at the end)
272+ // apply the transform, get how much matched (at the end)
271273 const size_t matchedContext = transforms->apply (ctxtstr, outputString);
272274
273275 if (matchedContext > 0 ) {
274- // Found something .
275- // Now, delete some of the old context
276- /* * how much of the context we need to clear */
276+ // We have a match .
277+
278+ /* * how many chars of the context we need to clear */
277279 auto charsToDelete = matchedContext;
278- // for now: ignore ctxtstr, just work with context
279- // TODO-LDML: assert that ctxtstr matches
280+
281+ /* * how many context items need to be removed */
280282 size_t contextRemoved = 0 ;
281283 for (auto c = state->context ().rbegin (); charsToDelete > 0 && c != state->context ().rend (); c++, contextRemoved++) {
282- km_kbp_usv lastCtx = ctxtstr[ctxtstr.length ()-1 ];
284+ /* * last char of context */
285+ km_kbp_usv lastCtx = ctxtstr.back ();
283286 uint8_t type = c->type ;
284287 assert (type == KM_KBP_BT_CHAR || type == KM_KBP_BT_MARKER);
285288 if (type == KM_KBP_BT_CHAR) {
@@ -291,21 +294,28 @@ ldml_processor::process_event(
291294 } else if (type == KM_KBP_BT_MARKER) {
292295 // it's a marker, 'worth' 3 uchars
293296 assert (charsToDelete >= 3 );
294- charsToDelete -= 3 ;
295297 assert (lastCtx == c->marker ); // end of list
298+ charsToDelete -= 3 ;
299+ // pop off the three-part sentinel string
296300 ctxtstr.pop_back ();
297301 ctxtstr.pop_back ();
298302 ctxtstr.pop_back ();
303+ // push a special backsapce to delete the marker
299304 state->actions ().push_backspace (KM_KBP_BT_MARKER, c->marker );
300305 }
301306 }
302307 // now, pop the right number of context items
303308 for (size_t i = 0 ; i < contextRemoved; i++) {
309+ // we don't pop during the above loop because the iterator gets confused
304310 state->context ().pop_back ();
305311 }
306- // Now, add in the updated text
312+ // Now, add in the updated text. This will convert UC_SENTINEL, etc back to marker actions.
307313 emit_text (state, outputString);
314+ // If we needed it further. we could update ctxtstr here:
315+ // ctxtstr.append(outputString);
316+ // ... but it is no longer needed at this point.
308317 }
318+ // else: no match
309319 }
310320 }
311321 }
0 commit comments