Update wrt-plugins-common_0.3.53
[framework/web/wrt-plugins-common.git] / src / Commons / StringUtils.cpp
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 #include <cstring>
17 #include <algorithm>
18 #include <iterator>
19 #include <new>
20 #include "StringUtils.h"
21
22 namespace WrtDeviceApis {
23 namespace Commons {
24 namespace String {
25
26 std::string lTrim(const std::string& str)
27 {
28     std::string result = str;
29     result.erase(result.begin(),
30                  std::find_if(result.begin(), result.end(),
31                               std::not1(std::ptr_fun<int, int>(std::isspace))));
32     return result;
33 }
34
35 std::string rTrim(const std::string& str)
36 {
37     std::string result = str;
38     result.erase(std::find_if(result.rbegin(), result.rend(),
39                               std::not1(std::ptr_fun<int, int>(
40                                             std::isspace))).base(), result.end());
41     return result;
42 }
43
44 std::string trim(const std::string& str)
45 {
46     return lTrim(rTrim(str));
47 }
48
49 bool startsWith(const std::string& str,
50         const std::string& startStr)
51 {
52     return str.find(startStr) == 0;
53 }
54
55 bool endsWith(const std::string& str,
56         const std::string& endStr)
57 {
58     std::size_t pos = str.rfind(endStr);
59     if (pos == std::string::npos) {
60         return false;
61     }
62     return pos == str.length() - endStr.length();
63 }
64
65 std::string unique(const std::string& str,
66         const char needle)
67 {
68     std::string result = str;
69
70     std::string::size_type pos = 0;
71     while ((pos = result.find_first_of(needle, pos)) < std::string::npos) {
72         std::string::size_type last = result.find_first_not_of(needle, pos);
73         if (last == std::string::npos) {
74             last = result.size();
75         }
76         if (last - pos > 1) {
77             result.erase(pos + 1, last - pos - 1);
78         }
79         pos = last;
80     }
81
82     return result;
83 }
84
85 bool inCStringArray(const std::string& needle,
86         const char* stack[])
87 {
88     while (*stack) {
89         if (needle == *stack) {
90             return true;
91         }
92         ++stack;
93     }
94     return false;
95 }
96
97 char* strdup(const char* str)
98 {
99     char* result = ::strdup(str);
100     if (NULL == result) {
101         throw std::bad_alloc();
102     }
103     return result;
104 }
105
106 std::string merge(const std::vector<std::string>& strs,
107                   std::string::value_type delim)
108 {
109     typedef std::vector<std::string> Strings;
110
111     std::ostringstream ss;
112     if (!strs.empty()) {
113         for (Strings::size_type i = 0; i < strs.size() - 1; ++i) {
114             ss << strs[i] << delim;
115         }
116         ss << strs.at(strs.size() - 1);
117     }
118     return ss.str();
119 }
120
121 std::vector<std::string> split(const std::string& str,
122         std::string::value_type delim)
123 {
124     std::vector<std::string> result;
125     std::string::size_type begin = 0, end = 0;
126     while ((end = str.find_first_of(delim, begin))) {
127         std::string part = (std::string::npos == end ?
128                             str.substr(begin) :
129                             str.substr(begin, end - begin));
130         if (!part.empty()) {
131             result.push_back(part);
132         }
133         if (std::string::npos == end) { break; }
134         begin = end + 1;
135     }
136     return result;
137 }
138
139 int toInt(const std::string& str)
140 {
141     return convertTo<int>(str);
142 }
143
144 }
145 }
146 }