From c8297fa5aada88c0799ed62d5bacf9c8a1be42dd Mon Sep 17 00:00:00 2001 From: Gazizova Sofia Date: Sun, 17 Mar 2024 16:48:31 +0300 Subject: [PATCH] Add string hex checker helper Signed-off-by: Gazizova Sofia Change-Id: If8d1e30c6ef57a2390f3ba63888efbd319616dc4 --- base/include/string_ex.h | 10 ++++++++++ base/src/string_ex.cpp | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/base/include/string_ex.h b/base/include/string_ex.h index cf99110..f5eb23b 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 0082ff7..ec8c1a4 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) { -- Gitee