Initialize Tizen 2.3
[framework/web/wrt-plugins-common.git] / src_wearable / 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  * Removes spaces from begining of the string.
36  * @param str String to remove spaces from.
37  * @return String with spaces removed.
38  */
39 std::string lTrim(const std::string& str);
40
41 /**
42  * Removes spaces from end of the string.
43  * @param str String to remove spaces from.
44  * @return String with spaces removed.
45  */
46 std::string rTrim(const std::string& str);
47
48 /**
49  * Removes spaces from begining and end of the string.
50  * @param str String to remove spaces from.
51  * @return String with spaces removed.
52  */
53 std::string trim(const std::string& str);
54
55 /**
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.
60  */
61 bool startsWith(const std::string& str, const std::string& startStr);
62
63 /**
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.
68  */
69 bool endsWith(const std::string& str, const std::string& endStr);
70
71 /**
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.
76  * @code
77  * ...
78  * std::string str = "Aala maa kota";
79  * std::string uniq = unique(str, 'a');
80  * uniq == "Ala ma kota";
81  * ...
82  * @endcode
83  */
84 std::string unique(const std::string& str, const char needle);
85
86 /**
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.
93  */
94 bool inCStringArray(const std::string& needle, const char* stack[]);
95
96 /**
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
102  *          free() function.
103  */
104 char* strdup(const char* str);
105 inline char* strdup(const std::string& str)
106 {
107     return strdup(str.c_str());
108 }
109
110 std::string merge(const std::vector<std::string>& strs,
111                   std::string::value_type delim);
112
113 std::vector<std::string> split(const std::string& str,
114                                std::string::value_type delim);
115
116 /**
117  * @deprecated Use convertTo<int>().
118  */
119 int toInt(const std::string& str) WRT_PLUGINS_DEPRECATED;
120
121 /**
122  * Converts string to specified type.
123  *
124  * @tparam T Type to convert to.
125  * @param str String to convert.
126  * @return Converted value.
127  * @throw ConversionException If conversion fails.
128  *
129  * @remarks T must implement default constructor and an implementation
130  *          of input string operator for T must exist.
131  */
132 template<typename T>
133 T convertTo(const std::string& str)
134 {
135     T result;
136     std::istringstream iss(str);
137     if (!(iss >> result)) {
138         Throw(ConversionException);
139     }
140     return std::move(result);
141 }
142
143 /**
144  * Converts a given value to string.
145  *
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.
150  *
151  * @remarks Output stream operator for type T must exist.
152  */
153 template<typename T>
154 std::string parse(const T& data)
155 {
156     std::ostringstream oss;
157     if (!(oss << data)) {
158         Throw(ConversionException);
159     }
160     return oss.str();
161 }
162 }
163 }
164 }
165
166 #endif