home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stl453up.zip / stl453fx / src / num_put.h < prev    next >
C/C++ Source or Header  |  2002-04-29  |  2KB  |  78 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18.  
  19. # ifndef _STLP_NUM_PUT_H
  20. #  define _STLP_NUM_PUT_H
  21.  
  22. #ifndef _STLP_INTERNAL_NUM_PUT_H
  23. #include <stl/_num_put.h>
  24. #endif
  25. #ifndef _STLP_INTERNAL_OSTREAM_H
  26. #include <stl/_ostream.h>
  27. #endif
  28.  
  29. _STLP_BEGIN_NAMESPACE
  30.  
  31. // Note that grouping[0] is the number of digits in the *rightmost* group.
  32. // We assume, without checking, that *last is null and that there is enough
  33. // space in the buffer to extend the number past [first, last).
  34. template <class Char>
  35. ptrdiff_t 
  36. __insert_grouping_aux(Char* first, Char* last, const string& grouping,
  37.                       Char separator, Char Plus, Char Minus,
  38.               int basechars)
  39. {
  40.   typedef string::size_type str_size;
  41.  
  42.   if (first == last)
  43.     return 0;
  44.  
  45.   int sign = 0;
  46.  
  47.   if (*first == Plus || *first == Minus) {
  48.     sign = 1;
  49.     ++first;
  50.   }
  51.  
  52.   first += basechars;
  53.   str_size n = 0;               // Index of the current group.
  54.   Char* cur_group = last;       // Points immediately beyond the rightmost
  55.                                 // digit of the current group.
  56.   int groupsize = 0;            // Size of the current group.
  57.   
  58.   while (true) {
  59.     groupsize = n < grouping.size() ? grouping[n] : groupsize;
  60.     ++n;
  61.  
  62.     if (groupsize <= 0 || groupsize >= cur_group - first)
  63.       break;
  64.  
  65.     // Insert a separator character just before position cur_group - groupsize
  66.     cur_group -= groupsize;
  67.     ++last;
  68.     copy_backward(cur_group, last, last + 1);
  69.     *cur_group = separator;
  70.   }
  71.  
  72.   return (last - first) + sign + basechars;
  73. }
  74.  
  75. _STLP_END_NAMESPACE
  76.  
  77. # endif
  78.