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.
21 #include <Commons/Exception.h>
22 #include "StringUtils.h"
24 namespace WrtDeviceApis {
28 std::string lTrim(const std::string& str)
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))));
37 std::string rTrim(const std::string& str)
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());
46 std::string trim(const std::string& str)
48 return lTrim(rTrim(str));
51 bool startsWith(const std::string& str,
52 const std::string& startStr)
54 return str.find(startStr) == 0;
57 bool endsWith(const std::string& str,
58 const std::string& endStr)
60 std::size_t pos = str.rfind(endStr);
61 if (pos == std::string::npos) {
64 return pos == str.length() - endStr.length();
67 std::string unique(const std::string& str,
70 std::string result = str;
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) {
79 result.erase(pos + 1, last - pos - 1);
87 bool inCStringArray(const std::string& needle,
91 if (needle == *stack) {
99 char* strdup(const char* str)
101 char* result = ::strdup(str);
102 if (NULL == result) {
103 throw std::bad_alloc();
108 std::string merge(const std::vector<std::string>& strs,
109 std::string::value_type delim)
111 typedef std::vector<std::string> Strings;
113 std::ostringstream ss;
115 for (Strings::size_type i = 0; i < strs.size() - 1; ++i) {
116 ss << strs[i] << delim;
118 ss << strs.at(strs.size() - 1);
123 std::vector<std::string> split(const std::string& str,
124 std::string::value_type delim)
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 ?
131 str.substr(begin, end - begin));
133 result.push_back(part);
135 if (std::string::npos == end) { break; }
141 int toInt(const std::string &text)
144 std::stringstream ss(text);
145 if (!(ss >> result)) {
146 Throw(ConversionException);
153 } // WrtDeviceApisCommon