summaryrefslogtreecommitdiffstats
path: root/base/strings.cpp
diff options
context:
space:
mode:
authorDan Albert <danalbert@google.com>2015-03-13 22:57:40 -0700
committerDan Albert <danalbert@google.com>2015-03-16 20:03:03 -0700
commit0f1e54491ceace259d156313e583aeffbcd9b444 (patch)
tree3cf78554c63b23d168dff547e721083a0a693dc1 /base/strings.cpp
parentaa9eca2955cebf7d7e6b049750ed088329e615ed (diff)
downloadsystem_core-0f1e54491ceace259d156313e583aeffbcd9b444.zip
system_core-0f1e54491ceace259d156313e583aeffbcd9b444.tar.gz
system_core-0f1e54491ceace259d156313e583aeffbcd9b444.tar.bz2
Add common string utilities to libbase.
These are useful outside of ART. Nothing changed (aside from fixing Trim to not segfault on empty strings), so ART should be able to move to using these. Change-Id: Id026ebffe8d31f784a91834786ab189680b13a0f
Diffstat (limited to 'base/strings.cpp')
-rw-r--r--base/strings.cpp111
1 files changed, 111 insertions, 0 deletions
diff --git a/base/strings.cpp b/base/strings.cpp
new file mode 100644
index 0000000..224a46f
--- /dev/null
+++ b/base/strings.cpp
@@ -0,0 +1,111 @@
+/*
+ * 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.
+ */
+
+#include "base/strings.h"
+
+#include <string>
+#include <vector>
+
+namespace android {
+namespace base {
+
+void Split(const std::string& s, char separator,
+ std::vector<std::string>* result) {
+ const char* p = s.data();
+ const char* end = p + s.size();
+ while (p != end) {
+ if (*p == separator) {
+ ++p;
+ } else {
+ const char* start = p;
+ while (++p != end && *p != separator) {
+ // Skip to the next occurrence of the separator.
+ }
+ result->push_back(std::string(start, p - start));
+ }
+ }
+}
+
+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);
+}
+
+template <typename StringT>
+std::string Join(const std::vector<StringT>& strings, char separator) {
+ if (strings.empty()) {
+ return "";
+ }
+
+ std::string result(strings[0]);
+ for (size_t i = 1; i < strings.size(); ++i) {
+ result += separator;
+ result += strings[i];
+ }
+ return result;
+}
+
+// Explicit instantiations.
+template std::string Join<std::string>(const std::vector<std::string>& strings,
+ char separator);
+template std::string Join<const char*>(const std::vector<const char*>& strings,
+ char separator);
+
+bool StartsWith(const std::string& s, const char* prefix) {
+ return s.compare(0, strlen(prefix), prefix) == 0;
+}
+
+bool EndsWith(const std::string& s, const char* suffix) {
+ 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;
+ return s.compare(offset, suffix_length, suffix) == 0;
+}
+
+} // namespace base
+} // namespace android