language: C++ 4.7.2 (gcc-4.7.2)
date: 928 days 9 hours ago
link:
visibility: public
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* Copyright Richard Benjamin Voigt */
/* All rights reserved.             */
/* contact richardvoigt@gmail.com for a cleaned-up version under a BSD license */
 
 
#include <climits>
#include <string>
 
template<typename T>
struct assert_integral
{
    enum { value = (T)0.5 };
    char test[1 - 4 * value];
};
 
template<typename T, bool is_signed, size_t bits>
struct itostr_impl { };
 
template<typename T, bool is_signed>
struct itostr_impl<T,is_signed,8> 
{
    static std::string cvt(T val)
    {
        std::string retval(5, '\0');
        int i = 0;
        char ch = 0;
 
        if (is_signed) {
            if (val < 0) {
                retval[i] = '-';
                ++i;
                if (val <= -100) {
                    ch = '1';
                    val += 100;
                }
                val = -val;
            }
            else if (val >= 100) {
                ch |= '1';
                val -= 100;
            }
        }
        else {
            if (val >= 200) {
                ch |= '2';
                val -= 200;
            }
            else if (val >= 100) {
                ch |= '1';
                val -= 100;
            }
        }
        if (ch) {
            retval[i] = ch;
            ++i;
            ch = '0';
        }
 
        if (val >= 80) {
            ch |= '8';
            val -= 80;
        }
        else if (val >= 40) {
            ch |= '4';
            val -= 40;
        }
        if (val >= 20) {
            ch |= '2';
            val -= 20;
        }
        if (val >= 10) {
            ch |= '1';
            val -= 10;
        }
        if (ch) {
            retval[i] = ch;
            ++i;
        }
 
        retval[i] = '0' + val;
        retval.resize(i+1);
 
        return retval;
    }
};
 
template<typename T, bool is_signed>
struct itostr_impl<T,is_signed,16> 
{
    static std::string cvt(T val)
    {
        std::string retval(7, '\0');
        int i = 0;
        char ch = 0;
 
        if (is_signed) {
            if (val < 0) {
                retval[i] = '-';
                ++i;
                if (val <= -20000) {
                    ch = '2';
                    val += 20000;
                }
                val = -val;
            }
            else if (val >= 20000) {
                ch |= '2';
                val -= 20000;
            }
        }
        else {
            if (val >= 40000) {
                ch |= '4';
                val -= 40000;
            }
            else if (val >= 20000) {
                ch |= '2';
                val -= 20000;
            }
        }
        if (val >= 10000) {
            ch |= '1';
            val -= 10000;
        }
 
        if (ch) {
            retval[i] = ch;
            ++i;
            ch = '0';
        }
 
        if (val >= 8000) {
            ch |= '8';
            val -= 8000;
        }
        else if (val >= 4000) {
            ch |= '4';
            val -= 4000;
        }
        if (val >= 2000) {
            ch |= '2';
            val -= 2000;
        }
        if (val >= 1000) {
            ch |= '1';
            val -= 1000;
        }
        if (ch) {
            retval[i] = ch;
            ++i;
            ch = '0';
        }
 
        if (val >= 800) {
            ch |= '8';
            val -= 800;
        }
        else if (val >= 400) {
            ch |= '4';
            val -= 400;
        }
        if (val >= 200) {
            ch |= '2';
            val -= 200;
        }
        if (val >= 100) {
            ch |= '1';
            val -= 100;
        }
        if (ch) {
            retval[i] = ch;
            ++i;
            ch = '0';
        }
 
        if (val >= 80) {
            ch |= '8';
            val -= 80;
        }
        else if (val >= 40) {
            ch |= '4';
            val -= 40;
        }
        if (val >= 20) {
            ch |= '2';
            val -= 20;
        }
        if (val >= 10) {
            ch |= '1';
            val -= 10;
        }
        if (ch) {
            retval[i] = ch;
            ++i;
        }
 
        retval[i] = '0' + val;
        retval.resize(i+1);
 
        return retval;
    }
};
 
 
const char digit_pair_table[201] = {
    "00010203040506070809"
    "10111213141516171819"
    "20212223242526272829"
    "30313233343536373839"
    "40414243444546474849"
    "50515253545556575859"
    "60616263646566676869"
    "70717273747576777879"
    "80818283848586878889"
    "90919293949596979899"
};
 
