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