From: Joel Rosdahl Date: Mon, 21 Jun 2021 06:20:44 +0000 (+0200) Subject: Add util::split_once X-Git-Tag: v4.4~185 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8abad0b7d3d66408c12c7611f13cd5cb856ddf27;p=thirdparty%2Fccache.git Add util::split_once --- diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index 0ca3a4de3..a9e1f0438 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -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 index 000000000..da951e599 --- /dev/null +++ b/src/util/string_utils.cpp @@ -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> +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 index 000000000..7b2744186 --- /dev/null +++ b/src/util/string_utils.hpp @@ -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 +#include + +// System headers +#include +// 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> +split_once(nonstd::string_view string, char split_char); + +} // namespace util diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt index fc1ddfa46..68e462986 100644 --- a/unittest/CMakeLists.txt +++ b/unittest/CMakeLists.txt @@ -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 index 000000000..ea74dd007 --- /dev/null +++ b/unittest/test_util_string_utils.cpp @@ -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 + +#include + +static bool +operator==( + std::pair> left, + std::pair> 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")); +}