Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert std::string_view to double?

Tags:

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?

like image 951
patatahooligan Avatar asked Aug 11 '17 14:08

patatahooligan


People also ask

How do I convert a string to a double in C++?

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 .

What is std::string_view for?

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.

Is string_view null terminated?

By design, string_view is not a null-terminated type. There's no null byte after that 'g' .

How do I convert a string to an int in C++?

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.


1 Answers

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.

like image 79
Nicol Bolas Avatar answered Sep 20 '22 18:09

Nicol Bolas