template<typename T, bool is_signed>
struct itostr_impl<T,is_signed,32> 
{
    static std::string cvt(T val)
    {
        unsigned buf[11], ch = 0;
        unsigned* start = buf + 1;
        unsigned* p = start;
        bool neg = val < 0;
        unsigned digit;
 
        if (is_signed) {
            if (neg) {
                if (val <= -2000000000) {
                    ch = '2';
                    val += 2000000000;
                }
                val = -val;
            }
            else if (val >= 2000000000) {
                ch = '2';
                val -= 2000000000;
            }
        }
        else {
            if (val >= 4000000000U) {
                ch |= '4';
                val -= 4000000000U;
            }
            else if (val >= 2000000000) {
                ch |= '2';
                val -= 2000000000;
            }
        }
        if (val >= 1000000000) {
            ch |= '1';
            val -= 1000000000;
        }
 
        if (ch) {
            *p = ch;
            ++p;
            ch = '0';
        }
        else if (val < 1000) {
            if (val < 10) goto d1;
            if (val < 1000) goto d10;
        }
        else {
            if (val < 100000) goto d1000;
            if (val < 10000000) goto d100000;
        }
 
#define DO_PAIR(n) \
    d##n: \
        digit = val / n; \
        *(p++) = digit_pair_table[digit*2]; \
        *(p++) = digit_pair_table[digit*2+1]; \
        val -= n * digit;
 
        DO_PAIR(10000000);
        DO_PAIR(100000);
        DO_PAIR(1000);
        DO_PAIR(10);
 
d1:
        *p = '0' | val;
 
        if (p > start && *start == '0') ++start;
 
        if (is_signed && neg) *--start = '-';
 
        return std::string(start, p+1);
    }
};
 
template<typename T>
std::string itostr(T val)
{
    sizeof(assert_integral<T>);
    return itostr_impl<T,((T)-1) < 0,sizeof(T) * CHAR_BIT>::cvt(val);
}
 
#include <iostream>
 
int main(void)
{
    int i = 0;
    i += itostr(INT_MIN).size();
    i += itostr(INT_MAX).size();
 
    i += itostr(unsigned(INT_MAX) + 1U).size();
    i += itostr(unsigned(~0)).size(); 
 
    char c = CHAR_MIN;
    do {
        i += itostr(c).size();
                i += itostr((11111111 * c)).size();
 
                if (c >= CHAR_MAX) break;
 
        c++;
    } while (1);
 
    while (i < 100000000)
        i += itostr(i).size();
 
    std::cout << i << std::endl;
 
    return i;
}
prog.cpp: In function ‘std::string itostr(T) [with T = int]’:
prog.cpp:306:   instantiated from here
prog.cpp:297: warning: statement has no effect
prog.cpp: In function ‘std::string itostr(T) [with T = unsigned int]’:
prog.cpp:309:   instantiated from here
prog.cpp:297: warning: statement has no effect
prog.cpp: In function ‘std::string itostr(T) [with T = char]’:
prog.cpp:314:   instantiated from here
prog.cpp:297: warning: statement has no effect
prog.cpp: In static member function ‘static std::string itostr_impl<T, is_signed, 32u>::cvt(T) [with T = int, bool is_signed = true]’:
prog.cpp:298:   instantiated from ‘std::string itostr(T) [with T = int]’
prog.cpp:306:   instantiated from here
prog.cpp:243: warning: comparison between signed and unsigned integer expressions
prog.cpp:298:   instantiated from ‘std::string itostr(T) [with T = int]’
prog.cpp:306:   instantiated from here
prog.cpp:278: warning: label ‘d10000000’ defined but not used
prog.cpp: In static member function ‘static std::string itostr_impl<T, is_signed, 32u>::cvt(T) [with T = unsigned int, bool is_signed = false]’:
prog.cpp:298:   instantiated from ‘std::string itostr(T) [with T = unsigned int]’
prog.cpp:309:   instantiated from here
prog.cpp:231: warning: comparison between signed and unsigned integer expressions
prog.cpp:298:   instantiated from ‘std::string itostr(T) [with T = unsigned int]’
prog.cpp:309:   instantiated from here
prog.cpp:278: warning: label ‘d10000000’ defined but not used