All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ttmathmisc.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TTMath Bignum Library
3  * and is distributed under the (new) BSD licence.
4  * Author: Tomasz Sowa <t.sowa@ttmath.org>
5  */
6 
7 /*
8  * Copyright (c) 2006-2010, Tomasz Sowa
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions are met:
13  *
14  * * Redistributions of source code must retain the above copyright notice,
15  * this list of conditions and the following disclaimer.
16  *
17  * * Redistributions in binary form must reproduce the above copyright
18  * notice, this list of conditions and the following disclaimer in the
19  * documentation and/or other materials provided with the distribution.
20  *
21  * * Neither the name Tomasz Sowa nor the names of contributors to this
22  * project may be used to endorse or promote products derived
23  * from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
35  * THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef headerfilettmathmisc
39 #define headerfilettmathmisc
40 
41 
48 #include <string>
49 
50 
51 namespace ttmath
52 {
53 
57 class Misc
58 {
59 public:
60 
61 
62 /*
63  *
64  * AssignString(result, str)
65  * result = str
66  *
67  */
68 
72 static void AssignString(std::string & result, const char * str)
73 {
74  result = str;
75 }
76 
77 
78 #ifndef TTMATH_DONT_USE_WCHAR
79 
83 static void AssignString(std::wstring & result, const char * str)
84 {
85  result.clear();
86 
87  for( ; *str ; ++str )
88  result += *str;
89 }
90 
91 
95 static void AssignString(std::wstring & result, const std::string & str)
96 {
97  return AssignString(result, str.c_str());
98 }
99 
100 
104 static void AssignString(std::string & result, const wchar_t * str)
105 {
106  result.clear();
107 
108  for( ; *str ; ++str )
109  result += static_cast<char>(*str);
110 }
111 
112 
116 static void AssignString(std::string & result, const std::wstring & str)
117 {
118  return AssignString(result, str.c_str());
119 }
120 
121 #endif
122 
123 
124 /*
125  *
126  * AddString(result, str)
127  * result += str
128  *
129  */
130 
131 
135 static void AddString(std::string & result, const char * str)
136 {
137  result += str;
138 }
139 
140 
141 #ifndef TTMATH_DONT_USE_WCHAR
142 
146 static void AddString(std::wstring & result, const char * str)
147 {
148  for( ; *str ; ++str )
149  result += *str;
150 }
151 
152 #endif
153 
154 
155 /*
156  this method omits any white characters from the string
157  char_type is char or wchar_t
158 */
159 template<class char_type>
160 static void SkipWhiteCharacters(const char_type * & c)
161 {
162  // 13 is at the end in a DOS text file (\r\n)
163  while( (*c==' ' ) || (*c=='\t') || (*c==13 ) || (*c=='\n') )
164  ++c;
165 }
166 
167 
168 
169 
182 {
183  if(c>='0' && c<='9')
184  return c-'0';
185 
186  if(c>='a' && c<='z')
187  return c-'a'+10;
188 
189 return c-'A'+10;
190 }
191 
192 
202 static sint CharToDigit(uint c, uint base)
203 {
204  if( c>='0' && c<='9' )
205  c=c-'0';
206  else
207  if( c>='a' && c<='z' )
208  c=c-'a'+10;
209  else
210  if( c>='A' && c<='Z' )
211  c=c-'A'+10;
212  else
213  return -1;
214 
215 
216  if( c >= base )
217  return -1;
218 
219 
220 return sint(c);
221 }
222 
223 
224 
236 static uint DigitToChar(uint digit)
237 {
238  if( digit < 10 )
239  return digit + '0';
240 
241 return digit - 10 + 'A';
242 }
243 
244 
245 }; // struct Misc
246 
247 }
248 
249 
250 #endif
static void SkipWhiteCharacters(const char_type *&c)
Definition: ttmathmisc.h:160
static uint CharToDigit(uint c)
Definition: ttmathmisc.h:181
static void AddString(std::string &result, const char *str)
Definition: ttmathmisc.h:135
static sint CharToDigit(uint c, uint base)
Definition: ttmathmisc.h:202
static uint DigitToChar(uint digit)
Definition: ttmathmisc.h:236
static void AssignString(std::wstring &result, const std::string &str)
Definition: ttmathmisc.h:95
signed int sint
Definition: ttmathtypes.h:164
static void AssignString(std::string &result, const char *str)
Definition: ttmathmisc.h:72
static void AssignString(std::wstring &result, const char *str)
Definition: ttmathmisc.h:83
static void AssignString(std::string &result, const std::wstring &str)
Definition: ttmathmisc.h:116
static void AddString(std::wstring &result, const char *str)
Definition: ttmathmisc.h:146
unsigned int uint
Definition: ttmathtypes.h:163
static void AssignString(std::string &result, const wchar_t *str)
Definition: ttmathmisc.h:104
Definition: ttmathmisc.h:57