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.
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>
std::string
trim(std::string_view text);
std::string
to_lower(std::string_view text);
std::string
to_upper(std::string_view text);
};
double nbytes,
int num_decimals = 2,
);
double seconds,
int precision = 2,
);
template <typename T>
std::stringstream sstream(text);
T ret;
sstream >> ret;
if (sstream.fail()) {
throw std::invalid_argument("cannot parse \"" + std::string{text} + "\"");
}
return ret;
}
template <>
std::vector<std::string>
parse_string_list(std::string_view text,
char delimiter =
',');
}
RAPIDS Multi-Processor interfaces.
TrimZeroFraction
Control whether a zero fractional part is omitted when formatting values.
@ 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.
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.