string.hpp
1 
5 #pragma once
6 
7 #include <cmath>
8 #include <cstdint>
9 #include <cstdlib>
10 #include <sstream>
11 #include <string>
12 #include <string_view>
13 
14 #include <rapidsmpf/utils/misc.hpp>
15 
16 namespace rapidsmpf {
17 
24 std::string trim(std::string_view text);
25 
32 std::string to_lower(std::string_view text);
33 
40 std::string to_upper(std::string_view text);
41 
43 enum class TrimZeroFraction {
44  NO,
45  YES,
46 };
47 
73 std::string format_nbytes(
74  double nbytes,
75  int num_decimals = 2,
76  TrimZeroFraction trim_zero_fraction = TrimZeroFraction::YES
77 );
78 
102 std::string format_duration(
103  double seconds,
104  int precision = 2,
105  TrimZeroFraction trim_zero_fraction = TrimZeroFraction::YES
106 );
107 
134 std::int64_t parse_nbytes(std::string_view text);
135 
164 std::size_t parse_nbytes_unsigned(std::string_view text);
165 
189 std::size_t parse_nbytes_or_percent(std::string_view text, double total_bytes);
190 
219 Duration parse_duration(std::string_view text);
220 
241 template <typename T>
242 T parse_string(std::string const& text) {
243  std::stringstream sstream(text);
244  T ret;
245  sstream >> ret;
246  if (sstream.fail()) {
247  throw std::invalid_argument("cannot parse \"" + std::string{text} + "\"");
248  }
249  return ret;
250 }
251 
266 template <>
267 bool parse_string(std::string const& text);
268 
282 std::optional<std::string> parse_optional(std::string text);
283 
297 std::vector<std::string> parse_string_list(std::string_view text, char delimiter = ',');
298 
299 } // namespace rapidsmpf
RAPIDS Multi-Processor interfaces.
Definition: backend.hpp:13
TrimZeroFraction
Control whether a zero fractional part is omitted when formatting values.
Definition: string.hpp:43
@ YES
Omit the fractional part when it consists only of zeros.
@ NO
Always keep the fractional part.
std::optional< std::string > parse_optional(std::string text)
Parse an optional string value.
std::int64_t parse_nbytes(std::string_view text)
Parse a human-readable byte count into an integer number of bytes.
std::size_t parse_nbytes_or_percent(std::string_view text, double total_bytes)
Parse a byte quantity or percentage into an absolute byte count.
std::vector< std::string > parse_string_list(std::string_view text, char delimiter=',')
Parse a delimited string into a list of trimmed substrings.
std::chrono::duration< double > Duration
Alias for a duration type representing time in seconds as a double.
Definition: misc.hpp:33
std::string format_nbytes(double nbytes, int num_decimals=2, TrimZeroFraction trim_zero_fraction=TrimZeroFraction::YES)
Format a byte count as a human-readable string using IEC units.
std::string to_lower(std::string_view text)
Converts the specified string to lowercase.
std::size_t parse_nbytes_unsigned(std::string_view text)
Parse a human-readable byte count into a non-negative number of bytes.
std::string to_upper(std::string_view text)
Converts the specified string to uppercase.
Duration parse_duration(std::string_view text)
Parse a human-readable time duration into seconds.
std::string format_duration(double seconds, int precision=2, TrimZeroFraction trim_zero_fraction=TrimZeroFraction::YES)
Format a time duration as a human-readable string.
std::string trim(std::string_view text)
Trims whitespace from both ends of the specified string.
T parse_string(std::string const &text)
Specialization of parse_string for boolean values.
Definition: string.hpp:242