00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00048 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS
00049 #define STLSOFT_INCL_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define STLSOFT_VER_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS_MAJOR 1
00053 # define STLSOFT_VER_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS_MINOR 0
00054 # define STLSOFT_VER_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS_REVISION 5
00055 # define STLSOFT_VER_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS_EDIT 12
00056 #endif
00057
00058
00059
00060
00061
00062 #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
00063 # include <stlsoft/stlsoft.h>
00064 #endif
00065 #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_INTEGER_TO_STRING
00066 # include <stlsoft/conversion/integer_to_string.hpp>
00067 #endif
00068 #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
00069 # include <stlsoft/memory/auto_buffer.hpp>
00070 #endif
00071 #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
00072 # include <stlsoft/string/char_traits.hpp>
00073 #endif
00074
00075
00076
00077
00078
00079 #ifndef _STLSOFT_NO_NAMESPACE
00080 namespace stlsoft
00081 {
00082 #endif
00083
00084
00085
00086
00087
00099 template <ss_typename_param_k C>
00100 inline ss_size_t translate_thousands( C *dest
00101 , ss_size_t cchDest
00102 , C const *picture
00103 , C const *rawNumber
00104 , C fmtSep
00105 , C outputSep
00106 )
00107 {
00108 typedef char_traits<C> traits_t;
00109
00110 const ss_size_t cchRawNumber = traits_t::length(rawNumber);
00111 auto_buffer<C> res(1 + 2 * cchRawNumber);
00112 C const *pic_next;
00113
00114
00115 for(pic_next = picture; fmtSep == *pic_next; ++pic_next)
00116 {}
00117 STLSOFT_ASSERT('0' != *pic_next);
00118 STLSOFT_ASSERT('0' < *pic_next);
00119
00120 C const* raw_end = rawNumber + cchRawNumber;
00121 C const* const raw_begin = rawNumber;
00122 C* res_end = &*(res.end() - 1);
00123
00124 ss_size_t num = static_cast<ss_size_t>(*pic_next - '0');
00125 ss_size_t n = num;
00126
00127 *res_end = '\0';
00128 for(--raw_end, --res_end; raw_end != raw_begin; --raw_end, --res_end)
00129 {
00130 *res_end = *raw_end;
00131 if( 0 != n &&
00132 0 == --n)
00133 {
00134 if('\0' == *pic_next)
00135 {
00136 }
00137 else if('0' == *pic_next)
00138 {
00139 n = num;
00140 }
00141 else
00142 {
00143 C const* pic_end_next = ++pic_next;
00144
00145 for(; *pic_end_next == fmtSep; ++pic_end_next)
00146 {}
00147
00148 pic_next = pic_end_next;
00149
00150 if('\0' == *pic_next)
00151 {
00152 *--res_end = outputSep;
00153 }
00154 else if('0' == *pic_next)
00155 {
00156 n = num;
00157 }
00158 else
00159 {
00160 num = static_cast<ss_size_t>(*pic_next - '0');
00161 n = num;
00162 }
00163 }
00164
00165 if(0 != n)
00166 {
00167 *--res_end = outputSep;
00168 }
00169 }
00170 }
00171 *res_end = *raw_end;
00172
00173 ss_size_t cch = static_cast<ss_size_t>(res.end() - res_end);
00174
00175 if(NULL != dest)
00176 {
00177 if(cch > cchDest)
00178 {
00179 cch = cchDest;
00180 }
00181
00182 traits_t::copy(dest, res_end, cch);
00183 }
00184
00185 return cch;
00186 }
00187
00199 template< ss_typename_param_k C
00200 , ss_typename_param_k I
00201 >
00202 inline ss_size_t format_thousands( C *dest
00203 , ss_size_t cchDest
00204 , C const *picture
00205 , I const &number
00206 , C fmtSep
00207 , C outputSep
00208 )
00209 {
00210 C szRawNumber[21];
00211 C const* rawNumber = integer_to_string(szRawNumber, STLSOFT_NUM_ELEMENTS(szRawNumber), static_cast<unsigned int>(number));
00212
00213 STLSOFT_STATIC_ASSERT(sizeof(C) <= 8);
00214
00215 return translate_thousands(dest, cchDest, picture, rawNumber, fmtSep, outputSep);
00216 }
00217
00227 template< ss_typename_param_k C
00228 , ss_typename_param_k I
00229 >
00230 inline ss_size_t format_thousands( C *dest
00231 , ss_size_t cchDest
00232 , C const *picture
00233 , I const &number
00234 )
00235 {
00236 return format_thousands<C, I>(dest, cchDest, picture, number, ';', ',');
00237 }
00238
00239
00240
00241
00242
00243 #ifdef STLSOFT_UNITTEST
00244 # include "./unittest/grouping_functions_unittest_.h"
00245 #endif
00246
00247
00248
00249 #ifndef _STLSOFT_NO_NAMESPACE
00250 }
00251 #endif
00252
00253
00254
00255 #endif
00256
00257