Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

124 lines
3.5KB

  1. // String Conversions -*- C++ -*-
  2. // Copyright (C) 2008-2020 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. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <bits/c++config.h>
  30. #include <ext/numeric_traits.h>
  31. #include <bits/functexcept.h>
  32. #include <cstdlib>
  33. #include <cwchar>
  34. #include <cstdio>
  35. #include <cerrno>
  36. namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)
  37. {
  38. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  39. // Helper for all the sto* functions.
  40. template<typename _TRet, typename _Ret = _TRet, typename _CharT,
  41. typename... _Base>
  42. _Ret
  43. __stoa(_TRet (*__convf) (const _CharT*, _CharT**, _Base...),
  44. const char* __name, const _CharT* __str, std::size_t* __idx,
  45. _Base... __base)
  46. {
  47. _Ret __ret;
  48. _CharT* __endptr;
  49. struct _Save_errno {
  50. _Save_errno() : _M_errno(errno) { errno = 0; }
  51. ~_Save_errno() { if (errno == 0) errno = _M_errno; }
  52. int _M_errno;
  53. } const __save_errno;
  54. struct _Range_chk {
  55. static bool
  56. _S_chk(_TRet, std::false_type) { return false; }
  57. static bool
  58. _S_chk(_TRet __val, std::true_type) // only called when _Ret is int
  59. {
  60. return __val < _TRet(__numeric_traits<int>::__min)
  61. || __val > _TRet(__numeric_traits<int>::__max);
  62. }
  63. };
  64. const _TRet __tmp = __convf(__str, &__endptr, __base...);
  65. if (__endptr == __str)
  66. std::__throw_invalid_argument(__name);
  67. else if (errno == ERANGE
  68. || _Range_chk::_S_chk(__tmp, std::is_same<_Ret, int>{}))
  69. std::__throw_out_of_range(__name);
  70. else
  71. __ret = __tmp;
  72. if (__idx)
  73. *__idx = __endptr - __str;
  74. return __ret;
  75. }
  76. // Helper for the to_string / to_wstring functions.
  77. template<typename _String, typename _CharT = typename _String::value_type>
  78. _String
  79. __to_xstring(int (*__convf) (_CharT*, std::size_t, const _CharT*,
  80. __builtin_va_list), std::size_t __n,
  81. const _CharT* __fmt, ...)
  82. {
  83. // XXX Eventually the result should be constructed in-place in
  84. // the __cxx11 string, likely with the help of internal hooks.
  85. _CharT* __s = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
  86. * __n));
  87. __builtin_va_list __args;
  88. __builtin_va_start(__args, __fmt);
  89. const int __len = __convf(__s, __n, __fmt, __args);
  90. __builtin_va_end(__args);
  91. return _String(__s, __s + __len);
  92. }
  93. _GLIBCXX_END_NAMESPACE_VERSION
  94. } // namespace
  95. #endif // C++11
  96. #endif // _STRING_CONVERSIONS_H