diff options
author | Tianjie Xu <xunchang@google.com> | 2017-03-22 20:27:26 +0100 |
---|---|---|
committer | Tianjie Xu <xunchang@google.com> | 2017-03-25 00:54:52 +0100 |
commit | 2078b22e4145fef2648cc714eae6588353940c4b (patch) | |
tree | 9d93bb7dfa2f4bd9951aaa5fe429a511b1435b33 /minui | |
parent | Merge "Redundant checking of STL container element" (diff) | |
download | android_bootable_recovery-2078b22e4145fef2648cc714eae6588353940c4b.tar android_bootable_recovery-2078b22e4145fef2648cc714eae6588353940c4b.tar.gz android_bootable_recovery-2078b22e4145fef2648cc714eae6588353940c4b.tar.bz2 android_bootable_recovery-2078b22e4145fef2648cc714eae6588353940c4b.tar.lz android_bootable_recovery-2078b22e4145fef2648cc714eae6588353940c4b.tar.xz android_bootable_recovery-2078b22e4145fef2648cc714eae6588353940c4b.tar.zst android_bootable_recovery-2078b22e4145fef2648cc714eae6588353940c4b.zip |
Diffstat (limited to 'minui')
-rw-r--r-- | minui/Android.mk | 10 | ||||
-rw-r--r-- | minui/include/minui/minui.h | 3 | ||||
-rw-r--r-- | minui/resources.cpp | 33 |
3 files changed, 33 insertions, 13 deletions
diff --git a/minui/Android.mk b/minui/Android.mk index 281f64912..4dfc65f8a 100644 --- a/minui/Android.mk +++ b/minui/Android.mk @@ -28,7 +28,10 @@ LOCAL_WHOLE_STATIC_LIBRARIES := \ libdrm \ libsync_recovery -LOCAL_STATIC_LIBRARIES := libpng +LOCAL_STATIC_LIBRARIES := \ + libpng \ + libbase + LOCAL_CFLAGS := -Werror LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include @@ -61,7 +64,10 @@ include $(BUILD_STATIC_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := libminui LOCAL_WHOLE_STATIC_LIBRARIES += libminui -LOCAL_SHARED_LIBRARIES := libpng +LOCAL_SHARED_LIBRARIES := \ + libpng \ + libbase + LOCAL_CFLAGS := -Werror LOCAL_C_INCLUDES := $(LOCAL_PATH)/include LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include diff --git a/minui/include/minui/minui.h b/minui/include/minui/minui.h index a1749dfe6..78dd4cb98 100644 --- a/minui/include/minui/minui.h +++ b/minui/include/minui/minui.h @@ -20,6 +20,7 @@ #include <sys/types.h> #include <functional> +#include <string> // // Graphics. @@ -93,7 +94,7 @@ int ev_get_epollfd(); // Resources // -bool matches_locale(const char* prefix, const char* locale); +bool matches_locale(const std::string& prefix, const std::string& locale); // res_create_*_surface() functions return 0 if no error, else // negative. diff --git a/minui/resources.cpp b/minui/resources.cpp index c0f9c5c85..86c731b02 100644 --- a/minui/resources.cpp +++ b/minui/resources.cpp @@ -25,8 +25,11 @@ #include <sys/types.h> #include <unistd.h> +#include <regex> +#include <string> #include <vector> +#include <android-base/strings.h> #include <png.h> #include "minui/minui.h" @@ -371,16 +374,26 @@ int res_create_alpha_surface(const char* name, GRSurface** pSurface) { // This function tests if a locale string stored in PNG (prefix) matches // the locale string provided by the system (locale). -bool matches_locale(const char* prefix, const char* locale) { - if (locale == nullptr) { - return false; - } - - // Return true if the whole string of prefix matches the top part of - // locale. For instance, prefix == "en" matches locale == "en_US"; - // and prefix == "zh_CN" matches locale == "zh_CN_#Hans". - - return (strncmp(prefix, locale, strlen(prefix)) == 0); +bool matches_locale(const std::string& prefix, const std::string& locale) { + // According to the BCP 47 format, A locale string may consists of: + // language-{extlang}-{script}-{region}-{variant} + // The locale headers in PNG mostly consist of language-{region} except for sr-Latn, and some + // android's system locale can have the format language-{script}-{region}. + + // Return true if the whole string of prefix matches the top part of locale. Otherwise try to + // match the locale string without the {script} section. + // For instance, prefix == "en" matches locale == "en-US", prefix == "sr-Latn" matches locale + // == "sr-Latn-BA", and prefix == "zh-CN" matches locale == "zh-Hans-CN". + if (android::base::StartsWith(locale, prefix.c_str())) { + return true; + } + + size_t separator = prefix.find('-'); + if (separator == std::string::npos) { + return false; + } + std::regex loc_regex(prefix.substr(0, separator) + "-[A-Za-z]*" + prefix.substr(separator)); + return std::regex_match(locale, loc_regex); } int res_create_localized_alpha_surface(const char* name, |