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.
18 * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
21 #ifndef WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
22 #define WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
28 #include <Commons/Exception.h>
29 #include <Commons/Deprecated.h>
31 namespace WrtDeviceApis {
35 * Removes spaces from begining of the string.
36 * @param str String to remove spaces from.
37 * @return String with spaces removed.
39 std::string lTrim(const std::string& str);
42 * Removes spaces from end of the string.
43 * @param str String to remove spaces from.
44 * @return String with spaces removed.
46 std::string rTrim(const std::string& str);
49 * Removes spaces from begining and end of the string.
50 * @param str String to remove spaces from.
51 * @return String with spaces removed.
53 std::string trim(const std::string& str);
56 * Checks if str starts with startStr
57 * @param str String to be searched.
58 * @param startStr String to be found at the beginning of str.
59 * @return true when str starts with startStr, false otherwise.
61 bool startsWith(const std::string& str, const std::string& startStr);
64 * Checks if str ends with startStr
65 * @param str String to be searched.
66 * @param endStr String to be found at the end of str.
67 * @return true when str starts with startStr, false otherwise.
69 bool endsWith(const std::string& str, const std::string& endStr);
72 * Merges consecutive groups of duplicate characters into one.
73 * @param str String to remove characters from.
74 * @param needle Character for which groups of duplicates to merge.
75 * @return String with group of duplicate characters merged.
78 * std::string str = "Aala maa kota";
79 * std::string uniq = unique(str, 'a');
80 * uniq == "Ala ma kota";
84 std::string unique(const std::string& str, const char needle);
87 * Checks if supplied string exists in specified array of c-strings.
88 * @param needle String to verify.
89 * @param stack Array of c-strings which last element should be NULL.
90 * @return True if string exists in array, false otherwise.
91 * @remarks Last element of array should be NULL, otherwise there is no
92 * way to calculate its number of elements.
94 bool inCStringArray(const std::string& needle, const char* stack[]);
97 * Duplicates a string.
98 * @param str String to duplicate.
99 * @return String copy.
100 * @throw std::bad_alloc If allocation fails.
101 * @remarks Ownership passed to the caller. String has to be deallocated using
104 char* strdup(const char* str);
105 inline char* strdup(const std::string& str)
107 return strdup(str.c_str());
110 std::string merge(const std::vector<std::string>& strs,
111 std::string::value_type delim);
113 std::vector<std::string> split(const std::string& str,
114 std::string::value_type delim);
117 * @deprecated Use convertTo<int>().
119 int toInt(const std::string& str) WRT_PLUGINS_DEPRECATED;
122 * Converts string to specified type.
124 * @tparam T Type to convert to.
125 * @param str String to convert.
126 * @return Converted value.
127 * @throw ConversionException If conversion fails.
129 * @remarks T must implement default constructor and an implementation
130 * of input string operator for T must exist.
133 T convertTo(const std::string& str)
136 std::istringstream iss(str);
137 if (!(iss >> result)) {
138 Throw(ConversionException);
140 return std::move(result);
144 * Converts a given value to string.
146 * @tparam T Type of the value to convert. It is deduced by the compiler.
147 * @param data Value to convert.
148 * @return Value converted to string.
149 * @throw ConversionException If operation fails.
151 * @remarks Output stream operator for type T must exist.
154 std::string parse(const T& data)
156 std::ostringstream oss;
157 if (!(oss << data)) {
158 Throw(ConversionException);