Format ObjC code with clang-format, WebKit style

This commit is contained in:
Lukhnos Liu 2022-02-19 08:26:28 -08:00
parent ca2f4bf8fd
commit 2091c1dc7d
3 changed files with 92 additions and 103 deletions

View File

@ -29,25 +29,27 @@ extern unsigned short vxSC2TCTable[];
const size_t vxTC2SCTableSize = 3059; const size_t vxTC2SCTableSize = 3059;
extern unsigned short vxTC2SCTable[]; extern unsigned short vxTC2SCTable[];
struct VXHCData struct VXHCData {
{
unsigned short key, value; unsigned short key, value;
}; };
int VXHCCompare(const void *a, const void *b) int VXHCCompare(const void *a, const void *b)
{ {
unsigned short x=((const struct VXHCData*)a)->key, y=((const struct VXHCData*)b)->key; unsigned short x = ((const struct VXHCData *)a)->key, y = ((const struct VXHCData *)b)->key;
if (x==y) return 0; if (x == y)
if (x<y) return -1; return 0;
if (x < y)
return -1;
return 1; return 1;
} }
unsigned short VXHCFind(unsigned key, unsigned short *table, size_t size) unsigned short VXHCFind(unsigned key, unsigned short *table, size_t size)
{ {
struct VXHCData k; struct VXHCData k;
k.key=key; k.key = key;
struct VXHCData *d=(struct VXHCData*)bsearch(&k, table, size, sizeof(struct VXHCData), VXHCCompare); struct VXHCData *d = (struct VXHCData *)bsearch(&k, table, size, sizeof(struct VXHCData), VXHCCompare);
if (!d) return 0; if (!d)
return 0;
return d->value; return d->value;
} }
@ -66,8 +68,8 @@ unsigned short VXUCS2SimpToTradChinese(unsigned short c)
+ (NSString *)convertToSimplifiedFrom:(NSString *)string NS_SWIFT_NAME(convertToSimplified(from:)) + (NSString *)convertToSimplifiedFrom:(NSString *)string NS_SWIFT_NAME(convertToSimplified(from:))
{ {
NSData *utf16Data = [string dataUsingEncoding:NSUTF16StringEncoding]; NSData *utf16Data = [string dataUsingEncoding:NSUTF16StringEncoding];
unsigned short * bytes = (unsigned short *)utf16Data.bytes; unsigned short *bytes = (unsigned short *)utf16Data.bytes;
for(NSInteger i = 0; i < utf16Data.length; i++) { for (NSInteger i = 0; i < utf16Data.length; i++) {
unsigned short c = bytes[i]; unsigned short c = bytes[i];
unsigned short value = VXUCS2TradToSimpChinese(c); unsigned short value = VXUCS2TradToSimpChinese(c);
bytes[i] = value ? value : c; bytes[i] = value ? value : c;
@ -79,8 +81,8 @@ unsigned short VXUCS2SimpToTradChinese(unsigned short c)
+ (NSString *)convertToTraditionalFrom:(NSString *)string NS_SWIFT_NAME(convertToTraditional(from:)) + (NSString *)convertToTraditionalFrom:(NSString *)string NS_SWIFT_NAME(convertToTraditional(from:))
{ {
NSData *utf16Data = [string dataUsingEncoding:NSUTF16StringEncoding]; NSData *utf16Data = [string dataUsingEncoding:NSUTF16StringEncoding];
unsigned short * bytes = (unsigned short *)utf16Data.bytes; unsigned short *bytes = (unsigned short *)utf16Data.bytes;
for(NSInteger i = 0; i < utf16Data.length; i++) { for (NSInteger i = 0; i < utf16Data.length; i++) {
unsigned short c = bytes[i]; unsigned short c = bytes[i];
unsigned short value = VXUCS2SimpToTradChinese(c); unsigned short value = VXUCS2SimpToTradChinese(c);
bytes[i] = value ? value : c; bytes[i] = value ? value : c;

View File

@ -21,13 +21,13 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE. // OTHER DEALINGS IN THE SOFTWARE.
#import "Mandarin.h" #import "KeyHandler.h"
#import "Gramambular.h" #import "Gramambular.h"
#import "LanguageModelManager+Privates.h"
#import "Mandarin.h"
#import "McBopomofo-Swift.h"
#import "McBopomofoLM.h" #import "McBopomofoLM.h"
#import "UserOverrideModel.h" #import "UserOverrideModel.h"
#import "LanguageModelManager+Privates.h"
#import "KeyHandler.h"
#import "McBopomofo-Swift.h"
#import <string> #import <string>
@import CandidateUI; @import CandidateUI;
@ -44,7 +44,8 @@ InputMode InputModePlainBopomofo = @"org.openvanilla.inputmethod.McBopomofo.Plai
static const double kEpsilon = 0.000001; static const double kEpsilon = 0.000001;
static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon) { static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
{
double highestScore = 0.0; double highestScore = 0.0;
for (auto ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) { for (auto ni = nodes.begin(), ne = nodes.end(); ni != ne; ++ni) {
double score = ni->node->highestUnigramScore(); double score = ni->node->highestUnigramScore();
@ -56,10 +57,10 @@ static double FindHighestScore(const vector<NodeAnchor> &nodes, double epsilon)
} }
// sort helper // sort helper
class NodeAnchorDescendingSorter class NodeAnchorDescendingSorter {
{
public: public:
bool operator()(const NodeAnchor &a, const NodeAnchor &b) const { bool operator()(const NodeAnchor &a, const NodeAnchor &b) const
{
return a.node->key().length() > b.node->key().length(); return a.node->key().length() > b.node->key().length();
} }
}; };
@ -70,9 +71,7 @@ public:
static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot"; static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot";
#endif #endif
@implementation KeyHandler {
@implementation KeyHandler
{
// the reading buffer that takes user input // the reading buffer that takes user input
Formosa::Mandarin::BopomofoReadingBuffer *_bpmfReadingBuffer; Formosa::Mandarin::BopomofoReadingBuffer *_bpmfReadingBuffer;
@ -168,27 +167,27 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
{ {
NSInteger layout = Preferences.keyboardLayout; NSInteger layout = Preferences.keyboardLayout;
switch (layout) { switch (layout) {
case KeyboardLayoutStandard: case KeyboardLayoutStandard:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::StandardLayout()); _bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::StandardLayout());
break; break;
case KeyboardLayoutEten: case KeyboardLayoutEten:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::ETenLayout()); _bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::ETenLayout());
break; break;
case KeyboardLayoutHsu: case KeyboardLayoutHsu:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::HsuLayout()); _bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::HsuLayout());
break; break;
case KeyboardLayoutEten26: case KeyboardLayoutEten26:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::ETen26Layout()); _bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::ETen26Layout());
break; break;
case KeyboardLayoutHanyuPinyin: case KeyboardLayoutHanyuPinyin:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::HanyuPinyinLayout()); _bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::HanyuPinyinLayout());
break; break;
case KeyboardLayoutIBM: case KeyboardLayoutIBM:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::IBMLayout()); _bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::IBMLayout());
break; break;
default: default:
_bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::StandardLayout()); _bpmfReadingBuffer->setKeyboardLayout(BopomofoKeyboardLayout::StandardLayout());
Preferences.keyboardLayout = KeyboardLayoutStandard; Preferences.keyboardLayout = KeyboardLayoutStandard;
} }
_languageModel->setExternalConverterEnabled(Preferences.chineseConversionStyle == 1); _languageModel->setExternalConverterEnabled(Preferences.chineseConversionStyle == 1);
} }
@ -218,10 +217,9 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
} }
[self _walk]; [self _walk];
if (Preferences.selectPhraseAfterCursorAsCandidate && if (Preferences.selectPhraseAfterCursorAsCandidate && Preferences.moveCursorAfterSelectingCandidate) {
Preferences.moveCursorAfterSelectingCandidate) {
size_t nextPosition = 0; size_t nextPosition = 0;
for (auto node: _walkedNodes) { for (auto node : _walkedNodes) {
if (nextPosition >= cursorIndex) { if (nextPosition >= cursorIndex) {
break; break;
} }
@ -260,9 +258,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// if the composing buffer is empty and there's no reading, and there is some function key combination, we ignore it // if the composing buffer is empty and there's no reading, and there is some function key combination, we ignore it
BOOL isFunctionKey = ([input isCommandHold] || [input isOptionHold] || [input isNumericPad]) || [input isControlHotKey]; BOOL isFunctionKey = ([input isCommandHold] || [input isOptionHold] || [input isNumericPad]) || [input isControlHotKey];
if (![state isKindOfClass:[InputStateNotEmpty class]] && if (![state isKindOfClass:[InputStateNotEmpty class]] && ![state isKindOfClass:[InputStateAssociatedPhrases class]] && isFunctionKey) {
![state isKindOfClass:[InputStateAssociatedPhrases class]] &&
isFunctionKey) {
return NO; return NO;
} }
@ -321,8 +317,8 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// MARK: Handle Marking // MARK: Handle Marking
if ([state isKindOfClass:[InputStateMarking class]]) { if ([state isKindOfClass:[InputStateMarking class]]) {
InputStateMarking *marking = (InputStateMarking *) state; InputStateMarking *marking = (InputStateMarking *)state;
if ([self _handleMarkingState:(InputStateMarking *) state input:input stateCallback:stateCallback errorCallback:errorCallback]) { if ([self _handleMarkingState:(InputStateMarking *)state input:input stateCallback:stateCallback errorCallback:errorCallback]) {
return YES; return YES;
} }
state = [marking convertToInputting]; state = [marking convertToInputting];
@ -335,8 +331,8 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// MARK: Handle BPMF Keys // MARK: Handle BPMF Keys
// see if it's valid BPMF reading // see if it's valid BPMF reading
if (!skipBpmfHandling && _bpmfReadingBuffer->isValidKey((char) charCode)) { if (!skipBpmfHandling && _bpmfReadingBuffer->isValidKey((char)charCode)) {
_bpmfReadingBuffer->combineKey((char) charCode); _bpmfReadingBuffer->combineKey((char)charCode);
// if we have a tone marker, we have to insert the reading to the // if we have a tone marker, we have to insert the reading to the
// builder in other words, if we don't have a tone marker, we just // builder in other words, if we don't have a tone marker, we just
@ -371,8 +367,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
NSString *poppedText = [self _popOverflowComposingTextAndWalk]; NSString *poppedText = [self _popOverflowComposingTextAndWalk];
// get user override model suggestion // get user override model suggestion
string overrideValue = (_inputMode == InputModePlainBopomofo) ? "" : string overrideValue = (_inputMode == InputModePlainBopomofo) ? "" : _userOverrideModel->suggest(_walkedNodes, _builder->cursorIndex(), [[NSDate date] timeIntervalSince1970]);
_userOverrideModel->suggest(_walkedNodes, _builder->cursorIndex(), [[NSDate date] timeIntervalSince1970]);
if (!overrideValue.empty()) { if (!overrideValue.empty()) {
size_t cursorIndex = [self _actualCandidateCursorIndex]; size_t cursorIndex = [self _actualCandidateCursorIndex];
@ -420,16 +415,15 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// MARK: Space and Down // MARK: Space and Down
// keyCode 125 = Down, charCode 32 = Space // keyCode 125 = Down, charCode 32 = Space
if (_bpmfReadingBuffer->isEmpty() && if (_bpmfReadingBuffer->isEmpty() &&
[state isKindOfClass:[InputStateNotEmpty class]] && [state isKindOfClass:[InputStateNotEmpty class]] && ([input isExtraChooseCandidateKey] || charCode == 32 || (input.useVerticalMode && ([input isVerticalModeOnlyChooseCandidateKey])))) {
([input isExtraChooseCandidateKey] || charCode == 32 || (input.useVerticalMode && ([input isVerticalModeOnlyChooseCandidateKey])))) {
if (charCode == 32) { if (charCode == 32) {
// if the spacebar is NOT set to be a selection key // if the spacebar is NOT set to be a selection key
if ([input isShiftHold] || !Preferences.chooseCandidateUsingSpace) { if ([input isShiftHold] || !Preferences.chooseCandidateUsingSpace) {
if (_builder->cursorIndex() >= _builder->length()) { if (_builder->cursorIndex() >= _builder->length()) {
NSString *composingBuffer = [(InputStateNotEmpty*) state composingBuffer]; NSString *composingBuffer = [(InputStateNotEmpty *)state composingBuffer];
if (composingBuffer.length) { if (composingBuffer.length) {
InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:composingBuffer]; InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:composingBuffer];
stateCallback (committing); stateCallback(committing);
} }
[self clear]; [self clear];
InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:@" "]; InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:@" "];
@ -444,10 +438,9 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
stateCallback(inputting); stateCallback(inputting);
} }
return YES; return YES;
} }
} }
InputStateChoosingCandidate *choosingCandidates = [self _buildCandidateState:(InputStateNotEmpty *) state useVerticalMode:input.useVerticalMode]; InputStateChoosingCandidate *choosingCandidates = [self _buildCandidateState:(InputStateNotEmpty *)state useVerticalMode:input.useVerticalMode];
stateCallback(choosingCandidates); stateCallback(choosingCandidates);
return YES; return YES;
} }
@ -501,7 +494,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
} }
// MARK: Punctuation list // MARK: Punctuation list
if ((char) charCode == '`') { if ((char)charCode == '`') {
if (_languageModel->hasUnigramsForKey(string("_punctuation_list"))) { if (_languageModel->hasUnigramsForKey(string("_punctuation_list"))) {
if (_bpmfReadingBuffer->isEmpty()) { if (_bpmfReadingBuffer->isEmpty()) {
_builder->insertReadingAtCursor(string("_punctuation_list")); _builder->insertReadingAtCursor(string("_punctuation_list"));
@ -530,19 +523,19 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
punctuationNamePrefix = string("_punctuation_"); punctuationNamePrefix = string("_punctuation_");
} }
string layout = [self _currentLayout]; string layout = [self _currentLayout];
string customPunctuation = punctuationNamePrefix + layout + string(1, (char) charCode); string customPunctuation = punctuationNamePrefix + layout + string(1, (char)charCode);
if ([self _handlePunctuation:customPunctuation state:state usingVerticalMode:input.useVerticalMode stateCallback:stateCallback errorCallback:errorCallback]) { if ([self _handlePunctuation:customPunctuation state:state usingVerticalMode:input.useVerticalMode stateCallback:stateCallback errorCallback:errorCallback]) {
return YES; return YES;
} }
// if nothing is matched, see if it's a punctuation key. // if nothing is matched, see if it's a punctuation key.
string punctuation = punctuationNamePrefix + string(1, (char) charCode); string punctuation = punctuationNamePrefix + string(1, (char)charCode);
if ([self _handlePunctuation:punctuation state:state usingVerticalMode:input.useVerticalMode stateCallback:stateCallback errorCallback:errorCallback]) { if ([self _handlePunctuation:punctuation state:state usingVerticalMode:input.useVerticalMode stateCallback:stateCallback errorCallback:errorCallback]) {
return YES; return YES;
} }
if ([state isKindOfClass:[InputStateNotEmpty class]] && (char) charCode >= 'A' && (char) charCode <= 'Z') { if ([state isKindOfClass:[InputStateNotEmpty class]] && (char)charCode >= 'A' && (char)charCode <= 'Z') {
string letter = string("_letter_") + string(1, (char) charCode); string letter = string("_letter_") + string(1, (char)charCode);
if ([self _handlePunctuation:letter state:state usingVerticalMode:input.useVerticalMode stateCallback:stateCallback errorCallback:errorCallback]) { if ([self _handlePunctuation:letter state:state usingVerticalMode:input.useVerticalMode stateCallback:stateCallback errorCallback:errorCallback]) {
return YES; return YES;
} }
@ -607,7 +600,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
return YES; return YES;
} }
InputStateInputting *currentState = (InputStateInputting *) state; InputStateInputting *currentState = (InputStateInputting *)state;
if ([input isShiftHold]) { if ([input isShiftHold]) {
// Shift + left // Shift + left
@ -645,7 +638,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
return YES; return YES;
} }
InputStateInputting *currentState = (InputStateInputting *) state; InputStateInputting *currentState = (InputStateInputting *)state;
if ([input isShiftHold]) { if ([input isShiftHold]) {
// Shift + Right // Shift + Right
@ -809,7 +802,6 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
return YES; return YES;
} }
- (BOOL)_handleEnterWithState:(InputState *)state stateCallback:(void (^)(InputState *))stateCallback errorCallback:(void (^)(void))errorCallback - (BOOL)_handleEnterWithState:(InputState *)state stateCallback:(void (^)(InputState *))stateCallback errorCallback:(void (^)(void))errorCallback
{ {
if (![state isKindOfClass:[InputStateInputting class]]) { if (![state isKindOfClass:[InputStateInputting class]]) {
@ -818,7 +810,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
[self clear]; [self clear];
InputStateInputting *current = (InputStateInputting *) state; InputStateInputting *current = (InputStateInputting *)state;
NSString *composingBuffer = current.composingBuffer; NSString *composingBuffer = current.composingBuffer;
InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:composingBuffer]; InputStateCommitting *committing = [[InputStateCommitting alloc] initWithPoppedText:composingBuffer];
stateCallback(committing); stateCallback(committing);
@ -863,7 +855,6 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
return YES; return YES;
} }
- (BOOL)_handleMarkingState:(InputStateMarking *)state - (BOOL)_handleMarkingState:(InputStateMarking *)state
input:(KeyHandlerInput *)input input:(KeyHandlerInput *)input
stateCallback:(void (^)(InputState *))stateCallback stateCallback:(void (^)(InputState *))stateCallback
@ -890,7 +881,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// Shift + left // Shift + left
if (([input isCursorBackward] || input.emacsKey == McBopomofoEmacsKeyBackward) if (([input isCursorBackward] || input.emacsKey == McBopomofoEmacsKeyBackward)
&& ([input isShiftHold])) { && ([input isShiftHold])) {
NSUInteger index = state.markerIndex; NSUInteger index = state.markerIndex;
if (index > 0) { if (index > 0) {
index = [state.composingBuffer previousUtf16PositionFor:index]; index = [state.composingBuffer previousUtf16PositionFor:index];
@ -912,7 +903,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
// Shift + Right // Shift + Right
if (([input isCursorForward] || input.emacsKey == McBopomofoEmacsKeyForward) if (([input isCursorForward] || input.emacsKey == McBopomofoEmacsKeyForward)
&& ([input isShiftHold])) { && ([input isShiftHold])) {
NSUInteger index = state.markerIndex; NSUInteger index = state.markerIndex;
if (index < state.composingBuffer.length) { if (index < state.composingBuffer.length) {
index = [state.composingBuffer nextUtf16PositionFor:index]; index = [state.composingBuffer nextUtf16PositionFor:index];
@ -933,7 +924,6 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
return NO; return NO;
} }
- (BOOL)_handleCandidateState:(InputState *)state - (BOOL)_handleCandidateState:(InputState *)state
input:(KeyHandlerInput *)input input:(KeyHandlerInput *)input
stateCallback:(void (^)(InputState *))stateCallback stateCallback:(void (^)(InputState *))stateCallback
@ -946,12 +936,11 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
BOOL cancelCandidateKey = (charCode == 27) || (charCode == 8) || [input isDelete]; BOOL cancelCandidateKey = (charCode == 27) || (charCode == 8) || [input isDelete];
if (cancelCandidateKey) { if (cancelCandidateKey) {
if ([state isKindOfClass: [InputStateAssociatedPhrases class]]) { if ([state isKindOfClass:[InputStateAssociatedPhrases class]]) {
[self clear]; [self clear];
InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init]; InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init];
stateCallback(empty); stateCallback(empty);
} } else if (_inputMode == InputModePlainBopomofo) {
else if (_inputMode == InputModePlainBopomofo) {
[self clear]; [self clear];
InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init]; InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init];
stateCallback(empty); stateCallback(empty);
@ -963,7 +952,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
} }
if (charCode == 13 || [input isEnter]) { if (charCode == 13 || [input isEnter]) {
if ([state isKindOfClass: [InputStateAssociatedPhrases class]]) { if ([state isKindOfClass:[InputStateAssociatedPhrases class]]) {
[self clear]; [self clear];
InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init]; InputStateEmptyIgnoringPreviousState *empty = [[InputStateEmptyIgnoringPreviousState alloc] init];
stateCallback(empty); stateCallback(empty);
@ -1077,9 +1066,9 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
NSArray *candidates; NSArray *candidates;
if ([state isKindOfClass: [InputStateChoosingCandidate class]]) { if ([state isKindOfClass:[InputStateChoosingCandidate class]]) {
candidates = [(InputStateChoosingCandidate *)state candidates]; candidates = [(InputStateChoosingCandidate *)state candidates];
} else if ([state isKindOfClass: [InputStateAssociatedPhrases class]]) { } else if ([state isKindOfClass:[InputStateAssociatedPhrases class]]) {
candidates = [(InputStateAssociatedPhrases *)state candidates]; candidates = [(InputStateAssociatedPhrases *)state candidates];
} }
@ -1140,14 +1129,13 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
} else { } else {
punctuationNamePrefix = string("_punctuation_"); punctuationNamePrefix = string("_punctuation_");
} }
string customPunctuation = punctuationNamePrefix + layout + string(1, (char) charCode); string customPunctuation = punctuationNamePrefix + layout + string(1, (char)charCode);
string punctuation = punctuationNamePrefix + string(1, (char) charCode); string punctuation = punctuationNamePrefix + string(1, (char)charCode);
BOOL shouldAutoSelectCandidate = _bpmfReadingBuffer->isValidKey((char) charCode) || _languageModel->hasUnigramsForKey(customPunctuation) || BOOL shouldAutoSelectCandidate = _bpmfReadingBuffer->isValidKey((char)charCode) || _languageModel->hasUnigramsForKey(customPunctuation) || _languageModel->hasUnigramsForKey(punctuation);
_languageModel->hasUnigramsForKey(punctuation);
if (!shouldAutoSelectCandidate && (char) charCode >= 'A' && (char) charCode <= 'Z') { if (!shouldAutoSelectCandidate && (char)charCode >= 'A' && (char)charCode <= 'Z') {
string letter = string("_letter_") + string(1, (char) charCode); string letter = string("_letter_") + string(1, (char)charCode);
if (_languageModel->hasUnigramsForKey(letter)) { if (_languageModel->hasUnigramsForKey(letter)) {
shouldAutoSelectCandidate = YES; shouldAutoSelectCandidate = YES;
} }
@ -1221,14 +1209,14 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
} }
if (builderCursorIndex == 0) { if (builderCursorIndex == 0) {
tooltip = [NSString stringWithFormat:NSLocalizedString(@"Cursor is before \"%@\".", @""), tooltip = [NSString stringWithFormat:NSLocalizedString(@"Cursor is before \"%@\".", @""),
[NSString stringWithUTF8String:_builder->readings()[builderCursorIndex].c_str()]]; [NSString stringWithUTF8String:_builder->readings()[builderCursorIndex].c_str()]];
} else if (builderCursorIndex >= _builder->readings().size()) { } else if (builderCursorIndex >= _builder->readings().size()) {
tooltip = [NSString stringWithFormat:NSLocalizedString(@"Cursor is after \"%@\".", @""), tooltip = [NSString stringWithFormat:NSLocalizedString(@"Cursor is after \"%@\".", @""),
[NSString stringWithUTF8String:_builder->readings()[_builder->readings().size() - 1].c_str()]]; [NSString stringWithUTF8String:_builder->readings()[_builder->readings().size() - 1].c_str()]];
} else { } else {
tooltip = [NSString stringWithFormat:NSLocalizedString(@"Cursor is between \"%@\" and \"%@\".", @""), tooltip = [NSString stringWithFormat:NSLocalizedString(@"Cursor is between \"%@\" and \"%@\".", @""),
[NSString stringWithUTF8String:_builder->readings()[builderCursorIndex - 1].c_str()], [NSString stringWithUTF8String:_builder->readings()[builderCursorIndex - 1].c_str()],
[NSString stringWithUTF8String:_builder->readings()[builderCursorIndex].c_str()]]; [NSString stringWithUTF8String:_builder->readings()[builderCursorIndex].c_str()]];
} }
} }
} }
@ -1286,7 +1274,7 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
NSString *poppedText = @""; NSString *poppedText = @"";
NSInteger composingBufferSize = Preferences.composingBufferSize; NSInteger composingBufferSize = Preferences.composingBufferSize;
if (_builder->grid().width() > (size_t) composingBufferSize) { if (_builder->grid().width() > (size_t)composingBufferSize) {
if (_walkedNodes.size() > 0) { if (_walkedNodes.size() > 0) {
NodeAnchor &anchor = _walkedNodes[0]; NodeAnchor &anchor = _walkedNodes[0];
poppedText = [NSString stringWithUTF8String:anchor.node->currentKeyValue().value.c_str()]; poppedText = [NSString stringWithUTF8String:anchor.node->currentKeyValue().value.c_str()];
@ -1352,8 +1340,8 @@ static NSString *const kGraphVizOutputfile = @"/tmp/McBopomofo-visualization.dot
string cppKey = string(key.UTF8String); string cppKey = string(key.UTF8String);
if (_languageModel->hasAssociatedPhrasesForKey(cppKey)) { if (_languageModel->hasAssociatedPhrasesForKey(cppKey)) {
vector<string> phrases = _languageModel->associatedPhrasesForKey(cppKey); vector<string> phrases = _languageModel->associatedPhrasesForKey(cppKey);
NSMutableArray <NSString *> *array = [NSMutableArray array]; NSMutableArray<NSString *> *array = [NSMutableArray array];
for (auto phrase: phrases) { for (auto phrase : phrases) {
NSString *item = [[NSString alloc] initWithUTF8String:phrase.c_str()]; NSString *item = [[NSString alloc] initWithUTF8String:phrase.c_str()];
[array addObject:item]; [array addObject:item];
} }

View File

@ -32,7 +32,7 @@ using namespace std;
using namespace McBopomofo; using namespace McBopomofo;
static const int kUserOverrideModelCapacity = 500; static const int kUserOverrideModelCapacity = 500;
static const double kObservedOverrideHalflife = 5400.0; // 1.5 hr. static const double kObservedOverrideHalflife = 5400.0; // 1.5 hr.
static McBopomofoLM gLanguageModelMcBopomofo; static McBopomofoLM gLanguageModelMcBopomofo;
static McBopomofoLM gLanguageModelPlainBopomofo; static McBopomofoLM gLanguageModelPlainBopomofo;
@ -104,7 +104,7 @@ static void LTLoadAssociatedPhrases(McBopomofoLM &lm)
+ (void)setupDataModelValueConverter + (void)setupDataModelValueConverter
{ {
auto converter = [] (string input) { auto converter = [](string input) {
if (!Preferences.chineseConversionEnabled) { if (!Preferences.chineseConversionEnabled) {
return input; return input;
} }
@ -116,8 +116,7 @@ static void LTLoadAssociatedPhrases(McBopomofoLM &lm)
NSString *text = [NSString stringWithUTF8String:input.c_str()]; NSString *text = [NSString stringWithUTF8String:input.c_str()];
if (Preferences.chineseConversionEngine == 1) { if (Preferences.chineseConversionEngine == 1) {
text = [VXHanConvert convertToSimplifiedFrom:text]; text = [VXHanConvert convertToSimplifiedFrom:text];
} } else {
else {
text = [OpenCCBridge convertToSimplified:text]; text = [OpenCCBridge convertToSimplified:text];
} }
return string(text.UTF8String); return string(text.UTF8String);
@ -198,7 +197,7 @@ static void LTLoadAssociatedPhrases(McBopomofoLM &lm)
string unigramKey = string(key.UTF8String); string unigramKey = string(key.UTF8String);
vector<Unigram> unigrams = gLanguageModelMcBopomofo.unigramsForKey(unigramKey); vector<Unigram> unigrams = gLanguageModelMcBopomofo.unigramsForKey(unigramKey);
string userPhraseString = string(userPhrase.UTF8String); string userPhraseString = string(userPhrase.UTF8String);
for (auto unigram: unigrams) { for (auto unigram : unigrams) {
if (unigram.keyValue.value == userPhraseString) { if (unigram.keyValue.value == userPhraseString) {
return YES; return YES;
} }
@ -249,9 +248,9 @@ static void LTLoadAssociatedPhrases(McBopomofoLM &lm)
[writeFile writeData:data]; [writeFile writeData:data];
[writeFile closeFile]; [writeFile closeFile];
// We use FSEventStream to monitor the change of the user phrase folder, // We use FSEventStream to monitor the change of the user phrase folder,
// so we don't have to load data here. // so we don't have to load data here.
// [self loadUserPhrases]; // [self loadUserPhrases];
return YES; return YES;
} }
@ -283,7 +282,7 @@ static void LTLoadAssociatedPhrases(McBopomofoLM &lm)
return [[self dataFolderPath] stringByAppendingPathComponent:@"phrases-replacement.txt"]; return [[self dataFolderPath] stringByAppendingPathComponent:@"phrases-replacement.txt"];
} }
+ (McBopomofoLM *)languageModelMcBopomofo + (McBopomofoLM *)languageModelMcBopomofo
{ {
return &gLanguageModelMcBopomofo; return &gLanguageModelMcBopomofo;
} }