string_conversions.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // String Conversions -*- C++ -*-
  2. // Copyright (C) 2008-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file ext/string_conversions.h
  21. * This file is a GNU extension to the Standard C++ Library.
  22. */
  23. #ifndef _STRING_CONVERSIONS_H
  24. #define _STRING_CONVERSIONS_H 1
  25. #pragma GCC system_header
  26. #include <bits/requires_hosted.h> // GNU extensions are currently omitted
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <bits/c++config.h>
  31. #include <ext/numeric_traits.h>
  32. #include <bits/functexcept.h>
  33. #include <cstdlib>
  34. #include <cwchar>
  35. #include <cstdio>
  36. #include <cerrno>
  37. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  38. {
  39. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  40. // Helper for all the sto* functions.
  41. template<typename _TRet, typename _Ret = _TRet, typename _CharT,
  42. typename... _Base>
  43. _Ret
  44. __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...),
  45. const char* __name, const _CharT* __str, std::size_t* __idx,
  46. _Base... __base)
  47. {
  48. _Ret __ret;
  49. _CharT* __endptr;
  50. struct _Save_errno {
  51. _Save_errno() : _M_errno(errno) { errno = 0; }
  52. ~_Save_errno() { if (errno == 0) errno = _M_errno; }
  53. int _M_errno;
  54. } const __save_errno;
  55. struct _Range_chk {
  56. static bool
  57. _S_chk(_TRet, std::false_type) { return false; }
  58. static bool
  59. _S_chk(_TRet __val, std::true_type) // only called when _Ret is int
  60. {
  61. return __val < _TRet(__numeric_traits<int>::__min)
  62. || __val > _TRet(__numeric_traits<int>::__max);
  63. }
  64. };
  65. const _TRet __tmp = __convf(__str, &__endptr, __base...);
  66. if (__endptr == __str)
  67. std::__throw_invalid_argument(__name);
  68. else if (errno == ERANGE
  69. || _Range_chk::_S_chk(__tmp, std::is_same<_Ret, int>{}))
  70. std::__throw_out_of_range(__name);
  71. else
  72. __ret = __tmp;
  73. if (__idx)
  74. *__idx = __endptr - __str;
  75. return __ret;
  76. }
  77. // Helper for the to_string / to_wstring functions.
  78. template<typename _String, typename _CharT = typename _String::value_type>
  79. _String
  80. __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*,
  81. __builtin_va_list), std::size_t __n,
  82. const _CharT* __fmt, ...)
  83. {
  84. // XXX Eventually the result should be constructed in-place in
  85. // the __cxx11 string, likely with the help of internal hooks.
  86. _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  87. * __n));
  88. __builtin_va_list __args;
  89. __builtin_va_start(__args, __fmt);
  90. const int __len = __convf(__s, __n, __fmt, __args);
  91. __builtin_va_end(__args);
  92. return _String(__s, __s + __len);
  93. }
  94. _GLIBCXX_END_NAMESPACE_VERSION
  95. } // namespace
  96. #endif // C++11
  97. #endif // _STRING_CONVERSIONS_H