I'm writing a c++ parser for a custom option file for an application. I have a loop that reads lines in the form of option=value
from a text file where value
must be converted to double
. In pseudocode it does the following:
while(not EOF) statement <- read_from_file useful_statement <- remove whitespaces, comments, etc from statement equal_position <- find '=' in useful_statement option_str <- useful_statement[0:equal_position) value_str <- useful_statement[equal_position:end) find_option(option_str) <- double(value_str)
To handle the string splitting and passing around to functions, I use std::string_view
because it avoids excessive copying and clearly states the intent of viewing segments of a pre-existing std::string
. I've done everything to the point where std::string_view value_str
points to the exact part of useful_statement
that contains the value I want to extract, but I can't figure out the way to read a double
from an std::string_view
.
I know of std::stod
which doesn't work with std::string_view
. It allows me to write
double value = std::stod(std::string(value_str));
However, this is ugly because it converts to a string which is not actually needed, and even though it will presumably not make a noticeable difference in my case, it could be too slow if one had to read a huge amount of numbers from a text file.
On the other hand, atof
won't work because I can't guarantee a null terminator. I could hack it by adding \0
to useful_statement
when constructing it, but that will make the code confusing to a reader and make it too easy to break if the code is altered/refactored.
So, what would be a clean, intuitive and reasonably efficient way to do this?
C++ string to float and double Conversion The easiest way to convert a string to a floating-point number is by using these C++11 functions: std::stof() - convert string to float. std::stod() - convert string to double. std::stold() - convert string to long double .
The std::string_view, from the C++17 standard, is a read-only non-owning reference to a char sequence. The motivation behind std::string_view is that it is quite common for functions to require a read-only reference to an std::string-like object where the exact type of the object does not matter.
By design, string_view is not a null-terminated type. There's no null byte after that 'g' .
One effective way to convert a string object into a numeral int is to use the stoi() function. This method is commonly used for newer versions of C++, with is being introduced with C++11. It takes as input a string value and returns as output the integer version of it.
Since you marked your question with C++1z, then that (theoretically) means you have access to from_chars
. It can handle your string-to-number conversion without needing anything more than a pair of const char*
s:
double dbl; auto result = from_chars(value_str.data(), value_str.data() + value_str.size(), dbl);
Of course, this requires that your standard library provide an implementation of from_chars
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With