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})
--- /dev/null
+// 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
--- /dev/null
+// 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
test_compopt.cpp
test_hashutil.cpp
test_util_Tokenizer.cpp
+ test_util_string_utils.cpp
)
if(INODE_CACHE_SUPPORTED)
--- /dev/null
+// 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"));
+}