| 1 |
/* |
| 2 |
* This file is a part of TTMath Bignum Library |
| 3 |
* and is distributed under the (new) BSD licence. |
| 4 |
* Author: Tomasz Sowa <[email protected]> |
| 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 |
|
| 42 |
/*! |
| 43 |
\file ttmathmisc.h |
| 44 |
\brief some helpful functions |
| 45 |
*/ |
| 46 |
|
| 47 |
|
| 48 |
#include <string> |
| 49 |
|
| 50 |
|
| 51 |
namespace ttmath |
| 52 |
{ |
| 53 |
|
| 54 |
/*! |
| 55 |
some helpful functions |
| 56 |
*/ |
| 57 |
class Misc |
| 58 |
{ |
| 59 |
public: |
| 60 |
|
| 61 |
|
| 62 |
/* |
| 63 |
* |
| 64 |
* AssignString(result, str) |
| 65 |
* result = str |
| 66 |
* |
| 67 |
*/ |
| 68 |
|
| 69 |
/*! |
| 70 |
result = str |
| 71 |
*/ |
| 72 |
static void AssignString(std::string & result, const char * str) |
| 73 |
{ |
| 74 |
result = str; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
#ifndef TTMATH_DONT_USE_WCHAR |
| 79 |
|
| 80 |
/*! |
| 81 |
result = str |
| 82 |
*/ |
| 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 |
|
| 92 |
/*! |
| 93 |
result = str |
| 94 |
*/ |
| 95 |
static void AssignString(std::wstring & result, const std::string & str) |
| 96 |
{ |
| 97 |
return AssignString(result, str.c_str()); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
/*! |
| 102 |
result = str |
| 103 |
*/ |
| 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 |
|
| 113 |
/*! |
| 114 |
result = str |
| 115 |
*/ |
| 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 |
|
| 132 |
/*! |
| 133 |
result += str |
| 134 |
*/ |
| 135 |
static void AddString(std::string & result, const char * str) |
| 136 |
{ |
| 137 |
result += str; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
#ifndef TTMATH_DONT_USE_WCHAR |
| 142 |
|
| 143 |
/*! |
| 144 |
result += str |
| 145 |
*/ |
| 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 |
|
| 170 |
/*! |
| 171 |
this static method converts one character into its value |
| 172 |
|
| 173 |
for example: |
| 174 |
1 -> 1 |
| 175 |
8 -> 8 |
| 176 |
A -> 10 |
| 177 |
f -> 15 |
| 178 |
|
| 179 |
this method don't check whether c is correct or not |
| 180 |
*/ |
| 181 |
static uint CharToDigit(uint c) |
| 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 |
|
| 193 |
/*! |
| 194 |
this method changes a character 'c' into its value |
| 195 |
(if there can't be a correct value it returns -1) |
| 196 |
|
| 197 |
for example: |
| 198 |
c=2, base=10 -> function returns 2 |
| 199 |
c=A, base=10 -> function returns -1 |
| 200 |
c=A, base=16 -> function returns 10 |
| 201 |
*/ |
| 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 |
|
| 225 |
/*! |
| 226 |
this method converts a digit into a char |
| 227 |
digit should be from <0,F> |
| 228 |
(we don't have to get a base) |
| 229 |
|
| 230 |
for example: |
| 231 |
1 -> 1 |
| 232 |
8 -> 8 |
| 233 |
10 -> A |
| 234 |
15 -> F |
| 235 |
*/ |
| 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 |