2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 #ifndef WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
17 #define WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
22 namespace WrtDeviceApis {
27 * Removes spaces from begining of the string.
28 * @param str String to remove spaces from.
29 * @return String with spaces removed.
31 std::string lTrim(const std::string& str);
34 * Removes spaces from end of the string.
35 * @param str String to remove spaces from.
36 * @return String with spaces removed.
38 std::string rTrim(const std::string& str);
41 * Removes spaces from begining and end of the string.
42 * @param str String to remove spaces from.
43 * @return String with spaces removed.
45 std::string trim(const std::string& str);
48 * Checks if str starts with startStr
49 * @param str String to be searched.
50 * @param startStr String to be found at the beginning of str.
51 * @return true when str starts with startStr, false otherwise.
53 bool startsWith(const std::string& str,
54 const std::string& startStr);
57 * Checks if str ends with startStr
58 * @param str String to be searched.
59 * @param endStr String to be found at the end of str.
60 * @return true when str starts with startStr, false otherwise.
62 bool endsWith(const std::string& str,
63 const std::string& endStr);
66 * Merges consecutive groups of duplicate characters into one.
67 * @param str String to remove characters from.
68 * @param needle Character for which groups of duplicates to merge.
69 * @return String with group of duplicate characters merged.
72 * std::string str = "Aala maa kota";
73 * std::string uniq = unique(str, 'a');
74 * uniq == "Ala ma kota";
78 std::string unique(const std::string& str,
82 * Checks if supplied string exists in specified array of c-strings.
83 * @param needle String to verify.
84 * @param stack Array of c-strings which last element should be NULL.
85 * @return True if string exists in array, false otherwise.
86 * @remarks Last element of array should be NULL, otherwise there is no
87 * way to calculate its number of elements.
89 bool inCStringArray(const std::string& needle,
93 * Duplicates a string.
94 * @param str String to duplicate.
95 * @return String copy.
96 * @throw std::bad_alloc If allocation fails.
97 * @remarks Ownership passed to the caller. String has to be deallocated using
100 char* strdup(const char* str);
101 inline char* strdup(const std::string& str)
103 return strdup(str.c_str());
106 std::string merge(const std::vector<std::string>& strs,
107 std::string::value_type delim);
109 std::vector<std::string> split(const std::string& str,
110 std::string::value_type delim);
112 int toInt(const std::string &text);
116 } // WrtDeviceApisCommon
118 #endif // WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_