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.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /externals/g4tools/include/tools/num2s (Version 11.3.0) and /externals/g4tools/include/tools/num2s (Version 11.2.2)


  1 // Copyright (C) 2010, Guy Barrand. All rights      1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.            2 // See the file tools.license for terms.
  3                                                     3 
  4 #ifndef tools_num2s                                 4 #ifndef tools_num2s
  5 #define tools_num2s                                 5 #define tools_num2s
  6                                                     6 
  7 // write numerics in a string as if done with       7 // write numerics in a string as if done with a std::ostringstream::operator<<(<num>).
  8                                                     8 
  9 #include "sprintf"                                  9 #include "sprintf"
 10 #include "typedefs"                                10 #include "typedefs"
 11                                                    11 
 12 #include <cstddef> //ptrdiff_t                     12 #include <cstddef> //ptrdiff_t
 13                                                    13 
 14 namespace tools {                                  14 namespace tools {
 15                                                    15 
 16 /*                                                 16 /*
 17 inline bool num2s(unsigned char a_value,std::s     17 inline bool num2s(unsigned char a_value,std::string& a_s){
 18   return print2s(a_s,32,"%c",a_value);             18   return print2s(a_s,32,"%c",a_value);
 19 }                                                  19 }
 20                                                    20 
 21 inline bool num2s(char a_value,std::string& a_     21 inline bool num2s(char a_value,std::string& a_s){
 22   return print2s(a_s,32,"%c",a_value);             22   return print2s(a_s,32,"%c",a_value);
 23 }                                                  23 }
 24 */                                                 24 */
 25                                                    25 
 26 inline bool num2s(unsigned short a_value,std::     26 inline bool num2s(unsigned short a_value,std::string& a_s) { //used in value
 27   return print2s(a_s,32,"%u",a_value);             27   return print2s(a_s,32,"%u",a_value);
 28 }                                                  28 }
 29                                                    29 
 30 inline bool num2s(short a_value,std::string& a     30 inline bool num2s(short a_value,std::string& a_s){ //used in value
 31   return print2s(a_s,32,"%d",a_value);             31   return print2s(a_s,32,"%d",a_value);
 32 }                                                  32 }
 33                                                    33 
 34 inline bool num2s(unsigned int a_value,std::st     34 inline bool num2s(unsigned int a_value,std::string& a_s) {
 35   return print2s(a_s,32,"%u",a_value);             35   return print2s(a_s,32,"%u",a_value);
 36 }                                                  36 }
 37                                                    37 
 38 /*                                                 38 /*
 39 inline bool num2sx(unsigned int a_value,std::s     39 inline bool num2sx(unsigned int a_value,std::string& a_s) {
 40   return print2s(a_s,32,"%x",a_value);             40   return print2s(a_s,32,"%x",a_value);
 41 }                                                  41 }
 42 */                                                 42 */
 43                                                    43 
 44 inline bool num2s(int a_value,std::string& a_s     44 inline bool num2s(int a_value,std::string& a_s){
 45   return print2s(a_s,32,"%d",a_value);             45   return print2s(a_s,32,"%d",a_value);
 46 }                                                  46 }
 47                                                    47 
 48 inline bool num2s(uint64 a_value,std::string&      48 inline bool num2s(uint64 a_value,std::string& a_s) {
 49   return print2s(a_s,32,uint64_format(),a_valu     49   return print2s(a_s,32,uint64_format(),a_value);
 50 }                                                  50 }
 51                                                    51 
 52 inline bool num2s(int64 a_value,std::string& a     52 inline bool num2s(int64 a_value,std::string& a_s){
 53   return print2s(a_s,32,int64_format(),a_value     53   return print2s(a_s,32,int64_format(),a_value);
 54 }                                                  54 }
 55                                                    55 
 56 inline bool num2s(float a_value,std::string& a     56 inline bool num2s(float a_value,std::string& a_s){
 57   return print2s(a_s,32,"%g",a_value);             57   return print2s(a_s,32,"%g",a_value);
 58 }                                                  58 }
 59                                                    59 
 60 inline bool num2s(double a_value,std::string&      60 inline bool num2s(double a_value,std::string& a_s){
 61   return print2s(a_s,32,"%g",a_value);             61   return print2s(a_s,32,"%g",a_value);
 62 }                                                  62 }
 63                                                    63 
 64 inline bool size_t2s(size_t a_value,std::strin     64 inline bool size_t2s(size_t a_value,std::string& a_s) {
 65   if(sizeof(size_t)==8) {                          65   if(sizeof(size_t)==8) {
 66     return num2s((uint64)a_value,a_s);             66     return num2s((uint64)a_value,a_s);
 67   } else { //assume 4 :                            67   } else { //assume 4 :
 68     return num2s((uint32)a_value,a_s);             68     return num2s((uint32)a_value,a_s);
 69   }                                                69   }
 70 }                                                  70 }
 71                                                    71 
 72 inline bool ptrdiff_t2s(ptrdiff_t a_value,std:     72 inline bool ptrdiff_t2s(ptrdiff_t a_value,std::string& a_s) {  //used in write_bsg.
 73   if(sizeof(ptrdiff_t)==8) {                       73   if(sizeof(ptrdiff_t)==8) {
 74     return num2s((int64)a_value,a_s);              74     return num2s((int64)a_value,a_s);
 75   } else { //assume 4 :                            75   } else { //assume 4 :
 76     return num2s((int32)a_value,a_s);              76     return num2s((int32)a_value,a_s);
 77   }                                                77   }
 78 }                                                  78 }
 79                                                    79 
 80 /*                                                 80 /*
 81 inline bool num2s(bool a_value,std::string& a_     81 inline bool num2s(bool a_value,std::string& a_s){
 82   //a_s = a_value?"true":"false";                  82   //a_s = a_value?"true":"false";
 83   a_s = a_value?"1":"0";                           83   a_s = a_value?"1":"0";
 84   return true;                                     84   return true;
 85 }                                                  85 }
 86 */                                                 86 */
 87                                                    87 
 88 template <class T>                                 88 template <class T>
 89 inline bool numas(const T& a_value,std::string     89 inline bool numas(const T& a_value,std::string& a_s){
 90   std::string stmp;                                90   std::string stmp;
 91   if(!num2s(a_value,stmp)) return false;           91   if(!num2s(a_value,stmp)) return false;
 92   a_s += stmp;                                     92   a_s += stmp;
 93   return true;                                     93   return true;
 94 }                                                  94 }
 95                                                    95 
 96 template <class T>                                 96 template <class T>
 97 inline bool size_tas(const T& a_value,std::str     97 inline bool size_tas(const T& a_value,std::string& a_s){
 98   std::string stmp;                                98   std::string stmp;
 99   if(!size_t2s(a_value,stmp)) return false;        99   if(!size_t2s(a_value,stmp)) return false;
100   a_s += stmp;                                    100   a_s += stmp;
101   return true;                                    101   return true;
102 }                                                 102 }
103                                                   103 
104 /*                                                104 /*
105 inline bool num2s(unsigned int a_linen,const c    105 inline bool num2s(unsigned int a_linen,const char* a_lines[],std::string& a_s) {
106   a_s.clear();                                    106   a_s.clear();
107   for(unsigned int index=0;index<a_linen;index    107   for(unsigned int index=0;index<a_linen;index++) {
108     a_s += a_lines[index];                        108     a_s += a_lines[index];
109     a_s += "\n";                                  109     a_s += "\n";
110   }                                               110   }
111   return true;                                    111   return true;
112 }                                                 112 }
113 */                                                113 */
114                                                   114 
115 // for the below std::vector num2s in case T=s    115 // for the below std::vector num2s in case T=std::string :
116 inline bool num2s(const std::string& a_value,s    116 inline bool num2s(const std::string& a_value,std::string& a_s){a_s = a_value;return true;}
117                                                   117 
118 template <class T>                                118 template <class T>
119 class num_out : public std::string {              119 class num_out : public std::string {
120   typedef std::string parent;                     120   typedef std::string parent;
121 private:                                          121 private:
122   //typedef typename std::enable_if<std::is_fl    122   //typedef typename std::enable_if<std::is_floating_point<T>::value, T>::type type_t;
123   //typedef typename std::enable_if<std::is_in    123   //typedef typename std::enable_if<std::is_integral<T>::value, T>::type type_t;
124   //typedef typename std::enable_if<std::is_ar    124   //typedef typename std::enable_if<std::is_arithmetic<T>::value, T>::type type_t;
125 public:                                           125 public:
126   num_out(const T& a_value) {                     126   num_out(const T& a_value) {
127     parent::operator+=("\"");                     127     parent::operator+=("\"");
128     if(!numas(a_value,*this)) {} //throw          128     if(!numas(a_value,*this)) {} //throw
129     parent::operator+=("\"");                     129     parent::operator+=("\"");
130   }                                               130   }
131 public:                                           131 public:
132   num_out(const num_out& a_from):parent(a_from    132   num_out(const num_out& a_from):parent(a_from){}
133   num_out& operator=(const num_out& a_from){pa    133   num_out& operator=(const num_out& a_from){parent::operator=(a_from);return *this;}
134 };                                                134 };
135                                                   135 
136 }                                                 136 }
137                                                   137 
138 #include <vector>                                 138 #include <vector>
139                                                   139 
140 namespace tools {                                 140 namespace tools {
141                                                   141 
142 template <class VEC>                              142 template <class VEC>
143 inline bool nums2s(const VEC& a_vals,std::stri    143 inline bool nums2s(const VEC& a_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
144   a_s.clear();                                    144   a_s.clear();
145   typename VEC::size_type number = a_vals.size    145   typename VEC::size_type number = a_vals.size();
146   if(number<=0) return true; //it is ok.          146   if(number<=0) return true; //it is ok.
147   number--;                                       147   number--;
148   std::string stmp;                               148   std::string stmp;
149   bool status = true;                             149   bool status = true;
150   for(typename VEC::size_type index=0;index<nu    150   for(typename VEC::size_type index=0;index<number;index++) {
151     if(!num2s(a_vals[index],stmp)) status = fa    151     if(!num2s(a_vals[index],stmp)) status = false; //continue.
152     a_s += stmp;                                  152     a_s += stmp;
153     a_s += a_sep;                                 153     a_s += a_sep;
154   }                                               154   }
155   if(!num2s(a_vals[number],stmp)) status = fal    155   if(!num2s(a_vals[number],stmp)) status = false;
156   a_s += stmp;                                    156   a_s += stmp;
157   if(a_sep_at_end) a_s += a_sep;                  157   if(a_sep_at_end) a_s += a_sep;
158   return status;                                  158   return status;
159 }                                                 159 }
160                                                   160 
161 template <class T>                                161 template <class T>
162 inline bool nums2s(const std::vector<T>& a_val    162 inline bool nums2s(const std::vector<T>& a_vals,std::string& a_s,const std::string& a_sep = "\n",bool a_sep_at_end = false) {
163   return nums2s< std::vector<T> >(a_vals,a_s,a    163   return nums2s< std::vector<T> >(a_vals,a_s,a_sep,a_sep_at_end);
164 }                                                 164 }
165                                                   165 
166 }                                                 166 }
167                                                   167 
168 #endif                                            168 #endif