Skip to content
2 changes: 2 additions & 0 deletions Source/Engine/Mandarin/Mandarin.h
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ class BopomofoReadingBuffer {

const BPMF syllable() const { return syllable_; }

void setsSyllable(BPMF syllable) { syllable_ = syllable; }
Comment thread
zonble marked this conversation as resolved.
Outdated
Comment thread
zonble marked this conversation as resolved.
Outdated
Comment thread
zonble marked this conversation as resolved.
Outdated

const std::string standardLayoutQueryString() const {
return BopomofoKeyboardLayout::StandardLayout()->keySequenceFromSyllable(
syllable_);
Expand Down
32 changes: 30 additions & 2 deletions Source/KeyHandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,8 @@ - (BOOL)handleInput:(KeyHandlerInput *)input state:(InputState *)inState stateCa
// MARK: Handle BPMF Keys

// see if it's valid BPMF reading
if (!skipBpmfHandling && _bpmfReadingBuffer->isValidKey((char)charCode)) {
bool isValidKey = _bpmfReadingBuffer->isValidKey((char)charCode);
if (!skipBpmfHandling && isValidKey) {
_bpmfReadingBuffer->combineKey((char)charCode);
keyConsumedByReading = YES;

Expand All @@ -463,7 +464,34 @@ - (BOOL)handleInput:(KeyHandlerInput *)input state:(InputState *)inState stateCa
}
}

BOOL composeReading = _bpmfReadingBuffer->isValidKey((char)charCode) && _bpmfReadingBuffer->hasToneMarker() && !_bpmfReadingBuffer->hasToneMarkerOnly();
// Issue 753
//
// This allows users to use tone key to change an existing reading before
// the current cursor.
if (_bpmfReadingBuffer->hasToneMarkerOnly() && _grid->readings().size() > 0 && _grid->cursor() > 0) {
Comment thread
zonble marked this conversation as resolved.
Outdated
size_t cursor = _grid->cursor() - 1;
std::string reading = _grid->readings()[cursor];

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To avoid a potentially unnecessary string copy, you can declare reading as a const reference.

Suggested change
std::string reading = _grid->readings()[cursor];
const std::string& reading = _grid->readings()[cursor];

if (reading.rfind(std::string("_"), 0) != 0) {
Comment thread
zonble marked this conversation as resolved.
Outdated
Formosa::Mandarin::BopomofoReadingBuffer *tmpBuffer = new Formosa::Mandarin::BopomofoReadingBuffer(_bpmfReadingBuffer->keyboardLayout());
Formosa::Mandarin::BopomofoSyllable syllable = Formosa::Mandarin::BopomofoSyllable::FromComposedString(reading);
tmpBuffer->setsSyllable(syllable);
tmpBuffer->combineKey((char)charCode);
std::string newReading = tmpBuffer->syllable().composedString();
delete tmpBuffer;
Comment thread
zonble marked this conversation as resolved.
Outdated
Comment thread
zonble marked this conversation as resolved.
Outdated
if (_languageModel->hasUnigrams(newReading)) {
_bpmfReadingBuffer->clear();
_grid->deleteReadingBeforeCursor();
_grid->insertReading(newReading);
[self _walk];
InputStateInputting *inputting = (InputStateInputting *)[self buildInputtingState];
stateCallback(inputting);
return YES;
}
}
}
Comment thread
zonble marked this conversation as resolved.
Outdated


Comment thread
zonble marked this conversation as resolved.
Outdated
BOOL composeReading = isValidKey && _bpmfReadingBuffer->hasToneMarker() && !_bpmfReadingBuffer->hasToneMarkerOnly();

// see if we have composition if Enter/Space is hit and buffer is not empty
// this is bit-OR'ed so that the tone marker key is also taken into account
Expand Down