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