StringComparison.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2015, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. // Modified by Lasse Oorni for Urho3D
  34. /** @file Definition of platform independent string workers:
  35. ASSIMP_itoa10
  36. ASSIMP_stricmp
  37. ASSIMP_strincmp
  38. These functions are not consistently available on all platforms,
  39. or the provided implementations behave too differently.
  40. */
  41. #ifndef INCLUDED_AI_STRING_WORKERS_H
  42. #define INCLUDED_AI_STRING_WORKERS_H
  43. #include "../include/assimp/ai_assert.h"
  44. #include "StringComparison.h"
  45. #include <string.h>
  46. // Urho3D: VS2008 compatibility
  47. #if !defined(_MSC_VER) || (_MSC_VER >= 1600)
  48. #include <stdint.h>
  49. #else
  50. #include "../include/assimp/Compiler/pstdint.h"
  51. #endif
  52. #include <string>
  53. #include <cctype>
  54. namespace Assimp {
  55. // -------------------------------------------------------------------------------
  56. /** @brief itoa with a fixed base 10
  57. * 'itoa' is not consistently available on all platforms so it is quite useful
  58. * to have a small replacement function here. No need to use a full sprintf()
  59. * if we just want to print a number ...
  60. * @param out Output buffer
  61. * @param max Maximum number of characters to be written, including '\0'.
  62. * This parameter may not be 0.
  63. * @param number Number to be written
  64. * @return Length of the output string, excluding the '\0'
  65. */
  66. inline unsigned int ASSIMP_itoa10( char* out, unsigned int max, int32_t number)
  67. {
  68. ai_assert(NULL != out);
  69. // write the unary minus to indicate we have a negative number
  70. unsigned int written = 1u;
  71. if (number < 0 && written < max) {
  72. *out++ = '-';
  73. ++written;
  74. number = -number;
  75. }
  76. // We begin with the largest number that is not zero.
  77. int32_t cur = 1000000000; // 2147483648
  78. bool mustPrint = false;
  79. while (written < max) {
  80. const unsigned int digit = number / cur;
  81. if (mustPrint || digit > 0 || 1 == cur) {
  82. // print all future zeroes from now
  83. mustPrint = true;
  84. *out++ = '0'+static_cast<char>(digit);
  85. ++written;
  86. number -= digit*cur;
  87. if (1 == cur) {
  88. break;
  89. }
  90. }
  91. cur /= 10;
  92. }
  93. // append a terminal zero
  94. *out++ = '\0';
  95. return written-1;
  96. }
  97. // -------------------------------------------------------------------------------
  98. /** @brief itoa with a fixed base 10 (Secure template overload)
  99. * The compiler should choose this function if he or she is able to determine the
  100. * size of the array automatically.
  101. */
  102. template <size_t length>
  103. inline unsigned int ASSIMP_itoa10( char(& out)[length], int32_t number)
  104. {
  105. return ASSIMP_itoa10(out,length,number);
  106. }
  107. // -------------------------------------------------------------------------------
  108. /** @brief Helper function to do platform independent string comparison.
  109. *
  110. * This is required since stricmp() is not consistently available on
  111. * all platforms. Some platforms use the '_' prefix, others don't even
  112. * have such a function.
  113. *
  114. * @param s1 First input string
  115. * @param s2 Second input string
  116. * @return 0 if the given strings are identical
  117. */
  118. inline int ASSIMP_stricmp(const char *s1, const char *s2)
  119. {
  120. ai_assert(NULL != s1 && NULL != s2);
  121. #if (defined _MSC_VER)
  122. return ::_stricmp(s1,s2);
  123. #elif defined( __GNUC__ )
  124. return ::strcasecmp(s1,s2);
  125. #else
  126. char c1, c2;
  127. do {
  128. c1 = tolower(*s1++);
  129. c2 = tolower(*s2++);
  130. }
  131. while ( c1 && (c1 == c2) );
  132. return c1 - c2;
  133. #endif
  134. }
  135. // -------------------------------------------------------------------------------
  136. /** @brief Case independent comparison of two std::strings
  137. *
  138. * @param a First string
  139. * @param b Second string
  140. * @return 0 if a == b
  141. */
  142. inline int ASSIMP_stricmp(const std::string& a, const std::string& b)
  143. {
  144. int i = (int)b.length()-(int)a.length();
  145. return (i ? i : ASSIMP_stricmp(a.c_str(),b.c_str()));
  146. }
  147. // -------------------------------------------------------------------------------
  148. /** @brief Helper function to do platform independent string comparison.
  149. *
  150. * This is required since strincmp() is not consistently available on
  151. * all platforms. Some platforms use the '_' prefix, others don't even
  152. * have such a function.
  153. *
  154. * @param s1 First input string
  155. * @param s2 Second input string
  156. * @param n Macimum number of characters to compare
  157. * @return 0 if the given strings are identical
  158. */
  159. inline int ASSIMP_strincmp(const char *s1, const char *s2, unsigned int n)
  160. {
  161. ai_assert(NULL != s1 && NULL != s2);
  162. if (!n)return 0;
  163. #if (defined _MSC_VER)
  164. return ::_strnicmp(s1,s2,n);
  165. #elif defined( __GNUC__ )
  166. return ::strncasecmp(s1,s2, n);
  167. #else
  168. char c1, c2;
  169. unsigned int p = 0;
  170. do
  171. {
  172. if (p++ >= n)return 0;
  173. c1 = tolower(*s1++);
  174. c2 = tolower(*s2++);
  175. }
  176. while ( c1 && (c1 == c2) );
  177. return c1 - c2;
  178. #endif
  179. }
  180. // -------------------------------------------------------------------------------
  181. /** @brief Evaluates an integer power
  182. *
  183. * todo: move somewhere where it fits better in than here
  184. */
  185. inline unsigned int integer_pow (unsigned int base, unsigned int power)
  186. {
  187. unsigned int res = 1;
  188. for (unsigned int i = 0; i < power;++i)
  189. res *= base;
  190. return res;
  191. }
  192. } // end of namespace
  193. #endif // ! AI_STRINGCOMPARISON_H_INC