#pragma once #include #include #include #include #include namespace nix { /* * workaround for unavailable view() method (C++20) of std::ostringstream under MacOS with clang-16 */ std::string_view toView(const std::ostringstream & os); /** * String tokenizer. * * See also `basicSplitString()`, which preserves empty strings between separators, as well as at the start and end. */ template C basicTokenizeString(std::basic_string_view s, std::basic_string_view separators); /** * Like `basicTokenizeString` but specialized to the default `char` */ template C tokenizeString(std::string_view s, std::string_view separators = " \t\n\r"); extern template std::list tokenizeString(std::string_view s, std::string_view separators); extern template std::set tokenizeString(std::string_view s, std::string_view separators); extern template std::vector tokenizeString(std::string_view s, std::string_view separators); /** * Split a string, preserving empty strings between separators, as well as at the start and end. * * Returns a non-empty collection of strings. */ template C basicSplitString(std::basic_string_view s, std::basic_string_view separators); template C splitString(std::string_view s, std::string_view separators); extern template std::list splitString(std::string_view s, std::string_view separators); extern template std::set splitString(std::string_view s, std::string_view separators); extern template std::vector splitString(std::string_view s, std::string_view separators); /** * Concatenate the given strings with a separator between the elements. */ template std::string concatStringsSep(const std::string_view sep, const C & ss); extern template std::string concatStringsSep(std::string_view, const std::list &); extern template std::string concatStringsSep(std::string_view, const std::set &); extern template std::string concatStringsSep(std::string_view, const std::vector &); /** * Ignore any empty strings at the start of the list, and then concatenate the * given strings with a separator between the elements. * * @deprecated This function exists for historical reasons. You probably just * want to use `concatStringsSep`. */ template [[deprecated( "Consider removing the empty string dropping behavior. If acceptable, use concatStringsSep instead.")]] std::string dropEmptyInitThenConcatStringsSep(const std::string_view sep, const C & ss); extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::list &); extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::set &); extern template std::string dropEmptyInitThenConcatStringsSep(std::string_view, const std::vector &); /** * Shell split string: split a string into shell arguments, respecting quotes and backslashes. * * Used for NIX_SSHOPTS handling, which previously used `tokenizeString` and was broken by * Arguments that need to be passed to ssh with spaces in them. */ std::list shellSplitString(std::string_view s); }