summaryrefslogtreecommitdiff
path: root/libstdc++-v3/src/c++11/snprintf_lite.cc
blob: a700041aec1fb9641f6dc7b90f71d7c9b0af0390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Debugging support -*- C++ -*-

// Copyright (C) 2013-2015 Free Software Foundation, Inc.
//
// This file is part of GCC.
//
// GCC is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3, or (at your option)
// any later version.
//
// GCC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <http://www.gnu.org/licenses/>.

#include <stdarg.h>
#include <bits/functexcept.h>
#include <bits/locale_facets.h>

namespace std {
_GLIBCXX_BEGIN_NAMESPACE_VERSION
  template<typename _CharT, typename _ValueT>
  int
  __int_to_char(_CharT* __bufend, _ValueT __v, const _CharT* __lit,
                ios_base::fmtflags __flags, bool __dec);
_GLIBCXX_END_NAMESPACE_VERSION
}

namespace __gnu_cxx {

  // Private helper to throw logic error if snprintf_lite runs out
  // of space (which is not expected to ever happen).
  // NUL-terminates __buf.
  void
  __throw_insufficient_space(const char *__buf, const char *__bufend)
    __attribute__((__noreturn__));

  void
  __throw_insufficient_space(const char *__buf, const char *__bufend)
  {
    // Include space for trailing NUL.
    const size_t __len = __bufend - __buf + 1;

    const char __err[] = "not enough space for format expansion "
      "(Please submit full bug report at http://gcc.gnu.org/bugs.html):\n    ";
    const size_t __errlen = sizeof(__err) - 1;

    char *const __e
      = static_cast<char*>(__builtin_alloca(__errlen + __len));

    __builtin_memcpy(__e, __err, __errlen);
    __builtin_memcpy(__e + __errlen, __buf, __len - 1);
    __e[__errlen + __len - 1] = '\0';
    std::__throw_logic_error(__e);
  }


  // Private routine to append decimal representation of VAL to the given
  // BUFFER, but not more than BUFSIZE characters.
  // Does not NUL-terminate the output buffer.
  // Returns number of characters appended, or -1 if BUFSIZE is too small.
  int __concat_size_t(char *__buf, size_t __bufsize, size_t __val)
  {
    // __int_to_char is explicitly instantiated and available only for
    // some, but not all, types. See locale-inst.cc.
#ifdef _GLIBCXX_USE_LONG_LONG
    unsigned long long __val2 = __val;
#else
    unsigned long __val2 = __val;
#endif
    // Long enough for decimal representation.
    int __ilen = 3 * sizeof(__val2);
    char *__cs = static_cast<char*>(__builtin_alloca(__ilen));
    size_t __len = std::__int_to_char(__cs + __ilen, __val2,
				      std::__num_base::_S_atoms_out,
				      std::ios_base::dec, true);
    if (__bufsize < __len)
      return -1;

    __builtin_memcpy(__buf, __cs + __ilen - __len, __len);
    return __len;
  }


  // Private routine to print into __buf arguments according to format,
  // not to exceed __bufsize.
  // Only '%%', '%s' and '%zu' format specifiers are understood.
  // Returns number of characters printed (excluding terminating NUL).
  // Always NUL-terminates __buf.
  // Throws logic_error on insufficient space.
  int __snprintf_lite(char *__buf, size_t __bufsize, const char *__fmt,
		      va_list __ap)
  {
    char *__d = __buf;
    const char *__s = __fmt;
    const char *const __limit = __d + __bufsize - 1;  // Leave space for NUL.

    while (__s[0] != '\0' && __d < __limit)
      {
	if (__s[0] == '%')
	  switch (__s[1])
	    {
	    default:  // Stray '%'. Just print it.
	      break;
	    case '%':  // '%%'
	      __s += 1;
	      break;
	    case 's':  // '%s'.
	      {
		const char *__v = va_arg(__ap, const char *);

		while (__v[0] != '\0' && __d < __limit)
		  *__d++ = *__v++;

		if (__v[0] != '\0')
		  // Not enough space for __fmt expansion.
		  __throw_insufficient_space(__buf, __d);

		__s += 2;  // Step over %s.
		continue;
	      }
	      break;
	    case 'z':
	      if (__s[2] == 'u')  // '%zu' -- expand next size_t arg.
		{
		  const int __len = __concat_size_t(__d, __limit - __d,
						    va_arg(__ap, size_t));
		  if (__len > 0)
		    __d += __len;
		  else
		    // Not enough space for __fmt expansion.
		    __throw_insufficient_space(__buf, __d);

		  __s += 3;  // Step over %zu
		  continue;
		}
	      // Stray '%zX'. Just print it.
	      break;
	    }
	*__d++ = *__s++;
      }

    if (__s[0] != '\0')
      // Not enough space for __fmt expansion.
      __throw_insufficient_space(__buf, __d);

    *__d = '\0';
    return __d - __buf;
  }

}  // __gnu_cxx