/__w/rapidsmpf/rapidsmpf/cpp/include/rapidsmpf/utils/string.hpp

Parses a string into a value of type T.This function attempts to parse the given string into a value of the specified type T using a std::stringstream. If the parsing fails, an exception is thrown.

Template Parameters
TThe type to parse the string into. Must support extraction from std::istream via operator>>.
Parameters
textThe input string to parse.
Returns
T The parsed value of type T.
Exceptions
std::invalid_argumentIf the string cannot be parsed into the requested type.
Note
This function assumes that the input string contains a valid representation of type T, and that T has a suitable operator>> overload.

int i = parse_string<int>("42"); // i == 42 double d = parse_string<double>("3.14"); // d == 3.14

#pragma once
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <sstream>
#include <string>
#include <string_view>
#include <rapidsmpf/utils/misc.hpp>
namespace rapidsmpf {
std::string trim(std::string_view text);
std::string to_lower(std::string_view text);
std::string to_upper(std::string_view text);
enum class TrimZeroFraction {
NO,
YES,
};
std::string format_nbytes(
double nbytes,
int num_decimals = 2,
);
std::string format_duration(
double seconds,
int precision = 2,
);
std::int64_t parse_nbytes(std::string_view text);
std::size_t parse_nbytes_unsigned(std::string_view text);
std::size_t parse_nbytes_or_percent(std::string_view text, double total_bytes);
Duration parse_duration(std::string_view text);
template <typename T>
T parse_string(std::string const& text) {
std::stringstream sstream(text);
T ret;
sstream >> ret;
if (sstream.fail()) {
throw std::invalid_argument("cannot parse \"" + std::string{text} + "\"");
}
return ret;
}
template <>
bool parse_string(std::string const& text);
std::optional<std::string> parse_optional(std::string text);
std::vector<std::string> parse_string_list(std::string_view text, char delimiter = ',');
} // 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