Stop using STL string in additional_proximity_chars

Change-Id: Ic016fd5983b8855b0fd9506a17b205db86af3a2f
diff --git a/native/jni/src/additional_proximity_chars.cpp b/native/jni/src/additional_proximity_chars.cpp
index de87646..f594927 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 c22de7d..1fe996d 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 <cstring>
 #include <stdint.h>
-#include <string>
 
 #include "defines.h"
 
@@ -27,7 +27,7 @@
 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 @@
     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 @@
         }
     }
 
-    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 31dd61e..484fc6b 100644
--- a/native/jni/src/defines.h
+++ b/native/jni/src/defines.h
@@ -265,6 +265,9 @@
 // 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 4f6507e..2564c8a 100644
--- a/native/jni/src/proximity_info.cpp
+++ b/native/jni/src/proximity_info.cpp
@@ -17,7 +17,6 @@
 #include <cassert>
 #include <cmath>
 #include <cstring>
-#include <string>
 
 #define LOG_TAG "LatinIME: proximity_info.cpp"
 
@@ -68,10 +67,12 @@
         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 @@
 }
 
 ProximityInfo::~ProximityInfo() {
-    delete mLocaleStr;
     delete[] mProximityCharsArray;
 }
 
diff --git a/native/jni/src/proximity_info.h b/native/jni/src/proximity_info.h
index 7222d0d..d7e24c5 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 <stdint.h>
-#include <string>
 
 #include "defines.h"
 #include "jni.h"
@@ -75,8 +74,8 @@
         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 @@
     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];