StringComparison.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, 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. /** @file Definition of platform independent string workers:
  34. ASSIMP_itoa10
  35. ASSIMP_stricmp
  36. ASSIMP_strincmp
  37. These functions are not consistently available on all platforms,
  38. or the provided implementations behave too differently.
  39. */
  40. #pragma once
  41. #ifndef INCLUDED_AI_STRING_WORKERS_H
  42. #define INCLUDED_AI_STRING_WORKERS_H
  43. #ifdef __GNUC__
  44. #pragma GCC system_header
  45. #endif
  46. #include <assimp/ai_assert.h>
  47. #include <assimp/defs.h>
  48. #include <cstdint>
  49. #include <cstring>
  50. #include <string>
  51. namespace Assimp {
  52. // -------------------------------------------------------------------------------
  53. /** @brief itoa with a fixed base 10
  54. * 'itoa' is not consistently available on all platforms so it is quite useful
  55. * to have a small replacement function here. No need to use a full sprintf()
  56. * if we just want to print a number ...
  57. * @param out Output buffer
  58. * @param max Maximum number of characters to be written, including '\0'.
  59. * This parameter may not be 0.
  60. * @param number Number to be written
  61. * @return Length of the output string, excluding the '\0'
  62. */
  63. inline unsigned int ASSIMP_itoa10(char *out, unsigned int max, int32_t number) {
  64. ai_assert(nullptr != out);
  65. // write the unary minus to indicate we have a negative number
  66. unsigned int written = 1u;
  67. if (number < 0 && written < max) {
  68. *out++ = '-';
  69. ++written;
  70. number = -number;
  71. }
  72. // We begin with the largest number that is not zero.
  73. int32_t cur = 1000000000; // 2147483648
  74. bool mustPrint = false;
  75. while (written < max) {
  76. const unsigned int digit = number / cur;
  77. if (mustPrint || digit > 0 || 1 == cur) {
  78. // print all future zero's from now
  79. mustPrint = true;
  80. *out++ = '0' + static_cast<char>(digit);
  81. ++written;
  82. number -= digit * cur;
  83. if (1 == cur) {
  84. break;
  85. }
  86. }
  87. cur /= 10;
  88. }
  89. // append a terminal zero
  90. *out++ = '\0';
  91. return written - 1;
  92. }
  93. // -------------------------------------------------------------------------------
  94. /** @brief itoa with a fixed base 10 (Secure template overload)
  95. * The compiler should choose this function if he or she is able to determine the
  96. * size of the array automatically.
  97. */
  98. template <size_t length>
  99. inline unsigned int ASSIMP_itoa10(char (&out)[length], int32_t number) {
  100. return ASSIMP_itoa10(out, length, number);
  101. }
  102. // -------------------------------------------------------------------------------
  103. /** @brief Helper function to do platform independent string comparison.
  104. *
  105. * This is required since stricmp() is not consistently available on
  106. * all platforms. Some platforms use the '_' prefix, others don't even
  107. * have such a function.
  108. *
  109. * @param s1 First input string
  110. * @param s2 Second input string
  111. * @return 0 if the given strings are identical
  112. */
  113. inline int ASSIMP_stricmp(const char *s1, const char *s2) {
  114. ai_assert(nullptr != s1);
  115. ai_assert(nullptr != s2);
  116. #if (defined _MSC_VER)
  117. return ::_stricmp(s1, s2);
  118. #else
  119. char c1, c2;
  120. do {
  121. c1 = tolower((unsigned char)*(s1++));
  122. c2 = tolower((unsigned char)*(s2++));
  123. } while (c1 && (c1 == c2));
  124. return c1 - c2;
  125. #endif
  126. }
  127. // -------------------------------------------------------------------------------
  128. /** @brief Case independent comparison of two std::strings
  129. *
  130. * @param a First string
  131. * @param b Second string
  132. * @return 0 if a == b
  133. */
  134. inline int ASSIMP_stricmp(const std::string &a, const std::string &b) {
  135. int i = (int)b.length() - (int)a.length();
  136. return (i ? i : ASSIMP_stricmp(a.c_str(), b.c_str()));
  137. }
  138. // -------------------------------------------------------------------------------
  139. /** @brief Helper function to do platform independent string comparison.
  140. *
  141. * This is required since strincmp() is not consistently available on
  142. * all platforms. Some platforms use the '_' prefix, others don't even
  143. * have such a function.
  144. *
  145. * @param s1 First input string
  146. * @param s2 Second input string
  147. * @param n Maximum number of characters to compare
  148. * @return 0 if the given strings are identical
  149. */
  150. inline int ASSIMP_strincmp(const char *s1, const char *s2, unsigned int n) {
  151. ai_assert(nullptr != s1);
  152. ai_assert(nullptr != s2);
  153. if (!n) {
  154. return 0;
  155. }
  156. #if (defined _MSC_VER)
  157. return ::_strnicmp(s1, s2, n);
  158. #elif defined(__GNUC__)
  159. return ::strncasecmp(s1, s2, n);
  160. #else
  161. char c1, c2;
  162. unsigned int p = 0;
  163. do {
  164. if (p++ >= n) return 0;
  165. c1 = tolower((unsigned char)*(s1++));
  166. c2 = tolower((unsigned char)*(s2++));
  167. } while (c1 && (c1 == c2));
  168. return c1 - c2;
  169. #endif
  170. }
  171. // -------------------------------------------------------------------------------
  172. /** @brief Evaluates an integer power
  173. *
  174. * todo: move somewhere where it fits better in than here
  175. */
  176. inline unsigned int integer_pow(unsigned int base, unsigned int power) {
  177. unsigned int res = 1;
  178. for (unsigned int i = 0; i < power; ++i) {
  179. res *= base;
  180. }
  181. return res;
  182. }
  183. } // namespace Assimp
  184. #endif // ! AI_STRINGCOMPARISON_H_INC