2015-03-14 13:57:40 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-12-05 14:00:26 +08:00
|
|
|
#include "android-base/strings.h"
|
2015-03-14 13:57:40 +08:00
|
|
|
|
2015-03-20 04:24:26 +08:00
|
|
|
#include <stdlib.h>
|
2015-03-27 14:23:32 +08:00
|
|
|
#include <string.h>
|
2015-03-20 04:24:26 +08:00
|
|
|
|
2015-03-14 13:57:40 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace android {
|
|
|
|
namespace base {
|
|
|
|
|
2015-03-20 04:24:26 +08:00
|
|
|
#define CHECK_NE(a, b) \
|
|
|
|
if ((a) == (b)) abort();
|
|
|
|
|
|
|
|
std::vector<std::string> Split(const std::string& s,
|
|
|
|
const std::string& delimiters) {
|
|
|
|
CHECK_NE(delimiters.size(), 0U);
|
|
|
|
|
2015-04-25 14:02:00 +08:00
|
|
|
std::vector<std::string> result;
|
2015-03-20 04:24:26 +08:00
|
|
|
|
|
|
|
size_t base = 0;
|
|
|
|
size_t found;
|
|
|
|
do {
|
|
|
|
found = s.find_first_of(delimiters, base);
|
2015-04-25 14:02:00 +08:00
|
|
|
result.push_back(s.substr(base, found - base));
|
2015-03-20 04:24:26 +08:00
|
|
|
base = found + 1;
|
|
|
|
} while (found != s.npos);
|
|
|
|
|
2015-04-25 14:02:00 +08:00
|
|
|
return result;
|
2015-03-14 13:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string Trim(const std::string& s) {
|
|
|
|
std::string result;
|
|
|
|
|
|
|
|
if (s.size() == 0) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t start_index = 0;
|
|
|
|
size_t end_index = s.size() - 1;
|
|
|
|
|
|
|
|
// Skip initial whitespace.
|
|
|
|
while (start_index < s.size()) {
|
|
|
|
if (!isspace(s[start_index])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
start_index++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip terminating whitespace.
|
|
|
|
while (end_index >= start_index) {
|
|
|
|
if (!isspace(s[end_index])) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
end_index--;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All spaces, no beef.
|
|
|
|
if (end_index < start_index) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
// Start_index is the first non-space, end_index is the last one.
|
|
|
|
return s.substr(start_index, end_index - start_index + 1);
|
|
|
|
}
|
|
|
|
|
2015-05-22 09:37:36 +08:00
|
|
|
// These cases are probably the norm, so we mark them extern in the header to
|
|
|
|
// aid compile time and binary size.
|
|
|
|
template std::string Join(const std::vector<std::string>&, char);
|
|
|
|
template std::string Join(const std::vector<const char*>&, char);
|
2015-10-31 09:54:38 +08:00
|
|
|
template std::string Join(const std::vector<std::string>&, const std::string&);
|
|
|
|
template std::string Join(const std::vector<const char*>&, const std::string&);
|
2015-03-14 13:57:40 +08:00
|
|
|
|
|
|
|
bool StartsWith(const std::string& s, const char* prefix) {
|
2016-10-26 05:56:04 +08:00
|
|
|
return strncmp(s.c_str(), prefix, strlen(prefix)) == 0;
|
2015-03-14 13:57:40 +08:00
|
|
|
}
|
|
|
|
|
2016-10-26 05:56:04 +08:00
|
|
|
bool StartsWithIgnoreCase(const std::string& s, const char* prefix) {
|
|
|
|
return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool EndsWith(const std::string& s, const char* suffix, bool case_sensitive) {
|
2015-03-14 13:57:40 +08:00
|
|
|
size_t suffix_length = strlen(suffix);
|
|
|
|
size_t string_length = s.size();
|
|
|
|
if (suffix_length > string_length) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
size_t offset = string_length - suffix_length;
|
2016-10-26 05:56:04 +08:00
|
|
|
return (case_sensitive ? strncmp : strncasecmp)(s.c_str() + offset, suffix, suffix_length) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EndsWith(const std::string& s, const char* suffix) {
|
|
|
|
return EndsWith(s, suffix, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool EndsWithIgnoreCase(const std::string& s, const char* suffix) {
|
|
|
|
return EndsWith(s, suffix, false);
|
2015-03-14 13:57:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace base
|
|
|
|
} // namespace android
|