2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "StringUtils.h"
22 namespace WrtDeviceApis {
25 std::string lTrim(const std::string& str)
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))));
34 std::string rTrim(const std::string& str)
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());
43 std::string trim(const std::string& str)
45 return lTrim(rTrim(str));
48 bool startsWith(const std::string& str,
49 const std::string& startStr)
51 return str.find(startStr) == 0;
54 bool endsWith(const std::string& str,
55 const std::string& endStr)
57 std::size_t pos = str.rfind(endStr);
58 if (pos == std::string::npos) {
61 return pos == str.length() - endStr.length();
64 std::string unique(const std::string& str,
67 std::string result = str;
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) {
76 result.erase(pos + 1, last - pos - 1);
84 bool inCStringArray(const std::string& needle,
88 if (needle == *stack) {
96 char* strdup(const char* str)
98 char* result = ::strdup(str);
100 throw std::bad_alloc();
105 std::string merge(const std::vector<std::string>& strs,
106 std::string::value_type delim)
108 typedef std::vector<std::string> Strings;
110 std::ostringstream ss;
112 for (Strings::size_type i = 0; i < strs.size() - 1; ++i) {
113 ss << strs[i] << delim;
115 ss << strs.at(strs.size() - 1);
120 std::vector<std::string> split(const std::string& str,
121 std::string::value_type delim)
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 ?
128 str.substr(begin, end - begin));
130 result.push_back(part);
132 if (std::string::npos == end) {
140 int toInt(const std::string& str)
142 return convertTo<int>(str);