StringUtils.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. #pragma once
  34. #ifndef INCLUDED_AI_STRINGUTILS_H
  35. #define INCLUDED_AI_STRINGUTILS_H
  36. #ifdef __GNUC__
  37. #pragma GCC system_header
  38. #endif
  39. #include <assimp/defs.h>
  40. #include <cstdarg>
  41. #include <algorithm>
  42. #include <cctype>
  43. #include <cstdlib>
  44. #include <locale>
  45. #include <sstream>
  46. #include <iomanip>
  47. #if defined(_MSC_VER) && !defined(__clang__)
  48. # define AI_SIZEFMT "%Iu"
  49. #else
  50. # define AI_SIZEFMT "%zu"
  51. #endif
  52. // ---------------------------------------------------------------------------------
  53. /// @fn ai_snprintf
  54. /// @brief The portable version of the function snprintf ( C99 standard ), which
  55. /// works on visual studio compilers 2013 and earlier.
  56. /// @param outBuf The buffer to write in
  57. /// @param size The buffer size
  58. /// @param format The format string
  59. /// @param ap The additional arguments.
  60. /// @return The number of written characters if the buffer size was big enough.
  61. /// If an encoding error occurs, a negative number is returned.
  62. // ---------------------------------------------------------------------------------
  63. #if defined(_MSC_VER) && _MSC_VER < 1900
  64. inline int c99_ai_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
  65. int count(-1);
  66. if (0 != size) {
  67. count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
  68. }
  69. if (count == -1) {
  70. count = _vscprintf(format, ap);
  71. }
  72. return count;
  73. }
  74. inline int ai_snprintf(char *outBuf, size_t size, const char *format, ...) {
  75. int count;
  76. va_list ap;
  77. va_start(ap, format);
  78. count = c99_ai_vsnprintf(outBuf, size, format, ap);
  79. va_end(ap);
  80. return count;
  81. }
  82. #elif defined(__MINGW32__)
  83. # define ai_snprintf __mingw_snprintf
  84. #else
  85. # define ai_snprintf snprintf
  86. #endif
  87. // ---------------------------------------------------------------------------------
  88. /// @fn to_string
  89. /// @brief The portable version of to_string ( some gcc-versions on embedded
  90. /// devices are not supporting this).
  91. /// @param value The value to write into the std::string.
  92. /// @return The value as a std::string
  93. // ---------------------------------------------------------------------------------
  94. template <typename T>
  95. AI_FORCE_INLINE std::string ai_to_string(T value) {
  96. std::ostringstream os;
  97. os << value;
  98. return os.str();
  99. }
  100. // ---------------------------------------------------------------------------------
  101. /// @fn ai_strtof
  102. /// @brief The portable version of strtof.
  103. /// @param begin The first character of the string.
  104. /// @param end The last character
  105. /// @return The float value, 0.0f in case of an error.
  106. // ---------------------------------------------------------------------------------
  107. AI_FORCE_INLINE
  108. float ai_strtof(const char *begin, const char *end) {
  109. if (nullptr == begin) {
  110. return 0.0f;
  111. }
  112. float val(0.0f);
  113. if (nullptr == end) {
  114. val = static_cast<float>(::atof(begin));
  115. } else {
  116. std::string::size_type len(end - begin);
  117. std::string token(begin, len);
  118. val = static_cast<float>(::atof(token.c_str()));
  119. }
  120. return val;
  121. }
  122. // ---------------------------------------------------------------------------------
  123. /// @fn DecimalToHexa
  124. /// @brief The portable to convert a decimal value into a hexadecimal string.
  125. /// @param toConvert Value to convert
  126. /// @return The hexadecimal string, is empty in case of an error.
  127. // ---------------------------------------------------------------------------------
  128. template <class T>
  129. AI_FORCE_INLINE std::string ai_decimal_to_hexa(T toConvert) {
  130. std::string result;
  131. std::stringstream ss;
  132. ss << std::hex << toConvert;
  133. ss >> result;
  134. for (size_t i = 0; i < result.size(); ++i) {
  135. result[i] = (char)toupper((unsigned char)result[i]);
  136. }
  137. return result;
  138. }
  139. // ---------------------------------------------------------------------------------
  140. /// @brief translate RGBA to String
  141. /// @param r aiColor.r
  142. /// @param g aiColor.g
  143. /// @param b aiColor.b
  144. /// @param a aiColor.a
  145. /// @param with_head #
  146. /// @return The hexadecimal string, is empty in case of an error.
  147. // ---------------------------------------------------------------------------------
  148. AI_FORCE_INLINE std::string ai_rgba2hex(int r, int g, int b, int a, bool with_head) {
  149. std::stringstream ss;
  150. if (with_head) {
  151. ss << "#";
  152. }
  153. ss << std::hex << std::setfill('0') << std::setw(8) << (r << 24 | g << 16 | b << 8 | a);
  154. return ss.str();
  155. }
  156. // ---------------------------------------------------------------------------------
  157. /// @brief Performs a trim from start (in place)
  158. /// @param s string to trim.
  159. // ---------------------------------------------------------------------------------
  160. AI_FORCE_INLINE void ai_trim_left(std::string &s) {
  161. s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) {
  162. return !std::isspace(ch);
  163. }));
  164. }
  165. // ---------------------------------------------------------------------------------
  166. /// @brief Performs a trim from end (in place).
  167. /// @param s string to trim.
  168. // ---------------------------------------------------------------------------------
  169. AI_FORCE_INLINE void ai_trim_right(std::string &s) {
  170. s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) {
  171. return !std::isspace(ch);
  172. }).base(), s.end());
  173. }
  174. // ---------------------------------------------------------------------------------
  175. /// @brief Performs a trim from both ends (in place).
  176. /// @param s string to trim.
  177. // ---------------------------------------------------------------------------------
  178. AI_FORCE_INLINE std::string ai_trim(std::string &s) {
  179. std::string out(s);
  180. ai_trim_left(out);
  181. ai_trim_right(out);
  182. return out;
  183. }
  184. // ---------------------------------------------------------------------------------
  185. /// @brief Performs a to lower operation onto on single character.
  186. /// @param in The character
  187. /// @return the character as lower-case.
  188. // ---------------------------------------------------------------------------------
  189. template <class char_t>
  190. AI_FORCE_INLINE char_t ai_tolower(char_t in) {
  191. return (in >= (char_t)'A' && in <= (char_t)'Z') ? (char_t)(in + 0x20) : in;
  192. }
  193. // ---------------------------------------------------------------------------------
  194. /// @brief Performs a ToLower-operation and return the lower-case string.
  195. /// @param in The incoming string.
  196. /// @return The string as lowercase.
  197. // ---------------------------------------------------------------------------------
  198. AI_FORCE_INLINE std::string ai_tolower(const std::string &in) {
  199. std::string out(in);
  200. ai_trim_left(out);
  201. ai_trim_right(out);
  202. std::transform(out.begin(), out.end(), out.begin(), [](unsigned char c) { return ai_tolower(c); });
  203. return out;
  204. }
  205. // ---------------------------------------------------------------------------------
  206. /// @brief Performs a to upper operation onto on single character.
  207. /// @param in The character
  208. /// @return the character as upper-case.
  209. // ---------------------------------------------------------------------------------
  210. template <class char_t>
  211. AI_FORCE_INLINE char_t ai_toupper(char_t in) {
  212. return (in >= (char_t)'a' && in <= (char_t)'z') ? (char_t)(in - 0x20) : in;
  213. }
  214. // ---------------------------------------------------------------------------------
  215. /// @brief Performs a ToLower-operation and return the upper-case string.
  216. /// @param in The incoming string.
  217. /// @return The string as uppercase.
  218. // ---------------------------------------------------------------------------------
  219. AI_FORCE_INLINE std::string ai_str_toupper(const std::string &in) {
  220. std::string out(in);
  221. std::transform(out.begin(), out.end(), out.begin(), [](char c) { return ai_toupper(c); });
  222. return out;
  223. }
  224. // ---------------------------------------------------------------------------------
  225. /// @brief Make a string printable by replacing all non-printable characters with
  226. /// the specified placeholder character.
  227. /// @param in The incoming string.
  228. /// @param placeholder Placeholder character, default is a question mark.
  229. /// @return The string, with all non-printable characters replaced.
  230. // ---------------------------------------------------------------------------------
  231. AI_FORCE_INLINE std::string ai_str_toprintable(const std::string &in, char placeholder = '?') {
  232. std::string out(in);
  233. std::transform(out.begin(), out.end(), out.begin(), [placeholder] (unsigned char c) {
  234. return isprint(c) ? (char)c : placeholder;
  235. });
  236. return out;
  237. }
  238. // ---------------------------------------------------------------------------------
  239. /// @brief Make a string printable by replacing all non-printable characters with
  240. /// the specified placeholder character.
  241. /// @param in The incoming string.
  242. /// @param len The length of the incoming string.
  243. /// @param placeholder Placeholder character, default is a question mark.
  244. /// @return The string, with all non-printable characters replaced. Will return an
  245. /// empty string if in is null or len is <= 0.
  246. // ---------------------------------------------------------------------------------
  247. AI_FORCE_INLINE std::string ai_str_toprintable(const char *in, int len, char placeholder = '?') {
  248. return (in && len > 0) ? ai_str_toprintable(std::string(in, len), placeholder) : std::string();
  249. }
  250. #endif // INCLUDED_AI_STRINGUTILS_H