Date: Sat, 11 Jan 2025 16:29:57 +0200
Hello tzsz,
On Sat, Jan 11, 2025 at 3:55 PM tzsz via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Hello everyone,
>
> I'd like to suggest an addition to functions /stoi/, /stol/ and /stoll/
> defined in namespace /std/ in header /<string>/. As of right now these
> functions take either a /std::string const & /or a /std::wstring const
> &/ as their parameter. [1]
>
> Why:
> My current use case is that I need to parse data from a buffer.
> Currently my design for this is a component that manages the text buffer
> and just passed parts of it to smaller functions that do the actual
> processing. All those functions only take a /string_view/ as a parameter
> to keep it efficient. Some of these functions call others and only
> provide a small window into the original text. For this reason
> /std::sting const &/ is not an option here. Because one function has to
> convert a part of the string into a number, I need to
> write code like this:
>
> int myint = std::stoi(std::string{myview});
>
> This works but is not very elegant because I involves a heap allocation.
> I could also use the C versions that are used internally but then I'd
> lose the nice C++ features of exceptions for conversion failures.
>
Another variant for your current use case is to create a few lines wrapper
function around std::from_chars
<https://en.cppreference.com/w/cpp/utility/from_chars> which can use with
std::string_view (That's what we've done for our projects).
> To fix this I suggest the following addition to these functions:
>
> int stoi ( const std::string& str, std::size_t* pos = nullptr, int base
> = 10 ); // current
> int stoi ( const std::wstring& str, std::size_t* pos = nullptr, int base
> = 10 ); // current
>
> int stoi ( std::string_view& str, std::size_t* pos = nullptr, int base =
> 10 ); // new overload
> int stoi ( std::wstring_view& str, std::size_t* pos = nullptr, int base
> = 10 ); // new overload
>
> The last two functions do not need an additional /const/ for the first
> parameter as /string_view/s are always read-only.
>
> There remains one issues here, the /stoi/ functions are defined in
> /<string>/ but it does not include /<string_view>/ and/<string_view>/
> does not include /<string>/. I am not sure what the best way to settle
> this is. One way could be to split the overloads into two headers.
>
As a side note to your proposal the std::string has a conversion operator
<https://en.cppreference.com/w/cpp/string/basic_string/operator_basic_string_view>
to string_view and thus I think the <string> header should include the
<string_view> header.
And at least in GCC 14.2 it does this through the <bits/basic_string.h> as
far as I checked.
> I really hope this change could be included into the next standard.
>
> Thank you very much for reading
>
>
> [1] https://en.cppreference.com/w/cpp/string/basic_string/stol
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Regards,
Pavel.
On Sat, Jan 11, 2025 at 3:55 PM tzsz via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> Hello everyone,
>
> I'd like to suggest an addition to functions /stoi/, /stol/ and /stoll/
> defined in namespace /std/ in header /<string>/. As of right now these
> functions take either a /std::string const & /or a /std::wstring const
> &/ as their parameter. [1]
>
> Why:
> My current use case is that I need to parse data from a buffer.
> Currently my design for this is a component that manages the text buffer
> and just passed parts of it to smaller functions that do the actual
> processing. All those functions only take a /string_view/ as a parameter
> to keep it efficient. Some of these functions call others and only
> provide a small window into the original text. For this reason
> /std::sting const &/ is not an option here. Because one function has to
> convert a part of the string into a number, I need to
> write code like this:
>
> int myint = std::stoi(std::string{myview});
>
> This works but is not very elegant because I involves a heap allocation.
> I could also use the C versions that are used internally but then I'd
> lose the nice C++ features of exceptions for conversion failures.
>
Another variant for your current use case is to create a few lines wrapper
function around std::from_chars
<https://en.cppreference.com/w/cpp/utility/from_chars> which can use with
std::string_view (That's what we've done for our projects).
> To fix this I suggest the following addition to these functions:
>
> int stoi ( const std::string& str, std::size_t* pos = nullptr, int base
> = 10 ); // current
> int stoi ( const std::wstring& str, std::size_t* pos = nullptr, int base
> = 10 ); // current
>
> int stoi ( std::string_view& str, std::size_t* pos = nullptr, int base =
> 10 ); // new overload
> int stoi ( std::wstring_view& str, std::size_t* pos = nullptr, int base
> = 10 ); // new overload
>
> The last two functions do not need an additional /const/ for the first
> parameter as /string_view/s are always read-only.
>
> There remains one issues here, the /stoi/ functions are defined in
> /<string>/ but it does not include /<string_view>/ and/<string_view>/
> does not include /<string>/. I am not sure what the best way to settle
> this is. One way could be to split the overloads into two headers.
>
As a side note to your proposal the std::string has a conversion operator
<https://en.cppreference.com/w/cpp/string/basic_string/operator_basic_string_view>
to string_view and thus I think the <string> header should include the
<string_view> header.
And at least in GCC 14.2 it does this through the <bits/basic_string.h> as
far as I checked.
> I really hope this change could be included into the next standard.
>
> Thank you very much for reading
>
>
> [1] https://en.cppreference.com/w/cpp/string/basic_string/stol
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
Regards,
Pavel.
Received on 2025-01-11 14:30:13