I am trying to convert wstring_view to int. Is there something like stoi that works on wstring_view instead of string? Can't use any C-api's since it's not necessarily null-terminated. Can't use from_chars since it's wchar_t. So far, I've been converting to std::wstring, then converting to int (using stoi), which is probably OK with small string optimizations and all, but goes against the point of using views in the first place.
convert wstring_view to int
887 Views Asked by Aykhan Hagverdili AtThere are 2 best solutions below
On
std::stoi(std::wstring) is just a wrapper for std::wcstol(), which you can call directly. However, it requires a null-terminated wchar_t* string, but std::wstring_view is not guaranteed to be null-terminated, as you noted.
Since a 32bit int has a maximum of 10 decimal digits, plus an optional sign, plus the null-terminator, you could simply copy the std::wstring_view data into a local wchar_t[12] array, null-terminate it, and then convert that with std::wcstol(), std::swscanf(), etc.
Alternatively, you can create a custom FILE* stream that reads from the std::wstring_view data, such as via fmemopen() or equivalent, and then you can use std::fwscanf().
Alternatively, you can write a (or find a 3rd party) custom std::basic_streambuf-derived class that reads from the std::wstring_view, and then you can give an instance of that class to a standard std::istream object and use its operator>> to extract the int.
Here's what I found that works:
Since we specify the size explicitly in
format_str, view doesn't need to be null-terminated.If we have verified that the string only contains numeric characters (no sign character, no leading or trailing spaces, etc.) and it won't overflow, we can use a simpler unchecked routine:
I did some benchmarks. The
swscanfperforms much worse thanstoi. I will be usingstoiorUncheckedStrToIntdepending on if thewstring_viewis already verified.