ttmathmisc.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. * Copyright (c) 2006-2010, Tomasz Sowa
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * * Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. *
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. *
  20. * * Neither the name Tomasz Sowa nor the names of contributors to this
  21. * project may be used to endorse or promote products derived
  22. * from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #ifndef headerfilettmathmisc
  37. #define headerfilettmathmisc
  38. /*!
  39. \file ttmathmisc.h
  40. \brief some helpful functions
  41. */
  42. #include <string>
  43. namespace ttmath
  44. {
  45. /*!
  46. some helpful functions
  47. */
  48. class Misc
  49. {
  50. public:
  51. /*
  52. *
  53. * AssignString(result, str)
  54. * result = str
  55. *
  56. */
  57. /*!
  58. result = str
  59. */
  60. static void AssignString(std::string & result, const char * str)
  61. {
  62. result = str;
  63. }
  64. #ifndef TTMATH_DONT_USE_WCHAR
  65. /*!
  66. result = str
  67. */
  68. static void AssignString(std::wstring & result, const char * str)
  69. {
  70. result.clear();
  71. for( ; *str ; ++str )
  72. result += *str;
  73. }
  74. /*!
  75. result = str
  76. */
  77. static void AssignString(std::wstring & result, const std::string & str)
  78. {
  79. return AssignString(result, str.c_str());
  80. }
  81. /*!
  82. result = str
  83. */
  84. static void AssignString(std::string & result, const wchar_t * str)
  85. {
  86. result.clear();
  87. for( ; *str ; ++str )
  88. result += static_cast<char>(*str);
  89. }
  90. /*!
  91. result = str
  92. */
  93. static void AssignString(std::string & result, const std::wstring & str)
  94. {
  95. return AssignString(result, str.c_str());
  96. }
  97. #endif
  98. /*
  99. *
  100. * AddString(result, str)
  101. * result += str
  102. *
  103. */
  104. /*!
  105. result += str
  106. */
  107. static void AddString(std::string & result, const char * str)
  108. {
  109. result += str;
  110. }
  111. #ifndef TTMATH_DONT_USE_WCHAR
  112. /*!
  113. result += str
  114. */
  115. static void AddString(std::wstring & result, const char * str)
  116. {
  117. for( ; *str ; ++str )
  118. result += *str;
  119. }
  120. #endif
  121. /*
  122. this method omits any white characters from the string
  123. char_type is char or wchar_t
  124. */
  125. template<class char_type>
  126. static void SkipWhiteCharacters(const char_type * & c)
  127. {
  128. // 13 is at the end in a DOS text file (\r\n)
  129. while( (*c==' ' ) || (*c=='\t') || (*c==13 ) || (*c=='\n') )
  130. ++c;
  131. }
  132. /*!
  133. this static method converts one character into its value
  134. for example:
  135. 1 -> 1
  136. 8 -> 8
  137. A -> 10
  138. f -> 15
  139. this method don't check whether c is correct or not
  140. */
  141. static uint CharToDigit(uint c)
  142. {
  143. if(c>='0' && c<='9')
  144. return c-'0';
  145. if(c>='a' && c<='z')
  146. return c-'a'+10;
  147. return c-'A'+10;
  148. }
  149. /*!
  150. this method changes a character 'c' into its value
  151. (if there can't be a correct value it returns -1)
  152. for example:
  153. c=2, base=10 -> function returns 2
  154. c=A, base=10 -> function returns -1
  155. c=A, base=16 -> function returns 10
  156. */
  157. static sint CharToDigit(uint c, uint base)
  158. {
  159. if( c>='0' && c<='9' )
  160. c=c-'0';
  161. else
  162. if( c>='a' && c<='z' )
  163. c=c-'a'+10;
  164. else
  165. if( c>='A' && c<='Z' )
  166. c=c-'A'+10;
  167. else
  168. return -1;
  169. if( c >= base )
  170. return -1;
  171. return sint(c);
  172. }
  173. /*!
  174. this method converts a digit into a char
  175. digit should be from <0,F>
  176. (we don't have to get a base)
  177. for example:
  178. 1 -> 1
  179. 8 -> 8
  180. 10 -> A
  181. 15 -> F
  182. */
  183. static uint DigitToChar(uint digit)
  184. {
  185. if( digit < 10 )
  186. return digit + '0';
  187. return digit - 10 + 'A';
  188. }
  189. }; // struct Misc
  190. }
  191. #endif