You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
8.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/conversion/number/grouping_functions.hpp
  3. *
  4. * Purpose: Number formatting functions.
  5. *
  6. * Created: 28th August 2005
  7. * Updated: 30th July 2012
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2012, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/conversion/number/grouping_functions.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::format_thousands() formatting
  42. * function
  43. * (\ref group__library__conversion "Conversion" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS
  46. #define STLSOFT_INCL_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS_MAJOR 1
  49. # define STLSOFT_VER_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS_MINOR 0
  50. # define STLSOFT_VER_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS_REVISION 5
  51. # define STLSOFT_VER_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS_EDIT 13
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Includes
  55. */
  56. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  57. # include <stlsoft/stlsoft.h>
  58. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  59. #ifndef STLSOFT_INCL_STLSOFT_CONVERSION_HPP_INTEGER_TO_STRING
  60. # include <stlsoft/conversion/integer_to_string.hpp>
  61. #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_HPP_INTEGER_TO_STRING */
  62. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER
  63. # include <stlsoft/memory/auto_buffer.hpp>
  64. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_AUTO_BUFFER */
  65. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
  66. # include <stlsoft/string/char_traits.hpp>
  67. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */
  68. /* /////////////////////////////////////////////////////////////////////////
  69. * Namespace
  70. */
  71. #ifndef _STLSOFT_NO_NAMESPACE
  72. namespace stlsoft
  73. {
  74. #endif /* _STLSOFT_NO_NAMESPACE */
  75. /* /////////////////////////////////////////////////////////////////////////
  76. * Functions
  77. */
  78. /** \brief
  79. *
  80. * \ingroup group__library__conversion
  81. *
  82. * \param dest Pointer to buffer to receive translation. If NULL, function returns required size.
  83. * \param cchDest Size of available buffer. Ignored if dest is NULL.
  84. * \param picture Grouping picture. May not be NULL. Behaviour is undefined if contains any characters other than <code>fmtSep</code> and digits.
  85. * \param rawNumber The raw number form. May not be NULL. Behaviour is undefined contains any characters other than digits.
  86. * \param fmtSep The separator in the format.
  87. * \param outputSep The separator in the output.
  88. */
  89. template <ss_typename_param_k C>
  90. inline
  91. ss_size_t
  92. translate_thousands(
  93. C* dest
  94. , ss_size_t cchDest
  95. , C const* picture
  96. , C const* rawNumber
  97. , C fmtSep
  98. , C outputSep
  99. )
  100. {
  101. typedef char_traits<C> traits_t;
  102. const ss_size_t cchRawNumber = traits_t::length(rawNumber);
  103. auto_buffer<C> res(1 + 2 * cchRawNumber);
  104. C const* pic_next;
  105. for(pic_next = picture; fmtSep == *pic_next; ++pic_next)
  106. {}
  107. STLSOFT_ASSERT('0' != *pic_next);
  108. STLSOFT_ASSERT('0' < *pic_next);
  109. C const* raw_end = rawNumber + cchRawNumber;
  110. C const* const raw_begin = rawNumber;
  111. C* res_end = &*(res.end() - 1);
  112. ss_size_t num = static_cast<ss_size_t>(*pic_next - '0'); // Derive the first
  113. ss_size_t n = num;
  114. *res_end = '\0';
  115. for(--raw_end, --res_end; raw_end != raw_begin; --raw_end, --res_end)
  116. {
  117. *res_end = *raw_end;
  118. if( 0 != n &&
  119. 0 == --n)
  120. {
  121. if('\0' == *pic_next)
  122. {
  123. }
  124. else if('0' == *pic_next)
  125. {
  126. n = num;
  127. }
  128. else
  129. {
  130. C const* pic_end_next = ++pic_next;
  131. for(; *pic_end_next == fmtSep; ++pic_end_next)
  132. {}
  133. pic_next = pic_end_next;
  134. if('\0' == *pic_next)
  135. {
  136. *--res_end = outputSep;
  137. }
  138. else if('0' == *pic_next)
  139. {
  140. n = num;
  141. }
  142. else
  143. {
  144. num = static_cast<ss_size_t>(*pic_next - '0');
  145. n = num;
  146. }
  147. }
  148. if(0 != n)
  149. {
  150. *--res_end = outputSep;
  151. }
  152. }
  153. }
  154. *res_end = *raw_end;
  155. ss_size_t cch = static_cast<ss_size_t>(res.end() - res_end);
  156. if(NULL != dest)
  157. {
  158. if(cch > cchDest)
  159. {
  160. cch = cchDest;
  161. }
  162. traits_t::copy(dest, res_end, cch);
  163. }
  164. return cch;
  165. }
  166. /** \brief
  167. *
  168. * \ingroup group__library__conversion
  169. *
  170. * \param dest Pointer to buffer to receive translation. If NULL, function returns required size.
  171. * \param cchDest Size of available buffer. Ignored if dest is NULL.
  172. * \param picture Grouping picture. May not be NULL. Behaviour is undefined if contains any characters other than <code>fmtSep</code> and digits.
  173. * \param number The raw number form. May not be NULL. Behaviour is undefined contains any characters other than digits.
  174. * \param fmtSep The separator in the format.
  175. * \param outputSep The separator in the output.
  176. */
  177. template< ss_typename_param_k C
  178. , ss_typename_param_k I
  179. >
  180. inline
  181. ss_size_t
  182. format_thousands(
  183. C* dest
  184. , ss_size_t cchDest
  185. , C const* picture
  186. , I const& number
  187. , C fmtSep
  188. , C outputSep
  189. )
  190. {
  191. C szRawNumber[21]; // 21 is large enough for any 64-bit number (signed or unsigned)
  192. C const* rawNumber = integer_to_string(szRawNumber, STLSOFT_NUM_ELEMENTS(szRawNumber), static_cast<unsigned int>(number));
  193. STLSOFT_STATIC_ASSERT(sizeof(C) <= 8);
  194. return translate_thousands(dest, cchDest, picture, rawNumber, fmtSep, outputSep);
  195. }
  196. /** \brief
  197. *
  198. * \ingroup group__library__conversion
  199. *
  200. * \param dest Pointer to buffer to receive translation. If NULL, function returns required size.
  201. * \param cchDest Size of available buffer. Ignored if dest is NULL.
  202. * \param picture Grouping picture. May not be NULL. Behaviour is undefined if contains any characters other than <code>fmtSep</code> and digits.
  203. * \param number The raw number form. May not be NULL. Behaviour is undefined contains any characters other than digits.
  204. */
  205. template< ss_typename_param_k C
  206. , ss_typename_param_k I
  207. >
  208. inline
  209. ss_size_t
  210. format_thousands(
  211. C* dest
  212. , ss_size_t cchDest
  213. , C const* picture
  214. , I const& number
  215. )
  216. {
  217. return format_thousands<C, I>(dest, cchDest, picture, number, ';', ',');
  218. }
  219. /* /////////////////////////////////////////////////////////////////////////
  220. * Unittest
  221. */
  222. #ifdef STLSOFT_UNITTEST
  223. # include "./unittest/grouping_functions_unittest_.h"
  224. #endif /* STLSOFT_UNITTEST */
  225. /* ////////////////////////////////////////////////////////////////////// */
  226. #ifndef _STLSOFT_NO_NAMESPACE
  227. } // namespace stlsoft
  228. #endif /* _STLSOFT_NO_NAMESPACE */
  229. /* ////////////////////////////////////////////////////////////////////// */
  230. #endif /* !STLSOFT_INCL_STLSOFT_CONVERSION_NUMBER_HPP_GROUPING_FUNCTIONS */
  231. /* ///////////////////////////// end of file //////////////////////////// */