]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Add util::split_once
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 21 Jun 2021 06:20:44 +0000 (08:20 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 27 Jun 2021 07:01:19 +0000 (09:01 +0200)
src/util/CMakeLists.txt
src/util/string_utils.cpp [new file with mode: 0644]
src/util/string_utils.hpp [new file with mode: 0644]
unittest/CMakeLists.txt
unittest/test_util_string_utils.cpp [new file with mode: 0644]

index 0ca3a4de329b71cc716ef8455a246bd91364c5e4..a9e1f0438ad37be930c70bc934337e8a06dc6e64 100644 (file)
@@ -2,6 +2,7 @@ set(
   sources
   ${CMAKE_CURRENT_SOURCE_DIR}/file_utils.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/Tokenizer.cpp
+  ${CMAKE_CURRENT_SOURCE_DIR}/string_utils.cpp
 )
 
 target_sources(ccache_lib PRIVATE ${sources})
diff --git a/src/util/string_utils.cpp b/src/util/string_utils.cpp
new file mode 100644 (file)
index 0000000..da951e5
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include "string_utils.hpp"
+
+namespace util {
+
+std::pair<nonstd::string_view, nonstd::optional<nonstd::string_view>>
+split_once(const nonstd::string_view string, const char split_char)
+{
+  const size_t sep_pos = string.find(split_char);
+  if (sep_pos == nonstd::string_view::npos) {
+    return std::make_pair(string, nonstd::nullopt);
+  } else {
+    return std::make_pair(string.substr(0, sep_pos),
+                          string.substr(sep_pos + 1));
+  }
+}
+
+} // namespace util
diff --git a/src/util/string_utils.hpp b/src/util/string_utils.hpp
new file mode 100644 (file)
index 0000000..7b27441
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#pragma once
+
+#include <third_party/nonstd/optional.hpp>
+#include <third_party/nonstd/string_view.hpp>
+
+// System headers
+#include <utility>
+// End of system headers
+
+namespace util {
+
+// Split `string` into two parts using `split_char` as the delimiter. The second
+// part will be `nullopt` if there is no `split_char` in `string.`
+std::pair<nonstd::string_view, nonstd::optional<nonstd::string_view>>
+split_once(nonstd::string_view string, char split_char);
+
+} // namespace util
index fc1ddfa465fdab01ec16d132450882c4bc6123aa..68e462986ffe84d7a39510eda8064abbbcbdddb9 100644 (file)
@@ -22,6 +22,7 @@ set(
   test_compopt.cpp
   test_hashutil.cpp
   test_util_Tokenizer.cpp
+  test_util_string_utils.cpp
 )
 
 if(INODE_CACHE_SUPPORTED)
diff --git a/unittest/test_util_string_utils.cpp b/unittest/test_util_string_utils.cpp
new file mode 100644 (file)
index 0000000..ea74dd0
--- /dev/null
@@ -0,0 +1,46 @@
+// Copyright (C) 2021 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include <util/string_utils.hpp>
+
+#include <third_party/doctest.h>
+
+static bool
+operator==(
+  std::pair<nonstd::string_view, nonstd::optional<nonstd::string_view>> left,
+  std::pair<nonstd::string_view, nonstd::optional<nonstd::string_view>> right)
+{
+  return left.first == right.first && left.second == right.second;
+}
+
+TEST_CASE("util::split_once")
+{
+  using nonstd::nullopt;
+  using std::make_pair;
+  using util::split_once;
+
+  CHECK(split_once("", '=') == make_pair("", nullopt));
+  CHECK(split_once("a", '=') == make_pair("a", nullopt));
+  CHECK(split_once("=a", '=') == make_pair("", "a"));
+  CHECK(split_once("a=", '=') == make_pair("a", ""));
+  CHECK(split_once("a==", '=') == make_pair("a", "="));
+  CHECK(split_once("a=b", '=') == make_pair("a", "b"));
+  CHECK(split_once("a=b=", '=') == make_pair("a", "b="));
+  CHECK(split_once("a=b=c", '=') == make_pair("a", "b=c"));
+  CHECK(split_once("x y", ' ') == make_pair("x", "y"));
+}