From 1db83e3cdcc9fa74bda4c84e3a88bd8f329c4c2b Mon Sep 17 00:00:00 2001 From: David Wendt <45795991+davidwendt@users.noreply.github.com> Date: Thu, 19 May 2022 09:10:15 -0400 Subject: [PATCH] Change std::string parameters in cudf::strings APIs to std::string_view (#10832) Follow on to #10810. This changes other occurrences of parameter type `std::string` to `std::string_view` in the non-regex APIs. This covers `cudf::strings::pad` fill-char and the to/from-timestamp/duration converters that accept format specifiers. Authors: - David Wendt (https://github.com/davidwendt) Approvers: - Mike Wilson (https://github.com/hyperbolic2346) - Bradley Dice (https://github.com/bdice) URL: https://github.com/rapidsai/cudf/pull/10832 --- .../cudf/strings/convert/convert_datetime.hpp | 8 +++--- .../strings/convert/convert_durations.hpp | 6 ++-- .../cudf/strings/detail/converters.hpp | 26 ++++++++--------- cpp/include/cudf/strings/padding.hpp | 4 +-- cpp/src/strings/convert/convert_datetime.cu | 24 ++++++++-------- cpp/src/strings/convert/convert_durations.cu | 28 +++++++++---------- cpp/src/strings/padding.cu | 6 ++-- 7 files changed, 51 insertions(+), 51 deletions(-) diff --git a/cpp/include/cudf/strings/convert/convert_datetime.hpp b/cpp/include/cudf/strings/convert/convert_datetime.hpp index 4abca96e32a..3c3e40a1f0e 100644 --- a/cpp/include/cudf/strings/convert/convert_datetime.hpp +++ b/cpp/include/cudf/strings/convert/convert_datetime.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2021, NVIDIA CORPORATION. + * Copyright (c) 2019-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -74,7 +74,7 @@ namespace strings { std::unique_ptr to_timestamps( strings_column_view const& strings, data_type timestamp_type, - std::string const& format, + std::string_view format, rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); /** @@ -115,7 +115,7 @@ std::unique_ptr to_timestamps( */ std::unique_ptr is_timestamp( strings_column_view const& strings, - std::string const& format, + std::string_view format, rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); /** @@ -225,7 +225,7 @@ std::unique_ptr is_timestamp( */ std::unique_ptr from_timestamps( column_view const& timestamps, - std::string const& format = "%Y-%m-%dT%H:%M:%SZ", + std::string_view format = "%Y-%m-%dT%H:%M:%SZ", strings_column_view const& names = strings_column_view(column_view{ data_type{type_id::STRING}, 0, nullptr}), rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); diff --git a/cpp/include/cudf/strings/convert/convert_durations.hpp b/cpp/include/cudf/strings/convert/convert_durations.hpp index 03c005a2358..ac96a2c2fc6 100644 --- a/cpp/include/cudf/strings/convert/convert_durations.hpp +++ b/cpp/include/cudf/strings/convert/convert_durations.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, NVIDIA CORPORATION. + * Copyright (c) 2020-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -72,7 +72,7 @@ namespace strings { std::unique_ptr to_durations( strings_column_view const& strings, data_type duration_type, - std::string const& format, + std::string_view format, rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); /** @@ -121,7 +121,7 @@ std::unique_ptr to_durations( */ std::unique_ptr from_durations( column_view const& durations, - std::string const& format = "%D days %H:%M:%S", + std::string_view format = "%D days %H:%M:%S", rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); /** @} */ // end of doxygen group diff --git a/cpp/include/cudf/strings/detail/converters.hpp b/cpp/include/cudf/strings/detail/converters.hpp index b44276fcc33..3337815342c 100644 --- a/cpp/include/cudf/strings/detail/converters.hpp +++ b/cpp/include/cudf/strings/detail/converters.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021, NVIDIA CORPORATION. + * Copyright (c) 2020-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -87,49 +87,49 @@ std::unique_ptr from_booleans(column_view const& booleans, rmm::mr::device_memory_resource* mr); /** - * @copydoc to_timestamps(strings_column_view const&,data_type,std::string - * const&,rmm::mr::device_memory_resource*) + * @copydoc to_timestamps(strings_column_view const&,data_type,std::string_view, + * rmm::mr::device_memory_resource*) * * @param stream CUDA stream used for device memory operations and kernel launches. */ std::unique_ptr to_timestamps(strings_column_view const& strings, data_type timestamp_type, - std::string const& format, + std::string_view format, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr); /** - * @copydoc from_timestamps(strings_column_view const&,std::string - * const&,strings_column_view const&,rmm::mr::device_memory_resource*) + * @copydoc from_timestamps(strings_column_view const&,std::string_view, + * strings_column_view const&,rmm::mr::device_memory_resource*) * * @param stream CUDA stream used for device memory operations and kernel launches. */ std::unique_ptr from_timestamps(column_view const& timestamps, - std::string const& format, + std::string_view format, strings_column_view const& names, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr); /** - * @copydoc to_durations(strings_column_view const&,data_type,std::string - * const&,rmm::mr::device_memory_resource*) + * @copydoc to_durations(strings_column_view const&,data_type,std::string_view, + * rmm::mr::device_memory_resource*) * * @param stream CUDA stream used for device memory operations and kernel launches. */ std::unique_ptr to_durations(strings_column_view const& strings, data_type duration_type, - std::string const& format, + std::string_view format, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr); /** - * @copydoc from_durations(strings_column_view const&,std::string - * const&,rmm::mr::device_memory_resource*) + * @copydoc from_durations(strings_column_view const&,std::string_view. + * rmm::mr::device_memory_resource*) * * @param stream CUDA stream used for device memory operations and kernel launches. */ std::unique_ptr from_durations(column_view const& durations, - std::string const& format, + std::string_view format, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr); diff --git a/cpp/include/cudf/strings/padding.hpp b/cpp/include/cudf/strings/padding.hpp index dcb7f29dd82..754e828fae0 100644 --- a/cpp/include/cudf/strings/padding.hpp +++ b/cpp/include/cudf/strings/padding.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019-2020, NVIDIA CORPORATION. + * Copyright (c) 2019-2022, NVIDIA CORPORATION. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,7 +65,7 @@ std::unique_ptr pad( strings_column_view const& strings, size_type width, pad_side side = cudf::strings::pad_side::RIGHT, - std::string const& fill_char = " ", + std::string_view fill_char = " ", rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()); /** diff --git a/cpp/src/strings/convert/convert_datetime.cu b/cpp/src/strings/convert/convert_datetime.cu index 9473bed963e..32ca2c44279 100644 --- a/cpp/src/strings/convert/convert_datetime.cu +++ b/cpp/src/strings/convert/convert_datetime.cu @@ -102,7 +102,7 @@ struct alignas(4) format_item { using specifier_map = std::map; struct format_compiler { - std::string const format; + std::string_view const format; rmm::device_uvector d_items; // clang-format off @@ -113,15 +113,15 @@ struct format_compiler { {'S', 2}, {'f', 6}, {'z', 5}, {'Z', 3}, {'p', 2}, {'j', 3}}; // clang-format on - format_compiler(std::string fmt, + format_compiler(std::string_view fmt, rmm::cuda_stream_view stream, specifier_map extra_specifiers = {}) : format(fmt), d_items(0, stream) { specifiers.insert(extra_specifiers.begin(), extra_specifiers.end()); std::vector items; - const char* str = format.c_str(); - auto length = format.length(); + auto str = format.data(); + auto length = format.length(); while (length > 0) { char ch = *str++; length--; @@ -362,7 +362,7 @@ struct parse_datetime { struct dispatch_to_timestamps_fn { template ()>* = nullptr> void operator()(column_device_view const& d_strings, - std::string const& format, + std::string_view format, mutable_column_view& results_view, rmm::cuda_stream_view stream) const { @@ -376,7 +376,7 @@ struct dispatch_to_timestamps_fn { } template ()>* = nullptr> void operator()(column_device_view const&, - std::string const&, + std::string_view, mutable_column_view&, rmm::cuda_stream_view) const { @@ -389,7 +389,7 @@ struct dispatch_to_timestamps_fn { // std::unique_ptr to_timestamps(strings_column_view const& input, data_type timestamp_type, - std::string const& format, + std::string_view format, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr) { @@ -612,7 +612,7 @@ struct check_datetime_format { }; std::unique_ptr is_timestamp(strings_column_view const& input, - std::string const& format, + std::string_view const& format, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr) { @@ -648,7 +648,7 @@ std::unique_ptr is_timestamp(strings_column_view const& input, std::unique_ptr to_timestamps(strings_column_view const& input, data_type timestamp_type, - std::string const& format, + std::string_view format, rmm::mr::device_memory_resource* mr) { CUDF_FUNC_RANGE(); @@ -656,7 +656,7 @@ std::unique_ptr to_timestamps(strings_column_view const& input, } std::unique_ptr is_timestamp(strings_column_view const& input, - std::string const& format, + std::string_view format, rmm::mr::device_memory_resource* mr) { CUDF_FUNC_RANGE(); @@ -1100,7 +1100,7 @@ struct dispatch_from_timestamps_fn { // std::unique_ptr from_timestamps(column_view const& timestamps, - std::string const& format, + std::string_view format, strings_column_view const& names, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr) @@ -1143,7 +1143,7 @@ std::unique_ptr from_timestamps(column_view const& timestamps, // external API std::unique_ptr from_timestamps(column_view const& timestamps, - std::string const& format, + std::string_view format, strings_column_view const& names, rmm::mr::device_memory_resource* mr) { diff --git a/cpp/src/strings/convert/convert_durations.cu b/cpp/src/strings/convert/convert_durations.cu index 1a423ef8eec..093f6fee2fd 100644 --- a/cpp/src/strings/convert/convert_durations.cu +++ b/cpp/src/strings/convert/convert_durations.cu @@ -84,10 +84,10 @@ struct alignas(4) format_item { * components and when formatting a string from duration components. */ struct format_compiler { - std::string format; + std::string_view const format; rmm::device_uvector d_items; - format_compiler(const char* format_, rmm::cuda_stream_view stream) - : format(format_), d_items(0, stream) + format_compiler(std::string_view format, rmm::cuda_stream_view stream) + : format(format), d_items(0, stream) { static std::map const specifier_lengths = { {'-', -1}, // '-' if negative @@ -102,8 +102,8 @@ struct format_compiler { {'r', 11} // HH:MM:SS AM/PM }; std::vector items; - const char* str = format.c_str(); - auto length = format.length(); + auto str = format.data(); + auto length = format.length(); bool negative_sign{true}; while (length > 0) { char ch = *str++; @@ -407,13 +407,13 @@ struct duration_to_string_fn : public duration_to_string_size_fn { struct dispatch_from_durations_fn { template ()>* = nullptr> std::unique_ptr operator()(column_view const& durations, - std::string const& format, + std::string_view format, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr) const { CUDF_EXPECTS(!format.empty(), "Format parameter must not be empty."); - format_compiler compiler(format.c_str(), stream); + format_compiler compiler(format, stream); auto d_format_items = compiler.compiled_format_items(); size_type strings_count = durations.size(); @@ -675,11 +675,11 @@ struct parse_duration { struct dispatch_to_durations_fn { template ()>* = nullptr> void operator()(column_device_view const& d_strings, - std::string const& format, + std::string_view format, mutable_column_view& results_view, rmm::cuda_stream_view stream) const { - format_compiler compiler(format.c_str(), stream); + format_compiler compiler(format, stream); auto d_items = compiler.compiled_format_items(); auto d_results = results_view.data(); parse_duration pfn{d_strings, d_items, compiler.items_count()}; @@ -691,7 +691,7 @@ struct dispatch_to_durations_fn { } template ()>* = nullptr> void operator()(column_device_view const&, - std::string const&, + std::string_view, mutable_column_view&, rmm::cuda_stream_view) const { @@ -702,7 +702,7 @@ struct dispatch_to_durations_fn { } // namespace std::unique_ptr from_durations(column_view const& durations, - std::string const& format, + std::string_view format, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr) { @@ -715,7 +715,7 @@ std::unique_ptr from_durations(column_view const& durations, std::unique_ptr to_durations(strings_column_view const& strings, data_type duration_type, - std::string const& format, + std::string_view format, rmm::cuda_stream_view stream, rmm::mr::device_memory_resource* mr) { @@ -743,7 +743,7 @@ std::unique_ptr to_durations(strings_column_view const& strings, } // namespace detail std::unique_ptr from_durations(column_view const& durations, - std::string const& format, + std::string_view format, rmm::mr::device_memory_resource* mr) { CUDF_FUNC_RANGE(); @@ -752,7 +752,7 @@ std::unique_ptr from_durations(column_view const& durations, std::unique_ptr to_durations(strings_column_view const& strings, data_type duration_type, - std::string const& format, + std::string_view format, rmm::mr::device_memory_resource* mr) { CUDF_FUNC_RANGE(); diff --git a/cpp/src/strings/padding.cu b/cpp/src/strings/padding.cu index 435125bfd5b..2565c41675a 100644 --- a/cpp/src/strings/padding.cu +++ b/cpp/src/strings/padding.cu @@ -60,7 +60,7 @@ std::unique_ptr pad( strings_column_view const& strings, size_type width, pad_side side = pad_side::RIGHT, - std::string const& fill_char = " ", + std::string_view fill_char = " ", rmm::cuda_stream_view stream = rmm::cuda_stream_default, rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource()) { @@ -68,7 +68,7 @@ std::unique_ptr pad( if (strings_count == 0) return make_empty_column(type_id::STRING); CUDF_EXPECTS(!fill_char.empty(), "fill_char parameter must not be empty"); char_utf8 d_fill_char = 0; - size_type fill_char_size = to_char_utf8(fill_char.c_str(), d_fill_char); + size_type fill_char_size = to_char_utf8(fill_char.data(), d_fill_char); auto strings_column = column_device_view::create(strings.parent(), stream); auto d_strings = *strings_column; @@ -206,7 +206,7 @@ std::unique_ptr zfill( std::unique_ptr pad(strings_column_view const& strings, size_type width, pad_side side, - std::string const& fill_char, + std::string_view fill_char, rmm::mr::device_memory_resource* mr) { CUDF_FUNC_RANGE();