Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / Commons / StringUtils.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /**
17  * @file   StringUtils.h
18  * @author Zbigniew Kostrzewa (z.kostrzewa@samsung.com)
19  */
20
21 #ifndef WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
22 #define WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
23
24 #include <string>
25 #include <vector>
26 #include <sstream>
27 #include <utility>
28 #include <Commons/Exception.h>
29 #include <Commons/Deprecated.h>
30
31 namespace WrtDeviceApis {
32 namespace Commons {
33 namespace String {
34
35 /**
36  * Removes spaces from begining of the string.
37  * @param str String to remove spaces from.
38  * @return String with spaces removed.
39  */
40 std::string lTrim(const std::string& str);
41
42 /**
43  * Removes spaces from end of the string.
44  * @param str String to remove spaces from.
45  * @return String with spaces removed.
46  */
47 std::string rTrim(const std::string& str);
48
49 /**
50  * Removes spaces from begining and end of the string.
51  * @param str String to remove spaces from.
52  * @return String with spaces removed.
53  */
54 std::string trim(const std::string& str);
55
56 /**
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.
61  */
62 bool startsWith(const std::string& str, const std::string& startStr);
63
64 /**
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.
69  */
70 bool endsWith(const std::string& str, const std::string& endStr);
71
72 /**
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.
77  * @code
78  * ...
79  * std::string str = "Aala maa kota";
80  * std::string uniq = unique(str, 'a');
81  * uniq == "Ala ma kota";
82  * ...
83  * @endcode
84  */
85 std::string unique(const std::string& str, const char needle);
86
87 /**
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.
94  */
95 bool inCStringArray(const std::string& needle, const char* stack[]);
96
97 /**
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
103  *          free() function.
104  */
105 char* strdup(const char* str);
106 inline char* strdup(const std::string& str)
107 {
108     return strdup(str.c_str());
109 }
110
111 std::string merge(const std::vector<std::string>& strs,
112                   std::string::value_type delim);
113
114 std::vector<std::string> split(const std::string& str,
115                                std::string::value_type delim);
116
117 /**
118  * @deprecated Use convertTo<int>().
119  */
120 int toInt(const std::string& str) WRT_PLUGINS_DEPRECATED;
121
122 /**
123  * Converts string to specified type.
124  *
125  * @tparam T Type to convert to.
126  * @param str String to convert.
127  * @return Converted value.
128  * @throw ConversionException If conversion fails.
129  *
130  * @remarks T must implement default constructor and an implementation
131  *          of input string operator for T must exist.
132  */
133 template<typename T>
134 T convertTo(const std::string& str)
135 {
136     T result;
137     std::istringstream iss(str);
138     if (!(iss >> result)) {
139         Throw(ConversionException);
140     }
141     return std::move(result);
142 }
143
144 /**
145  * Converts a given value to string.
146  *
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.
151  *
152  * @remarks Output stream operator for type T must exist.
153  */
154 template<typename T>
155 std::string parse(const T& data)
156 {
157     std::ostringstream oss;
158     if (!(oss << data))
159     {
160         Throw(ConversionException);
161     }
162     return oss.str();
163 }
164
165 }
166 }
167 }
168
169 #endif