remove \r
[extl.git] / extl / conversion / itos.h
blobd0f1cb42771ba98f508311678855e0c664bed826
1 /* ///////////////////////////////////////////////////////////////////////
2 * File: itos.h
4 * Created: 08.03.03
5 * Updated: 08.05.09
7 * Brief: integer ==> string
9 * [<Home>]
10 * Copyright (c) 2008-2020, Waruqi All rights reserved.
11 * //////////////////////////////////////////////////////////////////// */
12 #ifndef EXTL_CONVERSION_ITOS_H
13 #define EXTL_CONVERSION_ITOS_H
15 /*!\file itos.h
16 * \brief integer ==> string
18 * - itos<>
19 * - Automatic symbol identification
20 * - Automatic type identification
21 * - support 64-bit integer conversion
22 * - support static array size determination
23 * - Multi-band conversion ==> Default: 10
24 * - itos<10> or itos Decimal(Default)
25 * - itos<16> Hexadecimal
26 * - itos<2> Binary
28 * - uitos/sitos
29 * - Automatic type identification
30 * - support 64-bit integer conversion
31 * - support static array size determination
32 * - Multi-band conversion ==> Default: 10
33 * - Note: needs provide conversion buffer
35 * - i2s<> shim
36 * - Note: It's rather dangerous because the life cycle of it's buffer is so short
37 * - eg:
38 * - e_char_t *p = i2s<e_char_t>(0);
39 * - printf(p); <== Undefined!
40 * - printf(i2s<e_char_t>(0)); <== Safe!
42 * Compatibility:
43 * - WATCOM: Only support uitos(),sitos()
44 * - VC6.0: Only support itos()
45 * - VECTORC: When compiling i2s<> in unicode character-set will link failure
48 #ifndef __cplusplus
49 # error itos.h need be supported by c++.
50 #endif
52 /* ///////////////////////////////////////////////////////////////////////
53 * Includes
56 #include "prefix.h"
57 #include "../type/traits/limit_traits.h"
58 #include "../type/traits/is_signed.h"
59 #include "../type/traits/is_unsigned.h"
60 #include "../memory/stack_buffer.h"
62 #ifdef EXTL_CONVERSION_ITOS_TEST_ENABLE
63 # include "../counter/counter.h"
64 # ifdef EXTL_COMPILER_IS_GCC
65 # define ultoa_test _ultot
66 # else
67 # define ultoa_test _ultot
68 # endif
69 # define ltoa_test _ltot
70 #endif
72 /* ///////////////////////////////////////////////////////////////////////
73 * Compatibility
76 /* itos<N>() support */
77 #ifdef EXTL_CONVERSION_ITOS_WITH_PARAM_SUPPORT
78 # undef EXTL_CONVERSION_ITOS_WITH_PARAM_SUPPORT
79 #endif
81 #if !defined(EXTL_TEMPLATE_TYPE_REQUIRED_IN_ARGLIST) && \
82 defined(EXTL_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS_SUPPORT)
83 # define EXTL_CONVERSION_ITOS_WITH_PARAM_SUPPORT
84 #endif
86 /* The maximum number of the string buffer */
87 #define EXTL_ITOS_MAX_CHAR_NUM (64 + 1)
89 /* ///////////////////////////////////////////////////////////////////////
90 * ::extl::detail namespace
92 EXTL_BEGIN_NAMESPACE
93 EXTL_DETAIL_BEGIN_NAMESPACE
95 /* Search Table */
96 template < typename_param_k C >
97 #ifdef EXTL_TEMPLATE_TYPE_REQUIRED_IN_ARGLIST /* WATCOM */
98 inline C const* get_digit_char(C*)
99 #else
100 inline C const* get_digit_char()
101 #endif
103 static C const s_char_table[31] =
105 'F', 'E', 'D', 'C', 'B', 'A', '9', '8', '7', '6', '5', '4', '3', '2', '1' /* Negative integer */
106 , '0'
107 , '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' /* Positive integer */
110 return s_char_table + 15;
113 /* ///////////////////////////////////////////////////////////////////////
114 * unsigned integer conversion implementaion
116 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
117 template < typename_param_k I, typename_param_k C, e_size_t N = 10 >
118 #else
119 template < typename_param_k I, typename_param_k C, e_size_t N >
120 #endif
121 struct uitos_impl
123 static inline C const* convert(I num, C* buf, e_size_t const& n)
125 EXTL_MUST_BE_UNSIGNED(I);
127 EXTL_ASSERT(NULL != buf);
128 if(NULL == buf) return NULL;
130 EXTL_ASSERT(n >= 2);
131 if(n < 2)
133 buf[0] = static_cast<C>('\0');
134 return buf;
137 /* convert from the tail */
138 C* p = buf + n - 1;
139 *p = 0;
143 I index = static_cast<I>(num % static_cast<I>(N));
144 num = static_cast<I>(num / static_cast<I>(N));
146 --p;
148 #ifdef EXTL_COMPILER_IS_WATCOM
149 *p = get_digit_char(static_cast<C*>(NULL))[index];
150 #else
151 # ifdef EXTL_TEMPLATE_TYPE_REQUIRED_IN_ARGLIST
152 *p = get_digit_char<C>(static_cast<C*>(NULL))[index];
153 # else
154 *p = get_digit_char<C>()[index];
155 # endif
156 #endif
159 while (num != 0);
161 EXTL_ASSERT((p >= buf));
163 return p;
166 static inline C const* convert(I num, C* buf, e_size_t const& n, e_size_t& result_count)
168 C const* p = convert(num, buf, n);
169 result_count = n - (p - buf + 1);
170 return p;
174 /* ///////////////////////////////////////////////////////////////////////
175 * signed integer conversion implementaion
177 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
178 template < typename_param_k I, typename_param_k C, e_size_t N = 10 >
179 #else
180 template < typename_param_k I, typename_param_k C, e_size_t N >
181 #endif
182 struct sitos_impl
184 static inline C const* convert(I num, C* buf, e_size_t const& n)
186 EXTL_MUST_BE_SIGNED(I);
187 EXTL_ASSERT(NULL != buf);
188 if(NULL == buf) return NULL;
190 C* p = buf + n - 1;
191 *p = 0;
193 if((num < 0) && (N == 10)) /* Negative integer */
195 EXTL_ASSERT(n >= 3);
196 if(n < 3)
198 buf[0] = static_cast<C>('\0');
199 return buf;
204 I index = static_cast<I>(num % static_cast<I>(N));
205 num = static_cast<I>(num / static_cast<I>(N));
207 --p;
209 #ifdef EXTL_COMPILER_IS_WATCOM
210 *p = get_digit_char(static_cast<C*>(NULL))[index];
211 #else
212 # ifdef EXTL_TEMPLATE_TYPE_REQUIRED_IN_ARGLIST
213 *p = get_digit_char<C>(static_cast<C*>(NULL))[index];
214 # else
215 *p = get_digit_char<C>()[index];
216 # endif
217 #endif
220 while (num != 0);
222 *(--p) = C('-');
225 else /* Positive integer */
227 EXTL_ASSERT(n >= 2);
228 if(n < 2)
230 buf[0] = static_cast<C>('\0');
231 return buf;
236 I index = static_cast<I>(num % static_cast<I>(N));
237 num = static_cast<I>(num / static_cast<I>(N));
239 --p;
241 #ifdef EXTL_COMPILER_IS_WATCOM
242 *p = get_digit_char(static_cast<C*>(NULL))[index];
243 #else
244 # ifdef EXTL_TEMPLATE_TYPE_REQUIRED_IN_ARGLIST
245 *p = get_digit_char<C>(static_cast<C*>(NULL))[index];
246 # else
247 *p = get_digit_char<C>()[index];
248 # endif
249 #endif
252 while (num != 0);
255 EXTL_ASSERT((p >= buf));
257 return p;
260 static inline C const* convert(I num, C* buf, e_size_t const& n, e_size_t& result_count)
262 C const* p = convert(num, buf, n);
263 result_count = n - (p - buf + 1);
264 return p;
268 /* ///////////////////////////////////////////////////////////////////////
269 * itos<N>() implementation
270 * //////////////////////////////////////////////////////////////////// */
271 #ifndef EXTL_TEMPLATE_TYPE_REQUIRED_IN_ARGLIST
272 /* //////////////////////////////////////////////////////////////////// */
273 #ifdef EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT
274 # ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
275 template < e_bool_t is_unsigned, e_bool_t is_signed, typename_param_k I, typename_param_k C, e_size_t N = 10 >
276 struct sign_selector;
277 # else
278 template < e_bool_t is_unsigned, e_bool_t is_signed, typename_param_k I, typename_param_k C, e_size_t N >
279 struct sign_selector;
280 # endif
281 #else
282 template < e_bool_t is_unsigned, e_bool_t is_signed >
283 struct sign_selector;
284 #endif
286 /* Select unsigned integer conversion */
287 #ifdef EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT
288 template < typename_param_k I, typename_param_k C, e_size_t N >
289 struct sign_selector< e_true_v, e_false_v, I, C, N >
291 #else
292 EXTL_TEMPLATE_SPECIALISATION
293 struct sign_selector< e_true_v, e_false_v >
295 # ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
296 template < typename_param_k I, typename_param_k C, e_size_t N = 10 >
297 struct itos_impl
299 # else
300 template < typename_param_k I, typename_param_k C, e_size_t N >
301 struct itos_impl
303 # endif
304 #endif
305 static inline C const* convert(I num, C* buf, e_size_t const& n)
307 return uitos_impl<I, C, N>::convert(num, buf, n);
310 static inline C const* convert(I num, C* buf, e_size_t const& n, e_size_t& result_count)
312 C const* p = convert(num, buf, n);
313 result_count = n - (p - buf + 1);
314 return p;
317 #ifndef EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT
319 #endif
322 /* Select signed integer conversion */
323 #ifdef EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT
324 template < typename_param_k I, typename_param_k C, e_size_t N >
325 struct sign_selector< e_false_v, e_true_v, I, C, N >
327 #else
328 EXTL_TEMPLATE_SPECIALISATION
329 struct sign_selector< e_false_v, e_true_v >
331 # ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
332 template < typename_param_k I, typename_param_k C, e_size_t N = 10 >
333 struct itos_impl
335 # else
336 template < typename_param_k I, typename_param_k C, e_size_t N >
337 struct itos_impl
339 # endif
340 #endif
341 static inline C const* convert(I num, C* buf, e_size_t const& n)
343 return sitos_impl<I, C, N>::convert(num, buf, n);
346 static inline C const* convert(I num, C* buf, e_size_t const& n, e_size_t& result_count)
348 C const* p = convert(num, buf, n);
349 result_count = n - (p - buf + 1);
350 return p;
353 #ifndef EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT
355 #endif
357 #endif /* EXTL_TEMPLATE_TYPE_REQUIRED_IN_ARGLIST */
358 /* //////////////////////////////////////////////////////////////////// */
359 EXTL_DETAIL_END_NAMESPACE /* ::extl::detail namespace */
360 /* //////////////////////////////////////////////////////////////////// */
362 #if defined(EXTL_CONVERSION_ITOS_WITH_PARAM_SUPPORT)
364 /// uitos<N>()
365 template < e_size_t N, typename_param_k I, typename_param_k C >
366 inline C const* uitos(I num, C* buf, e_size_t const& n)
368 return EXTL_NS_DETAIL(uitos_impl)<I, C, N>::convert(num, buf, n);
370 /// uitos<N>()
371 template < e_size_t N, typename_param_k I, typename_param_k C >
372 inline C const* uitos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
374 return EXTL_NS_DETAIL(uitos_impl)<I, C, N>::convert(num, buf, n, result_count);
376 /// sitos<N>()
377 template < e_size_t N, typename_param_k I, typename_param_k C >
378 inline C const* sitos(I num, C* buf, e_size_t const& n)
380 return EXTL_NS_DETAIL(sitos_impl)<I, C, N>::convert(num, buf, n);
382 /// sitos<N>()
383 template < e_size_t N, typename_param_k I, typename_param_k C >
384 inline C const* sitos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
386 return EXTL_NS_DETAIL(sitos_impl)<I, C, N>::convert(num, buf, n, result_count);
389 /// itos<N>()
390 template < e_size_t N, typename_param_k I, typename_param_k C >
391 inline C const* itos(I num, C* buf, e_size_t const& n)
393 # ifdef EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT
394 return EXTL_NS_DETAIL(sign_selector)
396 (is_unsigned<I>::value),
397 (is_signed<I>::value),
398 I, C, N
399 >::convert(num, buf, n);
400 # else
401 return EXTL_NS_DETAIL(sign_selector)
403 (is_unsigned<I>::value),
404 (is_signed<I>::value)
405 >::template_qual_k itos_impl<I, C, N>::convert(num, buf, n);
406 # endif
408 /// itos<N>()
409 template < e_size_t N, typename_param_k I, typename_param_k C >
410 inline C const* itos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
412 C const* p = itos<N, I, C>(num, buf, n);
413 result_count = n - (p - buf + 1);
414 return p;
416 /* ///////////////////////////////////////////////////////////////////////
417 * Default: Decimal integer conversion
420 /// uitos
421 template < typename_param_k I, typename_param_k C >
422 inline C const* uitos(I num, C* buf, e_size_t const& n)
424 return EXTL_NS_DETAIL(uitos_impl)<I, C>::convert(num, buf, n);
426 /// uitos
427 template < typename_param_k I, typename_param_k C >
428 inline C const* uitos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
430 return EXTL_NS_DETAIL(uitos_impl)<I, C>::convert(num, buf, n, result_count);
433 /// sitos
434 template < typename_param_k I, typename_param_k C >
435 inline C const* sitos(I num, C* buf, e_size_t const& n)
437 return EXTL_NS_DETAIL(sitos_impl)<I, C>::convert(num, buf, n);
439 /// sitos
440 template < typename_param_k I, typename_param_k C >
441 inline C const* sitos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
443 return EXTL_NS_DETAIL(sitos_impl)<I, C>::convert(num, buf, n, result_count);
446 /// itos
447 template < typename_param_k I, typename_param_k C >
448 inline C const* itos(I num, C* buf, e_size_t const& n)
450 return itos<10, I, C>(num, buf, n);
452 /// itos
453 template < typename_param_k I, typename_param_k C >
454 inline C const* itos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
456 return itos<10, I, C>(num, buf, n, result_count);
458 /* ///////////////////////////////////////////////////////////////////////
459 * static array size determination
461 #ifdef EXTL_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
463 /// utos
464 template < e_size_t N, typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
465 inline C const* uitos(I num, C (&buf)[BUF_COUNT])
467 return EXTL_NS_DETAIL(uitos_impl)<I, C, N>::convert(num, buf, BUF_COUNT);
469 /// utos
470 template < e_size_t N, typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
471 inline C const* uitos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
473 return EXTL_NS_DETAIL(uitos_impl)<I, C, N>::convert(num, buf, BUF_COUNT, result_count);
475 /// sitos
476 template < e_size_t N, typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
477 inline C const* sitos(I num, C (&buf)[BUF_COUNT])
479 return EXTL_NS_DETAIL(sitos_impl)<I, C, N>::convert(num, buf, BUF_COUNT);
481 /// sitos
482 template < e_size_t N, typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
483 inline C const* sitos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
485 return EXTL_NS_DETAIL(sitos_impl)<I, C, N>::convert(num, buf, BUF_COUNT, result_count);
487 /// itos
488 template < e_size_t N, typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
489 inline C const* itos(I num, C (&buf)[BUF_COUNT])
491 return itos<N, I, C>(num, buf, BUF_COUNT);
493 /// itos
494 template < e_size_t N, typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
495 inline C const* itos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
497 return itos<N, I, C>(num, buf, BUF_COUNT, result_count);
499 /* ///////////////////////////////////////////////////////////////////////
500 * Default: Decimal integer conversion
503 /// utos
504 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
505 inline C const* uitos(I num, C (&buf)[BUF_COUNT])
507 return EXTL_NS_DETAIL(uitos_impl)<I, C>::convert(num, buf, BUF_COUNT);
509 /// utos
510 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
511 inline C const* uitos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
513 return EXTL_NS_DETAIL(uitos_impl)<I, C>::convert(num, buf, BUF_COUNT, result_count);
515 /// sitos
516 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
517 inline C const* sitos(I num, C (&buf)[BUF_COUNT])
519 return EXTL_NS_DETAIL(sitos_impl)<I, C>::convert(num, buf, BUF_COUNT);
521 /// sitos
522 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
523 inline C const* sitos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
525 return EXTL_NS_DETAIL(sitos_impl)<I, C>::convert(num, buf, BUF_COUNT, result_count);
527 /// itos
528 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
529 inline C const* itos(I num, C (&buf)[BUF_COUNT])
531 return itos<10, I, C>(num, buf, BUF_COUNT);
533 /// itos
534 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
535 inline C const* itos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
537 return itos<10, I, C>(num, buf, BUF_COUNT, result_count);
539 #endif
541 #else /* !defined(EXTL_CONVERSION_ITOS_WITH_PARAM_SUPPORT) */
543 /// uitos
544 template < typename_param_k I, typename_param_k C >
545 inline C const* uitos(I num, C* buf, e_size_t const& n)
547 return EXTL_NS_DETAIL(uitos_impl)<I, C, 10>::convert(num, buf, n);
549 /// uitos
550 template < typename_param_k I, typename_param_k C >
551 inline C const* uitos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
553 return EXTL_NS_DETAIL(uitos_impl)<I, C, 10>::convert(num, buf, n, result_count);
556 /// sitos
557 template < typename_param_k I, typename_param_k C >
558 inline C const* sitos(I num, C* buf, e_size_t const& n)
560 return EXTL_NS_DETAIL(sitos_impl)<I, C, 10>::convert(num, buf, n);
562 /// sitos
563 template < typename_param_k I, typename_param_k C >
564 inline C const* sitos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
566 return EXTL_NS_DETAIL(sitos_impl)<I, C, 10>::convert(num, buf, n, result_count);
569 #if defined(EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT) || \
570 defined(EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT)
571 /// itos
572 template < typename_param_k I, typename_param_k C >
573 inline C const* itos(I num, C* buf, e_size_t const& n)
575 # ifdef EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT
576 return EXTL_NS_DETAIL(sign_selector)
578 (is_unsigned<I>::value),
579 (is_signed<I>::value),
580 I, C, 10
581 >::convert(num, buf, n);
582 # else
583 return EXTL_NS_DETAIL(sign_selector)
585 (is_unsigned<I>::value),
586 (is_signed<I>::value)
587 >::template_qual_k itos_impl<I, C, 10>::convert(num, buf, n);
588 # endif
590 /// itos
591 template < typename_param_k I, typename_param_k C >
592 inline C const* itos(I num, C* buf, e_size_t const& n, e_size_t& result_count)
594 C const* p = itos(num, buf, n);
595 result_count = n - (p - buf + 1);
596 return p;
598 #endif /* defined(EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT) || \
599 defined(EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT) */
601 # ifdef EXTL_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT
602 /// utos
603 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
604 inline C const* uitos(I num, C (&buf)[BUF_COUNT])
606 return EXTL_NS_DETAIL(uitos_impl)<I, C, 10>::convert(num, buf, BUF_COUNT);
608 /// utos
609 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
610 inline C const* uitos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
612 return EXTL_NS_DETAIL(uitos_impl)<I, C, 10>::convert(num, buf, BUF_COUNT, result_count);
614 /// sitos
615 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
616 inline C const* sitos(I num, C (&buf)[BUF_COUNT])
618 return EXTL_NS_DETAIL(sitos_impl)<I, C, 10>::convert(num, buf, BUF_COUNT);
620 /// sitos
621 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
622 inline C const* sitos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
624 return EXTL_NS_DETAIL(sitos_impl)<I, C, 10>::convert(num, buf, BUF_COUNT, result_count);
627 # if defined(EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT) || \
628 defined(EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT)
629 /// itos
630 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
631 inline C const* itos(I num, C (&buf)[BUF_COUNT])
633 return itos(num, buf, BUF_COUNT);
635 /// itos
636 template < typename_param_k I, typename_param_k C, e_size_t BUF_COUNT >
637 inline C const* itos(I num, C (&buf)[BUF_COUNT], e_size_t& result_count)
639 return itos(num, buf, BUF_COUNT, result_count);
641 # endif /* defined(EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT) || \
642 defined(EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT) */
644 # endif /* EXTL_STATIC_ARRAY_SIZE_DETERMINATION_SUPPORT */
646 #endif /* !defined(EXTL_TEMPLATE_TYPE_REQUIRED_IN_ARGLIST) && \
647 defined(EXTL_EXPLICIT_FUNCTION_TEMPLATE_ARGUMENTS_SUPPORT) */
649 /// i2s<> conversion shim
650 #if defined(EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT) || \
651 defined(EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT)
653 #if defined(EXTL_CONVERSION_ITOS_WITH_PARAM_SUPPORT)
654 # ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
655 template < typename_param_k C = e_tchar_t, e_size_t N = 10 >
656 # else
657 template < typename_param_k C, e_size_t N >
658 # endif
659 #else
660 template < typename_param_k C >
661 #endif
663 class i2s
664 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
665 : public stack_buffer<C, EXTL_ITOS_MAX_CHAR_NUM>
666 #else
667 : public stack_buffer < C
668 , EXTL_ITOS_MAX_CHAR_NUM
669 , typename_type_k memory_traits_selector<C>::memory_traits_type
671 #endif
673 private:
675 #ifdef EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT
676 typedef stack_buffer<C, EXTL_ITOS_MAX_CHAR_NUM> base_type;
677 #else
678 typedef stack_buffer< C
679 , EXTL_ITOS_MAX_CHAR_NUM
680 , typename_type_k memory_traits_selector<C>::memory_traits_type
681 > base_type;
682 #endif
684 public:
685 #if defined(EXTL_CONVERSION_ITOS_WITH_PARAM_SUPPORT)
686 typedef i2s<C, N> class_type;
687 #else
688 typedef i2s<C> class_type;
689 #endif
690 typedef typename_type_k base_type::size_type size_type;
691 typedef typename_type_k base_type::value_type dest_type;
692 typedef typename_type_k base_type::pointer pointer;
693 typedef typename_type_k base_type::const_pointer const_pointer;
695 private:
696 size_type m_result_count;
698 public:
699 template < typename_param_k I >
700 explicit_k i2s(I const& src)
701 : base_type(EXTL_ITOS_MAX_CHAR_NUM), /* 64 + 1 */
702 m_result_count(0)
704 convert(src);
706 public:
707 operator dest_type const*() const
709 const size_type n = base_type::size();
710 const_pointer buf = base_type::data();
712 return (buf + n - m_result_count - 1);
714 private:
715 template < typename_param_k I >
716 void convert(I const& src)
718 const size_type n = base_type::size();
719 const pointer buf = base_type::data();
721 #if defined(EXTL_CONVERSION_ITOS_WITH_PARAM_SUPPORT)
722 itos<N, I, C>(src, buf, n, m_result_count);
723 #else
724 itos(src, buf, n, m_result_count);
725 #endif
728 #endif /* defined(EXTL_TEMPLATE_PARTIAL_SPEC_SUPPORT) || \
729 defined(EXTL_TEMPLATE_CLASS_DEFAULT_ARGUMENT_SUPPORT) */
731 /* ///////////////////////////////////////////////////////////////////////
732 * Unit-testing
734 #ifdef EXTL_CONVERSION_ITOS_TEST_ENABLE
735 # include "unit_test/itos_test.h"
736 #endif
737 /* ///////////////////////////////////////////////////////////////////////
738 * ::extl namespace
740 EXTL_END_NAMESPACE
742 /* //////////////////////////////////////////////////////////////////// */
743 #endif /* EXTL_CONVERSION_ITOS_H */
744 /* //////////////////////////////////////////////////////////////////// */