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 {
26 std::string lTrim(const std::string& str)
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))));
35 std::string rTrim(const std::string& str)
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());
44 std::string trim(const std::string& str)
46 return lTrim(rTrim(str));
49 bool startsWith(const std::string& str,
50 const std::string& startStr)
52 return str.find(startStr) == 0;
55 bool endsWith(const std::string& str,
56 const std::string& endStr)
58 std::size_t pos = str.rfind(endStr);
59 if (pos == std::string::npos) {
62 return pos == str.length() - endStr.length();
65 std::string unique(const std::string& str,
68 std::string result = str;
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) {
77 result.erase(pos + 1, last - pos - 1);
85 bool inCStringArray(const std::string& needle,
89 if (needle == *stack) {
97 char* strdup(const char* str)
99 char* result = ::strdup(str);
100 if (NULL == result) {
101 throw std::bad_alloc();
106 std::string merge(const std::vector<std::string>& strs,
107 std::string::value_type delim)
109 typedef std::vector<std::string> Strings;
111 std::ostringstream ss;
113 for (Strings::size_type i = 0; i < strs.size() - 1; ++i) {
114 ss << strs[i] << delim;
116 ss << strs.at(strs.size() - 1);
121 std::vector<std::string> split(const std::string& str,
122 std::string::value_type delim)
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 ?
129 str.substr(begin, end - begin));
131 result.push_back(part);
133 if (std::string::npos == end) { break; }
139 int toInt(const std::string& str)
141 return convertTo<int>(str);