move string helpers from libcros
The only project using these funcs is libcros, so move them here
to avoid having to create a small shared library for it.
While we're at it, rename the files from "string.h" to "cros_string.h"
to avoid implicit collision with the system "string.h".
BUG=chromium-os:29973
TEST=`cbuildbot {x86,amd64,arm}-generic-full` worked
Change-Id: Ic0c5bb481f163f26108c6bcfd18e731b18ff628d
Reviewed-on: https://gerrit.chromium.org/gerrit/25044
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
Reviewed-by: Chris Masone <cmasone@chromium.org>
Commit-Ready: Mike Frysinger <vapier@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/SConstruct.chromiumos b/SConstruct.chromiumos
index 97270dc..ff6ddec 100644
--- a/SConstruct.chromiumos
+++ b/SConstruct.chromiumos
@@ -67,6 +67,7 @@
SOURCES = Split('''\
chromeos_network.cc
+ cros_string.cc
marshal.glibmarshal.c
version_check.cc
''')
diff --git a/chromeos_network.cc b/chromeos_network.cc
index 4e2b23d..14810c1 100644
--- a/chromeos_network.cc
+++ b/chromeos_network.cc
@@ -18,7 +18,7 @@
#include "chromeos/dbus/dbus.h" // NOLINT
#include "chromeos/dbus/service_constants.h" // NOLINT
#include "chromeos/glib/object.h" // NOLINT
-#include "chromeos/string.h"
+#include "cros_string.h"
using namespace cashew;
using namespace flimflam;
diff --git a/cros_string.cc b/cros_string.cc
new file mode 100644
index 0000000..c9db942
--- /dev/null
+++ b/cros_string.cc
@@ -0,0 +1,56 @@
+// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <cstring>
+
+#include "cros_string.h"
+
+#include <pcrecpp.h>
+#include "base/logging.h"
+
+namespace chromeos {
+
+void SplitString(const std::string& str, std::vector<std::string>* parts) {
+ CHECK(parts);
+ parts->clear();
+ static pcrecpp::RE re("\\s*(\\S+)");
+ pcrecpp::StringPiece input(str);
+
+ // TODO (seanparent) : This code copies each substring into the vector.
+ // look at the gcc implementation of string (is it copy-on-write?). Likely
+ // it would be better to insert and empty string then swap the part into
+ // place.
+
+ std::string part;
+ while (re.Consume(&input, &part))
+ parts->push_back(part);
+}
+
+void SplitStringUsing(const std::string& str,
+ const std::string& delim,
+ std::vector<std::string>* parts) {
+ CHECK(parts);
+ CHECK(!delim.empty());
+ parts->clear();
+ size_t start = 0;
+ while (start < str.size()) {
+ size_t delim_pos = str.find(delim, start);
+ if (delim_pos == std::string::npos) {
+ delim_pos = str.size();
+ }
+ if (delim_pos > start) {
+ parts->push_back(str.substr(start, delim_pos - start));
+ }
+ start = delim_pos + delim.size();
+ }
+}
+
+char* NewStringCopy(const char* x) {
+ char* result = static_cast<char*>(::operator new(std::strlen(x) + 1));
+ std::strcpy(result, x); // NOLINT
+ return result;
+}
+
+} // namespace chromeos
+
diff --git a/cros_string.h b/cros_string.h
new file mode 100644
index 0000000..1216524
--- /dev/null
+++ b/cros_string.h
@@ -0,0 +1,26 @@
+// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROMEOS_STRING_H_
+#define CHROMEOS_STRING_H_
+
+#include <string>
+#include <vector>
+
+namespace chromeos {
+
+void SplitString(const std::string& str, std::vector<std::string>* parts);
+
+void SplitStringUsing(const std::string& str,
+ const std::string& delim,
+ std::vector<std::string>* parts);
+
+// Copies the string. Use delete (not delete [] or free) to release the returned
+// string.
+char* NewStringCopy(const char* x);
+
+} // namespace chromeos
+
+#endif /* CHROMEOS_STRING_H_ */
+