diff --git a/base/include/string_ex.h b/base/include/string_ex.h index cf99110c3561048b2eec33507de2a2b1e8fa6196..f5eb23bc11d513657fe1fea90d79943bd2a152c2 100644 --- a/base/include/string_ex.h +++ b/base/include/string_ex.h @@ -224,6 +224,16 @@ bool IsSameTextStr(const std::string& first, const std::string& second); */ bool IsAsciiString(const std::string& str); +/** + * @ingroup StringOperation + * @brief Checks whether all characters in a string represent valid HEX number. + * + * @param str Indicates the base string. + * @return Returns `true` if all characters in the string represent valid HEX number; + * returns `false` otherwise. + */ +bool IsValidHexString(const std::string& colorStr); + #ifndef IOS_PLATFORM /** * @ingroup StringOperation diff --git a/base/src/string_ex.cpp b/base/src/string_ex.cpp index 0082ff75420c97f1c88826a7a6052247e1a4bd9e..ec8c1a475c08b23a1ae489f1c47191742d18e8f7 100644 --- a/base/src/string_ex.cpp +++ b/base/src/string_ex.cpp @@ -251,6 +251,20 @@ bool IsAsciiString(const string& str) return true; } +bool IsValidHexString(const std::string& str) +{ + if (str.empty()) { + return false; + } + for (char ch : str) { + if ((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) { + continue; + } + return false; + } + return true; +} + #ifndef IOS_PLATFORM u16string Str8ToStr16(const string& str) {