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 {
36 * Removes spaces from begining of the string.
37 * @param str String to remove spaces from.
38 * @return String with spaces removed.
40 std::string lTrim(const std::string& str);
43 * Removes spaces from end of the string.
44 * @param str String to remove spaces from.
45 * @return String with spaces removed.
47 std::string rTrim(const std::string& str);
50 * Removes spaces from begining and end of the string.
51 * @param str String to remove spaces from.
52 * @return String with spaces removed.
54 std::string trim(const std::string& str);
57 * Checks if str starts with startStr
58 * @param str String to be searched.
59 * @param startStr String to be found at the beginning of str.
60 * @return true when str starts with startStr, false otherwise.
62 bool startsWith(const std::string& str, const std::string& startStr);
65 * Checks if str ends with startStr
66 * @param str String to be searched.
67 * @param endStr String to be found at the end of str.
68 * @return true when str starts with startStr, false otherwise.
70 bool endsWith(const std::string& str, const std::string& endStr);
73 * Merges consecutive groups of duplicate characters into one.
74 * @param str String to remove characters from.
75 * @param needle Character for which groups of duplicates to merge.
76 * @return String with group of duplicate characters merged.
79 * std::string str = "Aala maa kota";
80 * std::string uniq = unique(str, 'a');
81 * uniq == "Ala ma kota";
85 std::string unique(const std::string& str, const char needle);
88 * Checks if supplied string exists in specified array of c-strings.
89 * @param needle String to verify.
90 * @param stack Array of c-strings which last element should be NULL.
91 * @return True if string exists in array, false otherwise.
92 * @remarks Last element of array should be NULL, otherwise there is no
93 * way to calculate its number of elements.
95 bool inCStringArray(const std::string& needle, const char* stack[]);
98 * Duplicates a string.
99 * @param str String to duplicate.
100 * @return String copy.
101 * @throw std::bad_alloc If allocation fails.
102 * @remarks Ownership passed to the caller. String has to be deallocated using
105 char* strdup(const char* str);
106 inline char* strdup(const std::string& str)
108 return strdup(str.c_str());
111 std::string merge(const std::vector<std::string>& strs,
112 std::string::value_type delim);
114 std::vector<std::string> split(const std::string& str,
115 std::string::value_type delim);
118 * @deprecated Use convertTo<int>().
120 int toInt(const std::string& str) WRT_PLUGINS_DEPRECATED;
123 * Converts string to specified type.
125 * @tparam T Type to convert to.
126 * @param str String to convert.
127 * @return Converted value.
128 * @throw ConversionException If conversion fails.
130 * @remarks T must implement default constructor and an implementation
131 * of input string operator for T must exist.
134 T convertTo(const std::string& str)
137 std::istringstream iss(str);
138 if (!(iss >> result)) {
139 Throw(ConversionException);
141 return std::move(result);
145 * Converts a given value to string.
147 * @tparam T Type of the value to convert. It is deduced by the compiler.
148 * @param data Value to convert.
149 * @return Value converted to string.
150 * @throw ConversionException If operation fails.
152 * @remarks Output stream operator for type T must exist.
155 std::string parse(const T& data)
157 std::ostringstream oss;
160 Throw(ConversionException);