Date: Sat, 11 Jan 2025 15:43:59 +0000
On Sat, 11 Jan 2025, 13:55 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.
The original reason for requiring a std::string is that stoi etc are
defined in terms of strtoll which requires a null terminated string. A
string_view isn't null terminated.
The functions would need to be re-specified in terms of std::from_chars.
> 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.
>
> 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.
>
> 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
>
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.
The original reason for requiring a std::string is that stoi etc are
defined in terms of strtoll which requires a null terminated string. A
string_view isn't null terminated.
The functions would need to be re-specified in terms of std::from_chars.
> 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.
>
> 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.
>
> 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
>
Received on 2025-01-11 15:45:21