13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci 163f4cbf05Sopenharmony_ci/** 173f4cbf05Sopenharmony_ci* @file string_ex.h 183f4cbf05Sopenharmony_ci* 193f4cbf05Sopenharmony_ci* @brief Provides the global string operation function implemented in c_utils. 203f4cbf05Sopenharmony_ci*/ 213f4cbf05Sopenharmony_ci 223f4cbf05Sopenharmony_ci/** 233f4cbf05Sopenharmony_ci* @defgroup StringOperation 243f4cbf05Sopenharmony_ci* @{ 253f4cbf05Sopenharmony_ci* @brief Provides interfaces for operating strings. 263f4cbf05Sopenharmony_ci* 273f4cbf05Sopenharmony_ci* Include converting between uppercase and lowercase, 283f4cbf05Sopenharmony_ci* string replacement, trim and split etc. 293f4cbf05Sopenharmony_ci*/ 303f4cbf05Sopenharmony_ci#ifndef STRING_EX_H 313f4cbf05Sopenharmony_ci#define STRING_EX_H 323f4cbf05Sopenharmony_ci 333f4cbf05Sopenharmony_ci#include <string> 343f4cbf05Sopenharmony_ci#include <vector> 353f4cbf05Sopenharmony_ci 363f4cbf05Sopenharmony_cinamespace OHOS { 373f4cbf05Sopenharmony_ci 383f4cbf05Sopenharmony_ci/** 393f4cbf05Sopenharmony_ci * @ingroup StringOperation 403f4cbf05Sopenharmony_ci * @brief Converts all letters in a string to uppercase. 413f4cbf05Sopenharmony_ci * 423f4cbf05Sopenharmony_ci * @param str Indicates the base string. 433f4cbf05Sopenharmony_ci * @return Returns a new `std::string` object after conversion. 443f4cbf05Sopenharmony_ci */ 453f4cbf05Sopenharmony_cistd::string UpperStr(const std::string& str); 463f4cbf05Sopenharmony_ci 473f4cbf05Sopenharmony_ci/** 483f4cbf05Sopenharmony_ci * @ingroup StringOperation 493f4cbf05Sopenharmony_ci * @brief Converts all letters in a string to lowercase. 503f4cbf05Sopenharmony_ci * 513f4cbf05Sopenharmony_ci * @param str Indicates the base string. 523f4cbf05Sopenharmony_ci * @return Returns a new `std::string` object after conversion. 533f4cbf05Sopenharmony_ci */ 543f4cbf05Sopenharmony_cistd::string LowerStr(const std::string& str); 553f4cbf05Sopenharmony_ci 563f4cbf05Sopenharmony_ci/** 573f4cbf05Sopenharmony_ci * @ingroup StringOperation 583f4cbf05Sopenharmony_ci * @brief Replaces a substring in the base string. 593f4cbf05Sopenharmony_ci * 603f4cbf05Sopenharmony_ci * @param str Indicates the substring to be replaced. 613f4cbf05Sopenharmony_ci * @param src Indicates the base string. 623f4cbf05Sopenharmony_ci * @param dst Indicates the expected substring for replacement. 633f4cbf05Sopenharmony_ci * @return Returns a new `std::string` object after replacement. 643f4cbf05Sopenharmony_ci */ 653f4cbf05Sopenharmony_cistd::string ReplaceStr(const std::string& str, const std::string& src, const std::string& dst); 663f4cbf05Sopenharmony_ci 673f4cbf05Sopenharmony_ci/** 683f4cbf05Sopenharmony_ci * @ingroup StringOperation 693f4cbf05Sopenharmony_ci * @brief Trims a string indicated by `cTrim` from both ends of the base string. 703f4cbf05Sopenharmony_ci * 713f4cbf05Sopenharmony_ci * @param str Indicates the base string. 723f4cbf05Sopenharmony_ci * @param cTrim Indicates the string to trim from the base string, which is '' by default. 733f4cbf05Sopenharmony_ci * @return Returns a new `std::string` object after trimming. 743f4cbf05Sopenharmony_ci */ 753f4cbf05Sopenharmony_cistd::string TrimStr(const std::string& str, const char cTrim = ' '); 763f4cbf05Sopenharmony_ci 773f4cbf05Sopenharmony_ci/** 783f4cbf05Sopenharmony_ci * @ingroup StringOperation 793f4cbf05Sopenharmony_ci * @brief Converts a decimal value to a hexadecimal string. 803f4cbf05Sopenharmony_ci * 813f4cbf05Sopenharmony_ci * @param value Indicates the decimal value to convert. 823f4cbf05Sopenharmony_ci * @param upper Specifies whether the output string is in uppercase. 833f4cbf05Sopenharmony_ci * The default value is `true`. 843f4cbf05Sopenharmony_ci * @return Returns a new `std::string` object after conversion. 853f4cbf05Sopenharmony_ci */ 863f4cbf05Sopenharmony_cistd::string DexToHexString(int value, bool upper = true); 873f4cbf05Sopenharmony_ci 883f4cbf05Sopenharmony_ci/** 893f4cbf05Sopenharmony_ci * @ingroup StringOperation 903f4cbf05Sopenharmony_ci * @brief Splits a string by `sep`. 913f4cbf05Sopenharmony_ci * 923f4cbf05Sopenharmony_ci * @param str Indicates the base string. 933f4cbf05Sopenharmony_ci * @param sep Indicates the substring to be used as the separator. 943f4cbf05Sopenharmony_ci * @param strs Indicates the `std::vector` object to store the results. 953f4cbf05Sopenharmony_ci * @param canEmpty Specifies whether the output string can be an empty string. 963f4cbf05Sopenharmony_ci * The default value is `false`. 973f4cbf05Sopenharmony_ci * @param needTrim Specifies whether to remove whitespace from the output string. 983f4cbf05Sopenharmony_ci * The default value is `true`. 993f4cbf05Sopenharmony_ci */ 1003f4cbf05Sopenharmony_civoid SplitStr(const std::string& str, const std::string& sep, std::vector<std::string>& strs, 1013f4cbf05Sopenharmony_ci bool canEmpty = false, bool needTrim = true); 1023f4cbf05Sopenharmony_ci 1033f4cbf05Sopenharmony_ci/** 1043f4cbf05Sopenharmony_ci * @ingroup StringOperation 1053f4cbf05Sopenharmony_ci * @brief Converts a value of int, double, or any other type to a string. 1063f4cbf05Sopenharmony_ci * 1073f4cbf05Sopenharmony_ci * @tparam T Indicates the type of the input data. 1083f4cbf05Sopenharmony_ci * @param iValue Indicates the input data. 1093f4cbf05Sopenharmony_ci * @return Returns a new `std::string` object after conversion. 1103f4cbf05Sopenharmony_ci */ 1113f4cbf05Sopenharmony_citemplate<class T> 1123f4cbf05Sopenharmony_ciinline std::string ToString(T iValue) 1133f4cbf05Sopenharmony_ci{ 1143f4cbf05Sopenharmony_ci return std::to_string(iValue); 1153f4cbf05Sopenharmony_ci} 1163f4cbf05Sopenharmony_ci 1173f4cbf05Sopenharmony_ci/** 1183f4cbf05Sopenharmony_ci * @ingroup StringOperation 1193f4cbf05Sopenharmony_ci * @brief Converts a string to an int value. 1203f4cbf05Sopenharmony_ci * 1213f4cbf05Sopenharmony_ci * @param str Indicates the string to be converted. 1223f4cbf05Sopenharmony_ci * @param value Indicates the `int` variable to store the result. 1233f4cbf05Sopenharmony_ci * @return Returns `true` if the operation is successful; 1243f4cbf05Sopenharmony_ci * returns `false` otherwise. 1253f4cbf05Sopenharmony_ci */ 1263f4cbf05Sopenharmony_cibool StrToInt(const std::string& str, int& value); 1273f4cbf05Sopenharmony_ci 1283f4cbf05Sopenharmony_ci/** 1293f4cbf05Sopenharmony_ci * @ingroup StringOperation 1303f4cbf05Sopenharmony_ci * @brief Checks whether all characters in a string are numeric. 1313f4cbf05Sopenharmony_ci * 1323f4cbf05Sopenharmony_ci * @param str Indicates the base string. 1333f4cbf05Sopenharmony_ci * @return Returns `true` if all characters in the string are numeric; 1343f4cbf05Sopenharmony_ci * returns `false` otherwise. 1353f4cbf05Sopenharmony_ci */ 1363f4cbf05Sopenharmony_cibool IsNumericStr(const std::string& str); 1373f4cbf05Sopenharmony_ci 1383f4cbf05Sopenharmony_ci/** 1393f4cbf05Sopenharmony_ci * @ingroup StringOperation 1403f4cbf05Sopenharmony_ci * @brief Checks whether all characters in a string are alphabetic. 1413f4cbf05Sopenharmony_ci * 1423f4cbf05Sopenharmony_ci * @param str Indicates the base string. 1433f4cbf05Sopenharmony_ci * @return Returns `true` if all characters in the string are alphabetic; 1443f4cbf05Sopenharmony_ci * returns `false` otherwise. 1453f4cbf05Sopenharmony_ci */ 1463f4cbf05Sopenharmony_cibool IsAlphaStr(const std::string& str); 1473f4cbf05Sopenharmony_ci 1483f4cbf05Sopenharmony_ci/** 1493f4cbf05Sopenharmony_ci * @ingroup StringOperation 1503f4cbf05Sopenharmony_ci * @brief Checks whether all characters in a string are in uppercase. 1513f4cbf05Sopenharmony_ci * 1523f4cbf05Sopenharmony_ci * @param str Indicates the base string. 1533f4cbf05Sopenharmony_ci * @return Returns `true` if all characters in the string are in uppercase; 1543f4cbf05Sopenharmony_ci * returns `false` otherwise. 1553f4cbf05Sopenharmony_ci */ 1563f4cbf05Sopenharmony_cibool IsUpperStr(const std::string& str); 1573f4cbf05Sopenharmony_ci 1583f4cbf05Sopenharmony_ci/** 1593f4cbf05Sopenharmony_ci * @ingroup StringOperation 1603f4cbf05Sopenharmony_ci * @brief Checks whether all characters in a string are in lowercase. 1613f4cbf05Sopenharmony_ci * 1623f4cbf05Sopenharmony_ci * @param str Indicates the base string. 1633f4cbf05Sopenharmony_ci * @return Returns `true` if all characters in the string are in lowercase; 1643f4cbf05Sopenharmony_ci * returns `false` otherwise. 1653f4cbf05Sopenharmony_ci */ 1663f4cbf05Sopenharmony_cibool IsLowerStr(const std::string& str); 1673f4cbf05Sopenharmony_ci 1683f4cbf05Sopenharmony_ci/** 1693f4cbf05Sopenharmony_ci * @ingroup StringOperation 1703f4cbf05Sopenharmony_ci * @brief Checks whether a string contains the specified substring. 1713f4cbf05Sopenharmony_ci * 1723f4cbf05Sopenharmony_ci * @param str Indicates the base string. 1733f4cbf05Sopenharmony_ci * @param sub Indicates the substring. 1743f4cbf05Sopenharmony_ci * @return Returns `true` if the string contains the specified substring; 1753f4cbf05Sopenharmony_ci * returns `false` otherwise. 1763f4cbf05Sopenharmony_ci */ 1773f4cbf05Sopenharmony_cibool IsSubStr(const std::string& str, const std::string& sub); 1783f4cbf05Sopenharmony_ci 1793f4cbf05Sopenharmony_ci/** 1803f4cbf05Sopenharmony_ci * @ingroup StringOperation 1813f4cbf05Sopenharmony_ci * @brief Obtains the first substring between the substrings specified 1823f4cbf05Sopenharmony_ci * by `left` and `right`. 1833f4cbf05Sopenharmony_ci * 1843f4cbf05Sopenharmony_ci * @param str Indicates the base string. 1853f4cbf05Sopenharmony_ci * @param left Indicates the left string. 1863f4cbf05Sopenharmony_ci * @param right Indicates the right string. 1873f4cbf05Sopenharmony_ci * @param sub Indicates the `std::string` object to store the result string. 1883f4cbf05Sopenharmony_ci * @return Returns `pos` if the operation is successful; 1893f4cbf05Sopenharmony_ci * returns `string::npos` otherwise. 1903f4cbf05Sopenharmony_ci */ 1913f4cbf05Sopenharmony_cistd::string::size_type GetFirstSubStrBetween(const std::string& str, const std::string& left, 1923f4cbf05Sopenharmony_ci const std::string& right, std::string& sub); 1933f4cbf05Sopenharmony_ci 1943f4cbf05Sopenharmony_ci/** 1953f4cbf05Sopenharmony_ci * @ingroup StringOperation 1963f4cbf05Sopenharmony_ci * @brief Obtains all of the substrings between the substrings specified 1973f4cbf05Sopenharmony_ci * by `left` and `right`. 1983f4cbf05Sopenharmony_ci * 1993f4cbf05Sopenharmony_ci * @param str Indicates the base string. 2003f4cbf05Sopenharmony_ci * @param left Indicates the left string. 2013f4cbf05Sopenharmony_ci * @param right Indicates the right string. 2023f4cbf05Sopenharmony_ci * @param sub Indicates the `std::vector` object to store all the result strings. 2033f4cbf05Sopenharmony_ci */ 2043f4cbf05Sopenharmony_civoid GetSubStrBetween(const std::string& str, const std::string& left, 2053f4cbf05Sopenharmony_ci const std::string& right, std::vector<std::string>& sub); 2063f4cbf05Sopenharmony_ci 2073f4cbf05Sopenharmony_ci/** 2083f4cbf05Sopenharmony_ci * @ingroup StringOperation 2093f4cbf05Sopenharmony_ci * @brief Checks whether two strings are equal. 2103f4cbf05Sopenharmony_ci * 2113f4cbf05Sopenharmony_ci * @param first Indicates the first string to check. 2123f4cbf05Sopenharmony_ci * @param second Indicates the second string to check. 2133f4cbf05Sopenharmony_ci * @note The string is case-insensitive. 2143f4cbf05Sopenharmony_ci */ 2153f4cbf05Sopenharmony_cibool IsSameTextStr(const std::string& first, const std::string& second); 2163f4cbf05Sopenharmony_ci 2173f4cbf05Sopenharmony_ci/** 2183f4cbf05Sopenharmony_ci * @ingroup StringOperation 2193f4cbf05Sopenharmony_ci * @brief Checks whether all characters in a string are ASCII encoded. 2203f4cbf05Sopenharmony_ci * 2213f4cbf05Sopenharmony_ci * @param str Indicates the base string. 2223f4cbf05Sopenharmony_ci * @return Returns `true` if all characters in the string are ASCII encoded; 2233f4cbf05Sopenharmony_ci * returns `false` otherwise. 2243f4cbf05Sopenharmony_ci */ 2253f4cbf05Sopenharmony_cibool IsAsciiString(const std::string& str); 2263f4cbf05Sopenharmony_ci 2273f4cbf05Sopenharmony_ci#ifndef IOS_PLATFORM 2283f4cbf05Sopenharmony_ci/** 2293f4cbf05Sopenharmony_ci * @ingroup StringOperation 2303f4cbf05Sopenharmony_ci * @brief Converts a string from UTF-16 to UTF-8 encoded. 2313f4cbf05Sopenharmony_ci * 2323f4cbf05Sopenharmony_ci * @param str16 Indicates a `std::u16string` object. 2333f4cbf05Sopenharmony_ci * @return Returns an empty `std::string` object if the operation fails. 2343f4cbf05Sopenharmony_ci */ 2353f4cbf05Sopenharmony_cistd::string Str16ToStr8(const std::u16string& str16); 2363f4cbf05Sopenharmony_ci 2373f4cbf05Sopenharmony_ci/** 2383f4cbf05Sopenharmony_ci * @ingroup StringOperation 2393f4cbf05Sopenharmony_ci * @brief Converts a string from UTF-8 to UTF-16 encoded. 2403f4cbf05Sopenharmony_ci * 2413f4cbf05Sopenharmony_ci * @param str Indicates a `std::string` object. 2423f4cbf05Sopenharmony_ci * @return Returns an empty `std::u16string` object if the operation fails. 2433f4cbf05Sopenharmony_ci */ 2443f4cbf05Sopenharmony_cistd::u16string Str8ToStr16(const std::string& str); 2453f4cbf05Sopenharmony_ci 2463f4cbf05Sopenharmony_ci/** 2473f4cbf05Sopenharmony_ci * @ingroup StringOperation 2483f4cbf05Sopenharmony_ci * @brief get the length of utf8 from utf16. 2493f4cbf05Sopenharmony_ci * 2503f4cbf05Sopenharmony_ci * @param str16 Indicates a `std::u16string` object. 2513f4cbf05Sopenharmony_ci * @return Returns -1 if the str16 is empty or the result is greater than INT MAX. 2523f4cbf05Sopenharmony_ci */ 2533f4cbf05Sopenharmony_ciint GetUtf16ToUtf8Length(const std::u16string& str16); 2543f4cbf05Sopenharmony_ci 2553f4cbf05Sopenharmony_ci/** 2563f4cbf05Sopenharmony_ci * @ingroup StringOperation 2573f4cbf05Sopenharmony_ci * @brief Converts a string from UTF-16 to UTF-8 encoded. 2583f4cbf05Sopenharmony_ci * 2593f4cbf05Sopenharmony_ci * @param str16 Indicates a `std::u16string` object. 2603f4cbf05Sopenharmony_ci * @param buffer Converted UTF-8 encoded buffer. 2613f4cbf05Sopenharmony_ci * @param bufferLen Buffer size. 2623f4cbf05Sopenharmony_ci * @return Returns the length of the converted UTF-8 encoding including the terminator '\0'; 2633f4cbf05Sopenharmony_ci * Returns -1 if the operation fails. 2643f4cbf05Sopenharmony_ci */ 2653f4cbf05Sopenharmony_ciint Char16ToChar8(const std::u16string& str16, char *buffer, int bufferLen); 2663f4cbf05Sopenharmony_ci#endif 2673f4cbf05Sopenharmony_ci} // namespace OHOS 2683f4cbf05Sopenharmony_ci 2693f4cbf05Sopenharmony_ci#endif // STRING_EX_H 2703f4cbf05Sopenharmony_ci 2713f4cbf05Sopenharmony_ci/**@}*/ 272