From 9e0c711a6230af1db0753af401804c95e4bee69d Mon Sep 17 00:00:00 2001 From: Ken Wakasa Date: Thu, 9 Aug 2012 22:26:58 +0900 Subject: [PATCH] Stop using STL string in additional_proximity_chars Change-Id: Ic016fd5983b8855b0fd9506a17b205db86af3a2f --- native/jni/src/additional_proximity_chars.cpp | 4 +++- native/jni/src/additional_proximity_chars.h | 20 +++++++++---------- native/jni/src/defines.h | 3 +++ native/jni/src/proximity_info.cpp | 12 +++++------ native/jni/src/proximity_info.h | 7 +++---- 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/native/jni/src/additional_proximity_chars.cpp b/native/jni/src/additional_proximity_chars.cpp index de8764678..f59492741 100644 --- a/native/jni/src/additional_proximity_chars.cpp +++ b/native/jni/src/additional_proximity_chars.cpp @@ -17,7 +17,9 @@ #include "additional_proximity_chars.h" namespace latinime { -const std::string AdditionalProximityChars::LOCALE_EN_US("en"); +// TODO: Stop using hardcoded additional proximity characters. +// TODO: Have proximity character informations in each language's binary dictionary. +const char *AdditionalProximityChars::LOCALE_EN_US = "en"; const int32_t AdditionalProximityChars::EN_US_ADDITIONAL_A[EN_US_ADDITIONAL_A_SIZE] = { 'e', 'i', 'o', 'u' diff --git a/native/jni/src/additional_proximity_chars.h b/native/jni/src/additional_proximity_chars.h index c22de7d3d..1fe996d0d 100644 --- a/native/jni/src/additional_proximity_chars.h +++ b/native/jni/src/additional_proximity_chars.h @@ -17,8 +17,8 @@ #ifndef LATINIME_ADDITIONAL_PROXIMITY_CHARS_H #define LATINIME_ADDITIONAL_PROXIMITY_CHARS_H +#include #include -#include #include "defines.h" @@ -27,7 +27,7 @@ namespace latinime { class AdditionalProximityChars { private: DISALLOW_IMPLICIT_CONSTRUCTORS(AdditionalProximityChars); - static const std::string LOCALE_EN_US; + static const char *LOCALE_EN_US; static const int EN_US_ADDITIONAL_A_SIZE = 4; static const int32_t EN_US_ADDITIONAL_A[]; static const int EN_US_ADDITIONAL_E_SIZE = 4; @@ -39,15 +39,15 @@ class AdditionalProximityChars { static const int EN_US_ADDITIONAL_U_SIZE = 4; static const int32_t EN_US_ADDITIONAL_U[]; - static bool isEnLocale(const std::string *locale_str) { - const size_t LOCALE_EN_US_SIZE = LOCALE_EN_US.size(); - return locale_str && locale_str->size() >= LOCALE_EN_US_SIZE - && locale_str->compare(0, LOCALE_EN_US_SIZE, LOCALE_EN_US) == 0; + static bool isEnLocale(const char *localeStr) { + const size_t LOCALE_EN_US_SIZE = strlen(LOCALE_EN_US); + return localeStr && strlen(localeStr) >= LOCALE_EN_US_SIZE + && strncmp(localeStr, LOCALE_EN_US, LOCALE_EN_US_SIZE) == 0; } public: - static int getAdditionalCharsSize(const std::string *locale_str, const int32_t c) { - if (!isEnLocale(locale_str)) { + static int getAdditionalCharsSize(const char *localeStr, const int32_t c) { + if (!isEnLocale(localeStr)) { return 0; } switch(c) { @@ -66,8 +66,8 @@ class AdditionalProximityChars { } } - static const int32_t *getAdditionalChars(const std::string *locale_str, const int32_t c) { - if (!isEnLocale(locale_str)) { + static const int32_t *getAdditionalChars(const char *localeStr, const int32_t c) { + if (!isEnLocale(localeStr)) { return 0; } switch(c) { diff --git a/native/jni/src/defines.h b/native/jni/src/defines.h index 31dd61e30..484fc6bde 100644 --- a/native/jni/src/defines.h +++ b/native/jni/src/defines.h @@ -265,6 +265,9 @@ static inline void prof_out(void) { // This must be equal to ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE in KeyDetector.java #define ADDITIONAL_PROXIMITY_CHAR_DELIMITER_CODE 2 +// Assuming locale strings such as en_US, sr-Latn etc. +#define MAX_LOCALE_STRING_LENGTH 10 + // Word limit for sub queues used in WordsPriorityQueuePool. Sub queues are temporary queues used // for better performance. // Holds up to 1 candidate for each word diff --git a/native/jni/src/proximity_info.cpp b/native/jni/src/proximity_info.cpp index 4f6507e53..2564c8a67 100644 --- a/native/jni/src/proximity_info.cpp +++ b/native/jni/src/proximity_info.cpp @@ -17,7 +17,6 @@ #include #include #include -#include #define LOG_TAG "LatinIME: proximity_info.cpp" @@ -68,10 +67,12 @@ ProximityInfo::ProximityInfo(JNIEnv *env, const jstring localeJStr, const int ma AKLOGI("Create proximity info array %d", proximityGridLength); } const jsize localeCStrUtf8Length = env->GetStringUTFLength(localeJStr); - char localeCStr[localeCStrUtf8Length + 1]; - env->GetStringUTFRegion(localeJStr, 0, env->GetStringLength(localeJStr), localeCStr); - localeCStr[localeCStrUtf8Length] = '\0'; - mLocaleStr = new std::string(localeCStr); + if (localeCStrUtf8Length >= MAX_LOCALE_STRING_LENGTH) { + AKLOGI("Locale string length too long: length=%d", localeCStrUtf8Length); + assert(false); + } + memset(mLocaleStr, 0, sizeof(mLocaleStr)); + env->GetStringUTFRegion(localeJStr, 0, env->GetStringLength(localeJStr), mLocaleStr); mProximityCharsArray = new int32_t[proximityGridLength]; safeGetOrFillZeroIntArrayRegion(env, proximityChars, proximityGridLength, mProximityCharsArray); safeGetOrFillZeroIntArrayRegion(env, keyXCoordinates, KEY_COUNT, mKeyXCoordinates); @@ -98,7 +99,6 @@ void ProximityInfo::initializeCodeToKeyIndex() { } ProximityInfo::~ProximityInfo() { - delete mLocaleStr; delete[] mProximityCharsArray; } diff --git a/native/jni/src/proximity_info.h b/native/jni/src/proximity_info.h index 7222d0d5d..d7e24c5f5 100644 --- a/native/jni/src/proximity_info.h +++ b/native/jni/src/proximity_info.h @@ -18,7 +18,6 @@ #define LATINIME_PROXIMITY_INFO_H #include -#include #include "defines.h" #include "jni.h" @@ -75,8 +74,8 @@ class ProximityInfo { return MOST_COMMON_KEY_WIDTH_SQUARE; } - std::string getLocaleStr() const { - return *mLocaleStr; + const char *getLocaleStr() const { + return mLocaleStr; } int getKeyCount() const { @@ -129,7 +128,7 @@ class ProximityInfo { const int CELL_HEIGHT; const int KEY_COUNT; const bool HAS_TOUCH_POSITION_CORRECTION_DATA; - const std::string *mLocaleStr; + char mLocaleStr[MAX_LOCALE_STRING_LENGTH]; int32_t *mProximityCharsArray; int32_t mKeyXCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD]; int32_t mKeyYCoordinates[MAX_KEY_COUNT_IN_A_KEYBOARD];