diff --git a/.inshpect.toml b/.inshpect.toml index 9270107c8..6b8fc272d 100644 --- a/.inshpect.toml +++ b/.inshpect.toml @@ -335,8 +335,8 @@ patterns = [ { pattern = '\bstd::error_(code|condition|category)\b', include = 'system_error' }, { pattern = '\bstd::system_error\b', include = 'system_error' }, { pattern = '\bpika::intrusive_ptr\b', include = 'pika/memory/intrusive_ptr.hpp' }, - { pattern = '\bpika::util::from_string\b', include = 'pika/string_util/from_string.hpp' }, - { pattern = '\bpika::util::to_string\b', include = 'pika/string_util/to_string.hpp' }, + { pattern = '\bpika::detail::from_string\b', include = 'pika/string_util/from_string.hpp' }, + { pattern = '\bpika::detail::to_string\b', include = 'pika/string_util/to_string.hpp' }, { pattern = '\bPIKA_PP_CAT\b', include = 'pika/preprocessor/cat.hpp' }, { pattern = '\bPIKA_PP_EXPAND\b', include = 'pika/preprocessor/expand.hpp' }, { pattern = '\bPIKA_PP_NARGS\b', include = 'pika/preprocessor/nargs.hpp' }, diff --git a/examples/quickstart/pipeline1.cpp b/examples/quickstart/pipeline1.cpp index 04d1c18a5..f72ac07b8 100644 --- a/examples/quickstart/pipeline1.cpp +++ b/examples/quickstart/pipeline1.cpp @@ -25,7 +25,7 @@ struct pipeline if (std::regex_match(item, regex)) { auto trim = [](std::string const& s) { - return pika::string_util::trim_copy(s); + return pika::detail::trim_copy(s); }; pika::async(trim, std::move(item)) diff --git a/libs/pika/command_line_handling/src/command_line_handling.cpp b/libs/pika/command_line_handling/src/command_line_handling.cpp index 18202eed8..36a7d6f36 100644 --- a/libs/pika/command_line_handling/src/command_line_handling.cpp +++ b/libs/pika/command_line_handling/src/command_line_handling.cpp @@ -273,7 +273,8 @@ namespace pika::detail { } else { - default_threads = pika::util::from_string(threads_str); + default_threads = + pika::detail::from_string(threads_str); } std::size_t threads = @@ -292,7 +293,7 @@ namespace pika::detail { } else { - threads = pika::util::from_string(threads_str); + threads = pika::detail::from_string(threads_str); } if (threads == 0) @@ -368,7 +369,7 @@ namespace pika::detail { } else { - num_cores = pika::util::from_string(cores_str); + num_cores = pika::detail::from_string(cores_str); } } diff --git a/libs/pika/command_line_handling/src/late_command_line_handling.cpp b/libs/pika/command_line_handling/src/late_command_line_handling.cpp index 1a912a1f9..96d281c0e 100644 --- a/libs/pika/command_line_handling/src/late_command_line_handling.cpp +++ b/libs/pika/command_line_handling/src/late_command_line_handling.cpp @@ -121,7 +121,7 @@ namespace pika::detail { if (vm.count("pika:print-bind")) { std::size_t num_threads = - pika::util::from_string( + pika::detail::from_string( ini.get_entry("pika.os_threads", 1)); handle_print_bind(num_threads); } diff --git a/libs/pika/ini/include/pika/ini/ini.hpp b/libs/pika/ini/include/pika/ini/ini.hpp index 636cd5eff..e6e382eba 100644 --- a/libs/pika/ini/include/pika/ini/ini.hpp +++ b/libs/pika/ini/include/pika/ini/ini.hpp @@ -197,7 +197,7 @@ namespace pika::util { std::string get_entry(std::string const& key, T dflt) const { std::unique_lock l(mtx_); - return get_entry(l, key, pika::util::to_string(dflt)); + return get_entry(l, key, pika::detail::to_string(dflt)); } void add_notification_callback( diff --git a/libs/pika/ini/src/ini.cpp b/libs/pika/ini/src/ini.cpp index afce54783..62d2eea09 100644 --- a/libs/pika/ini/src/ini.cpp +++ b/libs/pika/ini/src/ini.cpp @@ -678,8 +678,7 @@ namespace pika::util { using string_vector = std::vector; string_vector split_key; - pika::string_util::split( - split_key, key, pika::string_util::is_any_of(".")); + pika::detail::split(split_key, key, pika::detail::is_any_of(".")); std::string sk = split_key.back(); split_key.pop_back(); diff --git a/libs/pika/init_runtime/tests/unit/config_entry.cpp b/libs/pika/init_runtime/tests/unit/config_entry.cpp index 2550e0bb5..71c7b07aa 100644 --- a/libs/pika/init_runtime/tests/unit/config_entry.cpp +++ b/libs/pika/init_runtime/tests/unit/config_entry.cpp @@ -16,11 +16,11 @@ void test_get_entry() { std::string val = pika::get_config_entry("pika.localities", "42"); PIKA_TEST(!val.empty()); - PIKA_TEST_EQ(pika::util::from_string(val), 1); + PIKA_TEST_EQ(pika::detail::from_string(val), 1); val = pika::get_config_entry("pika.localities", 42); PIKA_TEST(!val.empty()); - PIKA_TEST_EQ(pika::util::from_string(val), 1); + PIKA_TEST_EQ(pika::detail::from_string(val), 1); } std::atomic invoked_callback(false); diff --git a/libs/pika/logging/src/logging.cpp b/libs/pika/logging/src/logging.cpp index 9383582f8..c61381a6b 100644 --- a/libs/pika/logging/src/logging.cpp +++ b/libs/pika/logging/src/logging.cpp @@ -39,7 +39,7 @@ namespace pika::util { { try { - int env_val = pika::util::from_string(env); + int env_val = pika::detail::from_string(env); if (env_val < 0) return pika::util::logging::level::disable_all; @@ -62,7 +62,7 @@ namespace pika::util { } return pika::util::logging::level::debug; } - catch (pika::util::bad_lexical_cast const&) + catch (pika::detail::bad_lexical_cast const&) { return pika::util::logging::level::disable_all; } diff --git a/libs/pika/prefix/src/find_prefix.cpp b/libs/pika/prefix/src/find_prefix.cpp index 36fb1ec25..801f4895b 100644 --- a/libs/pika/prefix/src/find_prefix.cpp +++ b/libs/pika/prefix/src/find_prefix.cpp @@ -105,9 +105,9 @@ namespace pika::util { { std::vector path_dirs; - pika::string_util::split(path_dirs, epath, - pika::string_util::is_any_of(":"), - pika::string_util::token_compress_mode::on); + pika::detail::split(path_dirs, epath, + pika::detail::is_any_of(":"), + pika::detail::token_compress_mode::on); for (std::uint64_t i = 0; i < path_dirs.size(); ++i) { diff --git a/libs/pika/program_options/examples/regex.cpp b/libs/pika/program_options/examples/regex.cpp index 6f75b3124..c358f8fbf 100644 --- a/libs/pika/program_options/examples/regex.cpp +++ b/libs/pika/program_options/examples/regex.cpp @@ -70,7 +70,7 @@ void validate( std::smatch match; if (regex_match(s, match, r)) { - v = std::any(magic_number(pika::util::from_string(match[1]))); + v = std::any(magic_number(pika::detail::from_string(match[1]))); } else { diff --git a/libs/pika/program_options/include/pika/program_options/detail/value_semantic.hpp b/libs/pika/program_options/include/pika/program_options/detail/value_semantic.hpp index 15cbcd96c..4830891be 100644 --- a/libs/pika/program_options/include/pika/program_options/detail/value_semantic.hpp +++ b/libs/pika/program_options/include/pika/program_options/detail/value_semantic.hpp @@ -107,9 +107,9 @@ namespace pika::program_options { std::basic_string s(validators::get_single_string(xs)); try { - v = std::any(pika::util::from_string(s)); + v = std::any(pika::detail::from_string(s)); } - catch (const pika::util::bad_lexical_cast&) + catch (const pika::detail::bad_lexical_cast&) { throw invalid_option_value(s); } @@ -154,7 +154,7 @@ namespace pika::program_options { validate(a, cv, (T*) nullptr, 0); tv->push_back(std::any_cast(a)); } - catch (const pika::util::bad_lexical_cast& /*e*/) + catch (const pika::detail::bad_lexical_cast& /*e*/) { throw invalid_option_value(s[i]); } diff --git a/libs/pika/program_options/include/pika/program_options/value_semantic.hpp b/libs/pika/program_options/include/pika/program_options/value_semantic.hpp index 845ebaa54..8ccf20564 100644 --- a/libs/pika/program_options/include/pika/program_options/value_semantic.hpp +++ b/libs/pika/program_options/include/pika/program_options/value_semantic.hpp @@ -210,7 +210,7 @@ namespace pika::program_options { typed_value* default_value(const T& v) { m_default_value = std::any(v); - m_default_value_as_text = pika::util::to_string(v); + m_default_value_as_text = pika::detail::to_string(v); return this; } @@ -234,7 +234,7 @@ namespace pika::program_options { typed_value* implicit_value(const T& v) { m_implicit_value = std::any(v); - m_implicit_value_as_text = pika::util::to_string(v); + m_implicit_value_as_text = pika::detail::to_string(v); return this; } diff --git a/libs/pika/resource_partitioner/src/detail_partitioner.cpp b/libs/pika/resource_partitioner/src/detail_partitioner.cpp index 6cec1f7bd..4a7c15bb2 100644 --- a/libs/pika/resource_partitioner/src/detail_partitioner.cpp +++ b/libs/pika/resource_partitioner/src/detail_partitioner.cpp @@ -240,7 +240,7 @@ namespace pika::resource::detail { if (!default_scheduler_mode_str.empty()) { default_scheduler_mode_ = - threads::scheduler_mode(pika::util::from_string( + threads::scheduler_mode(pika::detail::from_string( default_scheduler_mode_str)); PIKA_ASSERT_MSG((default_scheduler_mode_ & ~threads::scheduler_mode::all_flags) == diff --git a/libs/pika/runtime/src/custom_exception_info.cpp b/libs/pika/runtime/src/custom_exception_info.cpp index c6c672589..a12c45023 100644 --- a/libs/pika/runtime/src/custom_exception_info.cpp +++ b/libs/pika/runtime/src/custom_exception_info.cpp @@ -64,7 +64,7 @@ namespace pika { // return a string holding a formatted message. std::string diagnostic_information(pika::exception_info const& xi) { - int const verbosity = util::from_string( + int const verbosity = detail::from_string( get_config_entry("pika.exception_verbosity", "1")); std::ostringstream strm; @@ -377,7 +377,7 @@ namespace pika { std::int64_t pid = ::getpid(); std::size_t const trace_depth = - util::from_string(get_config_entry( + detail::from_string(get_config_entry( "pika.trace_depth", PIKA_HAVE_THREAD_BACKTRACE_DEPTH)); pika::debug::detail::backtrace bt(trace_depth); diff --git a/libs/pika/runtime/src/runtime.cpp b/libs/pika/runtime/src/runtime.cpp index f9ee4d919..38f7f6dbd 100644 --- a/libs/pika/runtime/src/runtime.cpp +++ b/libs/pika/runtime/src/runtime.cpp @@ -77,7 +77,7 @@ namespace pika { if (get_config_entry("pika.diagnostics_on_terminate", "1") == "1") { - int const verbosity = util::from_string( + int const verbosity = detail::from_string( get_config_entry("pika.exception_verbosity", "1")); if (verbosity >= 2) @@ -89,7 +89,7 @@ namespace pika { if (verbosity >= 1) { std::size_t const trace_depth = - util::from_string(get_config_entry( + detail::from_string(get_config_entry( "pika.trace_depth", PIKA_HAVE_THREAD_BACKTRACE_DEPTH)); std::cerr << "{stack-trace}: " << pika::debug::detail::trace(trace_depth) << "\n"; @@ -150,7 +150,7 @@ namespace pika { if (get_config_entry("pika.diagnostics_on_terminate", "1") == "1") { - int const verbosity = util::from_string( + int const verbosity = detail::from_string( get_config_entry("pika.exception_verbosity", "1")); char* reason = strsignal(signum); @@ -164,7 +164,7 @@ namespace pika { if (verbosity >= 1) { std::size_t const trace_depth = - util::from_string(get_config_entry( + detail::from_string(get_config_entry( "pika.trace_depth", PIKA_HAVE_THREAD_BACKTRACE_DEPTH)); std::cerr << "{stack-trace}: " << pika::debug::detail::trace(trace_depth) << "\n"; diff --git a/libs/pika/string_util/include/pika/string_util/bad_lexical_cast.hpp b/libs/pika/string_util/include/pika/string_util/bad_lexical_cast.hpp index 9269702e0..c66bb001b 100644 --- a/libs/pika/string_util/include/pika/string_util/bad_lexical_cast.hpp +++ b/libs/pika/string_util/include/pika/string_util/bad_lexical_cast.hpp @@ -12,7 +12,7 @@ #include -namespace pika::util { +namespace pika::detail { class PIKA_EXPORT bad_lexical_cast : public std::bad_cast { public: @@ -50,19 +50,15 @@ namespace pika::util { std::type_info const* target; }; - namespace detail { + [[noreturn]] PIKA_EXPORT void throw_bad_lexical_cast( + std::type_info const& source_type, std::type_info const& target_type); - [[noreturn]] PIKA_EXPORT void throw_bad_lexical_cast( - std::type_info const& source_type, - std::type_info const& target_type); - - template - [[noreturn]] inline Target throw_bad_lexical_cast() - { - detail::throw_bad_lexical_cast(typeid(Source), typeid(Target)); - } - } // namespace detail + template + [[noreturn]] inline Target throw_bad_lexical_cast() + { + throw_bad_lexical_cast(typeid(Source), typeid(Target)); + } -} // namespace pika::util +} // namespace pika::detail #include diff --git a/libs/pika/string_util/include/pika/string_util/case_conv.hpp b/libs/pika/string_util/include/pika/string_util/case_conv.hpp index afcf9da74..9a83d5431 100644 --- a/libs/pika/string_util/include/pika/string_util/case_conv.hpp +++ b/libs/pika/string_util/include/pika/string_util/case_conv.hpp @@ -10,11 +10,11 @@ #include #include -namespace pika::string_util { +namespace pika::detail { template void to_lower(std::basic_string& s) { std::transform(std::begin(s), std::end(s), std::begin(s), [](int c) { return std::tolower(c); }); } -} // namespace pika::string_util +} // namespace pika::detail diff --git a/libs/pika/string_util/include/pika/string_util/classification.hpp b/libs/pika/string_util/include/pika/string_util/classification.hpp index 6e5c927ed..8f6e4e731 100644 --- a/libs/pika/string_util/include/pika/string_util/classification.hpp +++ b/libs/pika/string_util/include/pika/string_util/classification.hpp @@ -9,38 +9,29 @@ #include #include -namespace pika::string_util { - namespace detail { - template - struct is_any_of_pred +namespace pika::detail { + template + struct is_any_of_pred + { + bool operator()(int c) const noexcept { - bool operator()(int c) const noexcept - { - return chars.find(c) != std::string::npos; - } + return chars.find(c) != std::string::npos; + } - std::basic_string chars; - }; - } // namespace detail + std::basic_string chars; + }; template - detail::is_any_of_pred + is_any_of_pred is_any_of(std::basic_string const& chars) { - return detail::is_any_of_pred{chars}; + return is_any_of_pred{chars}; } inline auto is_any_of(char const* chars) { - return detail::is_any_of_pred, + return is_any_of_pred, std::allocator>{std::string{chars}}; } - struct is_space - { - bool operator()(int c) const noexcept - { - return std::isspace(c); - } - }; -} // namespace pika::string_util +} // namespace pika::detail diff --git a/libs/pika/string_util/include/pika/string_util/from_string.hpp b/libs/pika/string_util/include/pika/string_util/from_string.hpp index 1be52012e..bb3fe9b74 100644 --- a/libs/pika/string_util/include/pika/string_util/from_string.hpp +++ b/libs/pika/string_util/include/pika/string_util/from_string.hpp @@ -19,25 +19,24 @@ #include #include -namespace pika::util { - namespace detail { - template - struct from_string - { - template - static void call(std::basic_string const& value, T& target) - { - std::basic_istringstream stream(value); - stream.exceptions(std::ios_base::failbit); - stream >> target; - } - }; - - template - T check_out_of_range(U const& value) - { - U const min = (std::numeric_limits::min)(); - U const max = (std::numeric_limits::max)(); +namespace pika::detail { + template + struct from_string_impl + { + template + static void call(std::basic_string const& value, T& target) + { + std::basic_istringstream stream(value); + stream.exceptions(std::ios_base::failbit); + stream >> target; + } + }; + + template + T check_out_of_range(U const& value) + { + U const min = (std::numeric_limits::min)(); + U const max = (std::numeric_limits::max)(); #if defined(PIKA_INTEL_VERSION) #pragma warning(push) #pragma warning(disable : 186) @@ -48,8 +47,8 @@ namespace pika::util { #pragma diag_suppress 186 #endif #endif - if (value < min || value > max) - throw std::out_of_range("from_string: out of range"); + if (value < min || value > max) + throw std::out_of_range("from_string: out of range"); #if defined(PIKA_INTEL_VERSION) #pragma warning(pop) #elif defined(PIKA_CUDA_VERSION) @@ -59,125 +58,120 @@ namespace pika::util { #pragma diag_default 186 #endif #endif - return static_cast(value); + return static_cast(value); + } + + template + void + check_only_whitespace(std::basic_string const& s, std::size_t pos) + { + auto i = s.begin(); + std::advance(i, pos); + i = std::find_if(i, s.end(), [](int c) { return !std::isspace(c); }); + + if (i != s.end()) + { + throw std::invalid_argument( + "from_string: found non-whitespace after token"); + } + } + + template + struct from_string_impl>> + { + template + static void call(std::basic_string const& value, int& target) + { + std::size_t pos = 0; + target = std::stoi(value, &pos); + check_only_whitespace(value, pos); + } + + template + static void call(std::basic_string const& value, long& target) + { + std::size_t pos = 0; + target = std::stol(value, &pos); + check_only_whitespace(value, pos); } template - void - check_only_whitespace(std::basic_string const& s, std::size_t pos) - { - auto i = s.begin(); - std::advance(i, pos); - i = std::find_if( - i, s.end(), [](int c) { return !std::isspace(c); }); - - if (i != s.end()) - { - throw std::invalid_argument( - "from_string: found non-whitespace after token"); - } - } - - template - struct from_string::value>> - { - template - static void call(std::basic_string const& value, int& target) - { - std::size_t pos = 0; - target = std::stoi(value, &pos); - check_only_whitespace(value, pos); - } - - template - static void call(std::basic_string const& value, long& target) - { - std::size_t pos = 0; - target = std::stol(value, &pos); - check_only_whitespace(value, pos); - } - - template - static void - call(std::basic_string const& value, long long& target) - { - std::size_t pos = 0; - target = std::stoll(value, &pos); - check_only_whitespace(value, pos); - } - - template - static void - call(std::basic_string const& value, unsigned int& target) - { - // there is no std::stoui - unsigned long target_long; - call(value, target_long); - target = check_out_of_range(target_long); - } - - template - static void - call(std::basic_string const& value, unsigned long& target) - { - std::size_t pos = 0; - target = std::stoul(value, &pos); - check_only_whitespace(value, pos); - } - - template - static void call(std::basic_string const& value, - unsigned long long& target) - { - std::size_t pos = 0; - target = std::stoull(value, &pos); - check_only_whitespace(value, pos); - } - - template - static void call(std::basic_string const& value, U& target) - { - using promoted_t = decltype(+std::declval()); - static_assert(!std::is_same::value, ""); - - promoted_t promoted; - call(value, promoted); - target = check_out_of_range(promoted); - } - }; - - template - struct from_string::value>> - { - template - static void - call(std::basic_string const& value, float& target) - { - std::size_t pos = 0; - target = std::stof(value, &pos); - check_only_whitespace(value, pos); - } - - template - static void - call(std::basic_string const& value, double& target) - { - std::size_t pos = 0; - target = std::stod(value, &pos); - check_only_whitespace(value, pos); - } - - template - static void - call(std::basic_string const& value, long double& target) - { - std::size_t pos = 0; - target = std::stold(value, &pos); - check_only_whitespace(value, pos); - } - }; - } // namespace detail + static void + call(std::basic_string const& value, long long& target) + { + std::size_t pos = 0; + target = std::stoll(value, &pos); + check_only_whitespace(value, pos); + } + + template + static void + call(std::basic_string const& value, unsigned int& target) + { + // there is no std::stoui + unsigned long target_long; + call(value, target_long); + target = check_out_of_range(target_long); + } + + template + static void + call(std::basic_string const& value, unsigned long& target) + { + std::size_t pos = 0; + target = std::stoul(value, &pos); + check_only_whitespace(value, pos); + } + + template + static void + call(std::basic_string const& value, unsigned long long& target) + { + std::size_t pos = 0; + target = std::stoull(value, &pos); + check_only_whitespace(value, pos); + } + + template + static void call(std::basic_string const& value, U& target) + { + using promoted_t = decltype(+std::declval()); + static_assert(!std::is_same_v, ""); + + promoted_t promoted; + call(value, promoted); + target = check_out_of_range(promoted); + } + }; + + template + struct from_string_impl>> + { + template + static void call(std::basic_string const& value, float& target) + { + std::size_t pos = 0; + target = std::stof(value, &pos); + check_only_whitespace(value, pos); + } + + template + static void call(std::basic_string const& value, double& target) + { + std::size_t pos = 0; + target = std::stod(value, &pos); + check_only_whitespace(value, pos); + } + + template + static void + call(std::basic_string const& value, long double& target) + { + std::size_t pos = 0; + target = std::stold(value, &pos); + check_only_whitespace(value, pos); + } + }; template T from_string(std::basic_string const& v) @@ -185,11 +179,11 @@ namespace pika::util { T target; try { - detail::from_string::call(v, target); + from_string_impl::call(v, target); } catch (...) { - return detail::throw_bad_lexical_cast, T>(); + return throw_bad_lexical_cast, T>(); } return target; } @@ -200,7 +194,7 @@ namespace pika::util { T target; try { - detail::from_string::call(v, target); + from_string_impl::call(v, target); return target; } catch (...) @@ -215,11 +209,11 @@ namespace pika::util { T target; try { - detail::from_string::call(v, target); + from_string_impl::call(v, target); } catch (...) { - return detail::throw_bad_lexical_cast(); + return throw_bad_lexical_cast(); } return target; } @@ -230,7 +224,7 @@ namespace pika::util { T target; try { - detail::from_string::call(v, target); + from_string_impl::call(v, target); return target; } catch (...) @@ -238,4 +232,4 @@ namespace pika::util { return PIKA_FORWARD(U, default_value); } } -} // namespace pika::util +} // namespace pika::detail diff --git a/libs/pika/string_util/include/pika/string_util/split.hpp b/libs/pika/string_util/include/pika/string_util/split.hpp index 99c85e2b9..79a285f55 100644 --- a/libs/pika/string_util/include/pika/string_util/split.hpp +++ b/libs/pika/string_util/include/pika/string_util/split.hpp @@ -15,19 +15,16 @@ #include #include -namespace pika::string_util { - namespace detail { - template - std::basic_string - substr(std::basic_string const& s, - It const& first, It const& last) - { - std::size_t const pos = std::distance(std::begin(s), first); - std::size_t const count = std::distance(first, last); - return s.substr(pos, count); - } - } // namespace detail +namespace pika::detail { + template + std::basic_string + substr(std::basic_string const& s, + It const& first, It const& last) + { + std::size_t const pos = std::distance(std::begin(s), first); + std::size_t const count = std::distance(first, last); + return s.substr(pos, count); + } enum class token_compress_mode { @@ -51,7 +48,7 @@ namespace pika::string_util { { token_end = std::find_if(token_begin, std::end(str), pred); - container.push_back(detail::substr(str, token_begin, token_end)); + container.push_back(substr(str, token_begin, token_end)); if (token_end != std::end(str)) { @@ -76,4 +73,4 @@ namespace pika::string_util { split(container, std::string{str}, PIKA_FORWARD(Predicate, pred), compress_mode); } -} // namespace pika::string_util +} // namespace pika::detail diff --git a/libs/pika/string_util/include/pika/string_util/to_string.hpp b/libs/pika/string_util/include/pika/string_util/to_string.hpp index afa36d25e..d59730994 100644 --- a/libs/pika/string_util/include/pika/string_util/to_string.hpp +++ b/libs/pika/string_util/include/pika/string_util/to_string.hpp @@ -14,41 +14,38 @@ #include #include -namespace pika::util { +namespace pika::detail { - namespace detail { - template - struct to_string + template + struct to_string_impl + { + static std::string call(T const& value) { - static std::string call(T const& value) - { - return fmt::format("{}", value); - } - }; - - template - struct to_string::value || - std::is_floating_point::value>::type> + return fmt::format("{}", value); + } + }; + + template + struct to_string_impl || std::is_floating_point_v>> + { + static std::string call(T const& value) { - static std::string call(T const& value) - { - return std::to_string(value); - } - }; - } // namespace detail + return std::to_string(value); + } + }; template std::string to_string(T const& v) { try { - return detail::to_string::call(v); + return to_string_impl::call(v); } catch (...) { - return detail::throw_bad_lexical_cast(); + return throw_bad_lexical_cast(); } } -} // namespace pika::util +} // namespace pika::detail diff --git a/libs/pika/string_util/include/pika/string_util/trim.hpp b/libs/pika/string_util/include/pika/string_util/trim.hpp index 9be7dc66c..191df475a 100644 --- a/libs/pika/string_util/include/pika/string_util/trim.hpp +++ b/libs/pika/string_util/include/pika/string_util/trim.hpp @@ -10,7 +10,7 @@ #include #include -namespace pika::string_util { +namespace pika::detail { template void trim(std::basic_string& s) { @@ -42,4 +42,4 @@ namespace pika::string_util { return t; } -} // namespace pika::string_util +} // namespace pika::detail diff --git a/libs/pika/string_util/src/bad_lexical_cast.cpp b/libs/pika/string_util/src/bad_lexical_cast.cpp index c1d999777..0ae304fe0 100644 --- a/libs/pika/string_util/src/bad_lexical_cast.cpp +++ b/libs/pika/string_util/src/bad_lexical_cast.cpp @@ -8,7 +8,7 @@ #include -namespace pika::util { +namespace pika::detail { const char* bad_lexical_cast::what() const noexcept { return "bad lexical cast: " @@ -17,11 +17,9 @@ namespace pika::util { bad_lexical_cast::~bad_lexical_cast() noexcept = default; - namespace detail { - void throw_bad_lexical_cast(std::type_info const& source_type, - std::type_info const& target_type) - { - throw bad_lexical_cast(source_type, target_type); - } - } // namespace detail -} // namespace pika::util + void throw_bad_lexical_cast( + std::type_info const& source_type, std::type_info const& target_type) + { + throw bad_lexical_cast(source_type, target_type); + } +} // namespace pika::detail diff --git a/libs/pika/string_util/tests/unit/string_split.cpp b/libs/pika/string_util/tests/unit/string_split.cpp index 2b67b8da8..70246eac6 100644 --- a/libs/pika/string_util/tests/unit/string_split.cpp +++ b/libs/pika/string_util/tests/unit/string_split.cpp @@ -24,8 +24,8 @@ int main() std::vector tokens; // split tests - pika::string_util::split(tokens, str2, pika::string_util::is_any_of("xX"), - pika::string_util::token_compress_mode::on); + pika::detail::split(tokens, str2, pika::detail::is_any_of("xX"), + pika::detail::token_compress_mode::on); PIKA_TEST_EQ(tokens.size(), std::size_t(4)); PIKA_TEST_EQ(tokens[0], std::string("")); @@ -33,8 +33,8 @@ int main() PIKA_TEST_EQ(tokens[2], std::string("-abb-")); PIKA_TEST_EQ(tokens[3], std::string("")); - pika::string_util::split(tokens, str2, pika::string_util::is_any_of("xX"), - pika::string_util::token_compress_mode::off); + pika::detail::split(tokens, str2, pika::detail::is_any_of("xX"), + pika::detail::token_compress_mode::off); PIKA_TEST_EQ(tokens.size(), std::size_t(7)); PIKA_TEST_EQ(tokens[0], std::string("")); @@ -45,16 +45,16 @@ int main() PIKA_TEST_EQ(tokens[5], std::string("")); PIKA_TEST_EQ(tokens[6], std::string("")); - pika::string_util::split(tokens, pch1, pika::string_util::is_any_of("x"), - pika::string_util::token_compress_mode::on); + pika::detail::split(tokens, pch1, pika::detail::is_any_of("x"), + pika::detail::token_compress_mode::on); PIKA_TEST_EQ(tokens.size(), std::size_t(3)); PIKA_TEST_EQ(tokens[0], std::string("")); PIKA_TEST_EQ(tokens[1], std::string("-abc--")); PIKA_TEST_EQ(tokens[2], std::string("-abb")); - pika::string_util::split(tokens, pch1, pika::string_util::is_any_of("x"), - pika::string_util::token_compress_mode::off); + pika::detail::split(tokens, pch1, pika::detail::is_any_of("x"), + pika::detail::token_compress_mode::off); PIKA_TEST_EQ(tokens.size(), std::size_t(5)); PIKA_TEST_EQ(tokens[0], std::string("")); @@ -63,47 +63,46 @@ int main() PIKA_TEST_EQ(tokens[3], std::string("")); PIKA_TEST_EQ(tokens[4], std::string("-abb")); - pika::string_util::split(tokens, str3, pika::string_util::is_any_of(","), - pika::string_util::token_compress_mode::on); + pika::detail::split(tokens, str3, pika::detail::is_any_of(","), + pika::detail::token_compress_mode::on); PIKA_TEST_EQ(tokens.size(), std::size_t(1)); PIKA_TEST_EQ(tokens[0], std::string("xx")); - pika::string_util::split(tokens, str3, pika::string_util::is_any_of(","), - pika::string_util::token_compress_mode::off); + pika::detail::split(tokens, str3, pika::detail::is_any_of(","), + pika::detail::token_compress_mode::off); - pika::string_util::split(tokens, str3, pika::string_util::is_any_of("xX"), - pika::string_util::token_compress_mode::on); + pika::detail::split(tokens, str3, pika::detail::is_any_of("xX"), + pika::detail::token_compress_mode::on); PIKA_TEST_EQ(tokens.size(), std::size_t(2)); PIKA_TEST_EQ(tokens[0], std::string("")); PIKA_TEST_EQ(tokens[1], std::string("")); - pika::string_util::split(tokens, str3, pika::string_util::is_any_of("xX"), - pika::string_util::token_compress_mode::off); + pika::detail::split(tokens, str3, pika::detail::is_any_of("xX"), + pika::detail::token_compress_mode::off); PIKA_TEST_EQ(tokens.size(), std::size_t(3)); PIKA_TEST_EQ(tokens[0], std::string("")); PIKA_TEST_EQ(tokens[1], std::string("")); PIKA_TEST_EQ(tokens[2], std::string("")); - split(tokens, strempty, pika::string_util::is_any_of(".:,;"), - pika::string_util::token_compress_mode::on); + split(tokens, strempty, pika::detail::is_any_of(".:,;"), + pika::detail::token_compress_mode::on); PIKA_TEST(tokens.size() == 1); PIKA_TEST(tokens[0] == std::string("")); - split(tokens, strempty, pika::string_util::is_any_of(".:,;"), - pika::string_util::token_compress_mode::off); + split(tokens, strempty, pika::detail::is_any_of(".:,;"), + pika::detail::token_compress_mode::off); PIKA_TEST(tokens.size() == 1); PIKA_TEST(tokens[0] == std::string("")); // If using a compiler that supports forwarding references, we should be // able to use rvalues, too - pika::string_util::split(tokens, std::string("Xx-abc--xX-abb-xx"), - pika::string_util::is_any_of("xX"), - pika::string_util::token_compress_mode::on); + pika::detail::split(tokens, std::string("Xx-abc--xX-abb-xx"), + pika::detail::is_any_of("xX"), pika::detail::token_compress_mode::on); PIKA_TEST_EQ(tokens.size(), std::size_t(4)); PIKA_TEST_EQ(tokens[0], std::string("")); @@ -111,9 +110,8 @@ int main() PIKA_TEST_EQ(tokens[2], std::string("-abb-")); PIKA_TEST_EQ(tokens[3], std::string("")); - pika::string_util::split(tokens, std::string("Xx-abc--xX-abb-xx"), - pika::string_util::is_any_of("xX"), - pika::string_util::token_compress_mode::off); + pika::detail::split(tokens, std::string("Xx-abc--xX-abb-xx"), + pika::detail::is_any_of("xX"), pika::detail::token_compress_mode::off); PIKA_TEST_EQ(tokens.size(), std::size_t(7)); PIKA_TEST_EQ(tokens[0], std::string("")); diff --git a/libs/pika/string_util/tests/unit/string_to_lower.cpp b/libs/pika/string_util/tests/unit/string_to_lower.cpp index 61af8049e..2aad1fa82 100644 --- a/libs/pika/string_util/tests/unit/string_to_lower.cpp +++ b/libs/pika/string_util/tests/unit/string_to_lower.cpp @@ -15,14 +15,14 @@ int main() std::string str1("AbCdEfG 123 xxxYYYzZzZ"); std::string str2(""); - pika::string_util::to_lower(str1); + pika::detail::to_lower(str1); PIKA_TEST(str1 == "abcdefg 123 xxxyyyzzzz"); // to_lower is idempotent - pika::string_util::to_lower(str1); + pika::detail::to_lower(str1); PIKA_TEST(str1 == "abcdefg 123 xxxyyyzzzz"); - pika::string_util::to_lower(str2); + pika::detail::to_lower(str2); PIKA_TEST(str2 == ""); return 0; diff --git a/libs/pika/string_util/tests/unit/string_trim.cpp b/libs/pika/string_util/tests/unit/string_trim.cpp index b6476ac7b..da38f4060 100644 --- a/libs/pika/string_util/tests/unit/string_trim.cpp +++ b/libs/pika/string_util/tests/unit/string_trim.cpp @@ -19,33 +19,33 @@ int main() std::string str5(" "); // general string test - PIKA_TEST_EQ(pika::string_util::trim_copy(str1), "1x x x x1"); - PIKA_TEST_EQ(pika::string_util::trim_copy(str3), "x"); - PIKA_TEST_EQ(pika::string_util::trim_copy(str4), "x"); + PIKA_TEST_EQ(pika::detail::trim_copy(str1), "1x x x x1"); + PIKA_TEST_EQ(pika::detail::trim_copy(str3), "x"); + PIKA_TEST_EQ(pika::detail::trim_copy(str4), "x"); // spaces-only string test - PIKA_TEST_EQ(pika::string_util::trim_copy(str5), ""); + PIKA_TEST_EQ(pika::detail::trim_copy(str5), ""); // empty string check - PIKA_TEST_EQ(pika::string_util::trim_copy(std::string("")), ""); + PIKA_TEST_EQ(pika::detail::trim_copy(std::string("")), ""); // general string test - pika::string_util::trim(str2); + pika::detail::trim(str2); PIKA_TEST_EQ(str2, "2x x x x2"); - pika::string_util::trim(str3); + pika::detail::trim(str3); PIKA_TEST_EQ(str3, "x"); - pika::string_util::trim(str4); + pika::detail::trim(str4); PIKA_TEST_EQ(str4, "x"); // spaces-only string test - pika::string_util::trim(str5); + pika::detail::trim(str5); PIKA_TEST_EQ(str5, ""); // empty string check str5 = ""; - pika::string_util::trim(str5); + pika::detail::trim(str5); PIKA_TEST_EQ(str5, ""); return 0; diff --git a/libs/pika/util/include/pika/util/get_entry_as.hpp b/libs/pika/util/include/pika/util/get_entry_as.hpp index f4b4766c1..3f222465f 100644 --- a/libs/pika/util/include/pika/util/get_entry_as.hpp +++ b/libs/pika/util/include/pika/util/get_entry_as.hpp @@ -24,7 +24,7 @@ namespace pika::detail { std::string const& entry = config.get_entry(key, ""); if (entry.empty()) return dflt; - return util::from_string(entry, dflt); + return detail::from_string(entry, dflt); } template ((*it).second, dflt); + return pika::detail::from_string((*it).second, dflt); return dflt; } diff --git a/tests/performance/local/timed_task_spawn.cpp b/tests/performance/local/timed_task_spawn.cpp index f81b2a68f..d6438163f 100644 --- a/tests/performance/local/timed_task_spawn.cpp +++ b/tests/performance/local/timed_task_spawn.cpp @@ -437,9 +437,9 @@ int pika_main(variables_map& vm) for (auto& raw_counter : raw_counters) { std::vector entry; - pika::string_util::split(entry, raw_counter, - pika::string_util::is_any_of(","), - pika::string_util::token_compress_mode::on); + pika::detail::split(entry, raw_counter, + pika::detail::is_any_of(","), + pika::detail::token_compress_mode::on); PIKA_TEST_EQ(entry.size(), 2);