tizen beta release
[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 #ifndef WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
17 #define WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_
18
19 #include <string>
20 #include <vector>
21
22 namespace WrtDeviceApis {
23 namespace Commons {
24 namespace String {
25
26 /**
27  * Removes spaces from begining of the string.
28  * @param str String to remove spaces from.
29  * @return String with spaces removed.
30  */
31 std::string lTrim(const std::string& str);
32
33 /**
34  * Removes spaces from end of the string.
35  * @param str String to remove spaces from.
36  * @return String with spaces removed.
37  */
38 std::string rTrim(const std::string& str);
39
40 /**
41  * Removes spaces from begining and end of the string.
42  * @param str String to remove spaces from.
43  * @return String with spaces removed.
44  */
45 std::string trim(const std::string& str);
46
47 /**
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.
52  */
53 bool startsWith(const std::string& str,
54         const std::string& startStr);
55
56 /**
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.
61  */
62 bool endsWith(const std::string& str,
63         const std::string& endStr);
64
65 /**
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.
70  * @code
71  * ...
72  * std::string str = "Aala maa kota";
73  * std::string uniq = unique(str, 'a');
74  * uniq == "Ala ma kota";
75  * ...
76  * @endcode
77  */
78 std::string unique(const std::string& str,
79         const char needle);
80
81 /**
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.
88  */
89 bool inCStringArray(const std::string& needle,
90         const char* stack[]);
91
92 /**
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
98  *          free() function.
99  */
100 char* strdup(const char* str);
101 inline char* strdup(const std::string& str)
102 {
103     return strdup(str.c_str());
104 }
105
106 std::string merge(const std::vector<std::string>& strs,
107         std::string::value_type delim);
108
109 std::vector<std::string> split(const std::string& str,
110         std::string::value_type delim);
111
112 int toInt(const std::string &text);
113
114 } // String
115 }
116 } // WrtDeviceApisCommon
117
118 #endif // WRTDEVICEAPIS_COMMONS_STRINGUTILS_H_