1/*
2Open Asset Import Library (assimp)
3----------------------------------------------------------------------
4
5Copyright (c) 2006-2025, assimp team
6
7All rights reserved.
8
9Redistribution and use of this software in source and binary forms,
10with or without modification, are permitted provided that the
11following conditions are met:
12
13* Redistributions of source code must retain the above
14copyright notice, this list of conditions and the
15following disclaimer.
16
17* Redistributions in binary form must reproduce the above
18copyright notice, this list of conditions and the
19following disclaimer in the documentation and/or other
20materials provided with the distribution.
21
22* Neither the name of the assimp team, nor the names of its
23contributors may be used to endorse or promote products
24derived from this software without specific prior
25written permission of the assimp team.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38
39----------------------------------------------------------------------
40*/
41#pragma once
42#ifndef INCLUDED_AI_STRINGUTILS_H
43#define INCLUDED_AI_STRINGUTILS_H
44
45#ifdef __GNUC__
46#pragma GCC system_header
47#endif
48
49#include <assimp/defs.h>
50
51#include <cstdarg>
52#include <algorithm>
53#include <cctype>
54#include <cstdlib>
55#include <locale>
56#include <sstream>
57#include <iomanip>
58
59#if defined(_MSC_VER) && !defined(__clang__)
60# define AI_SIZEFMT "%Iu"
61#else
62# define AI_SIZEFMT "%zu"
63#endif
64
65// ---------------------------------------------------------------------------------
66/// @fn ai_snprintf
67/// @brief The portable version of the function snprintf ( C99 standard ), which
68/// works on visual studio compilers 2013 and earlier.
69/// @param outBuf The buffer to write in
70/// @param size The buffer size
71/// @param format The format string
72/// @param ap The additional arguments.
73/// @return The number of written characters if the buffer size was big enough.
74/// If an encoding error occurs, a negative number is returned.
75// ---------------------------------------------------------------------------------
76#if defined(_MSC_VER) && _MSC_VER < 1900
77
78inline int c99_ai_vsnprintf(char *outBuf, size_t size, const char *format, va_list ap) {
79 int count(-1);
80 if (0 != size) {
81 count = _vsnprintf_s(outBuf, size, _TRUNCATE, format, ap);
82 }
83 if (count == -1) {
84 count = _vscprintf(format, ap);
85 }
86
87 return count;
88}
89
90inline int ai_snprintf(char *outBuf, size_t size, const char *format, ...) {
91 int count;
92 va_list ap;
93
94 va_start(ap, format);
95 count = c99_ai_vsnprintf(outBuf, size, format, ap);
96 va_end(ap);
97
98 return count;
99}
100
101#elif defined(__MINGW32__)
102# define ai_snprintf __mingw_snprintf
103#else
104# define ai_snprintf snprintf
105#endif
106
107// ---------------------------------------------------------------------------------
108/// @fn to_string
109/// @brief The portable version of to_string ( some gcc-versions on embedded
110/// devices are not supporting this).
111/// @param value The value to write into the std::string.
112/// @return The value as a std::string
113// ---------------------------------------------------------------------------------
114template <typename T>
115AI_FORCE_INLINE std::string ai_to_string(T value) {
116 std::ostringstream os;
117 os << value;
118
119 return os.str();
120}
121
122// ---------------------------------------------------------------------------------
123/// @fn ai_strtof
124/// @brief The portable version of strtof.
125/// @param begin The first character of the string.
126/// @param end The last character
127/// @return The float value, 0.0f in case of an error.
128// ---------------------------------------------------------------------------------
129AI_FORCE_INLINE
130float ai_strtof(const char *begin, const char *end) {
131 if (nullptr == begin) {
132 return 0.0f;
133 }
134 float val(0.0f);
135 if (nullptr == end) {
136 val = static_cast<float>(::atof(nptr: begin));
137 } else {
138 std::string::size_type len(end - begin);
139 std::string token(begin, len);
140 val = static_cast<float>(::atof(nptr: token.c_str()));
141 }
142
143 return val;
144}
145
146// ---------------------------------------------------------------------------------
147/// @fn DecimalToHexa
148/// @brief The portable to convert a decimal value into a hexadecimal string.
149/// @param toConvert Value to convert
150/// @return The hexadecimal string, is empty in case of an error.
151// ---------------------------------------------------------------------------------
152template <class T>
153AI_FORCE_INLINE std::string ai_decimal_to_hexa(T toConvert) {
154 std::string result;
155 std::stringstream ss;
156 ss << std::hex << toConvert;
157 ss >> result;
158
159 for (size_t i = 0; i < result.size(); ++i) {
160 result[i] = (char)toupper(c: (unsigned char)result[i]);
161 }
162
163 return result;
164}
165
166// ---------------------------------------------------------------------------------
167/// @brief translate RGBA to String
168/// @param r aiColor.r
169/// @param g aiColor.g
170/// @param b aiColor.b
171/// @param a aiColor.a
172/// @param with_head #
173/// @return The hexadecimal string, is empty in case of an error.
174// ---------------------------------------------------------------------------------
175AI_FORCE_INLINE std::string ai_rgba2hex(int r, int g, int b, int a, bool with_head) {
176 std::stringstream ss;
177 if (with_head) {
178 ss << "#";
179 }
180 ss << std::hex << std::setfill('0') << std::setw(8) << (r << 24 | g << 16 | b << 8 | a);
181
182 return ss.str();
183}
184
185// ---------------------------------------------------------------------------------
186/// @brief Performs a trim from start (in place)
187/// @param s string to trim.
188// ---------------------------------------------------------------------------------
189AI_FORCE_INLINE void ai_trim_left(std::string &s) {
190 s.erase(first: s.begin(), last: std::find_if(first: s.begin(), last: s.end(), pred: [](unsigned char ch) {
191 return !std::isspace(ch);
192 }));
193}
194
195// ---------------------------------------------------------------------------------
196/// @brief Performs a trim from end (in place).
197/// @param s string to trim.
198// ---------------------------------------------------------------------------------
199AI_FORCE_INLINE void ai_trim_right(std::string &s) {
200 s.erase(first: std::find_if(first: s.rbegin(), last: s.rend(), pred: [](unsigned char ch) {
201 return !std::isspace(ch);
202 }).base(), last: s.end());
203}
204
205// ---------------------------------------------------------------------------------
206/// @brief Performs a trim from both ends (in place).
207/// @param s string to trim.
208// ---------------------------------------------------------------------------------
209AI_FORCE_INLINE std::string ai_trim(std::string &s) {
210 std::string out(s);
211 ai_trim_left(s&: out);
212 ai_trim_right(s&: out);
213
214 return out;
215}
216
217// ---------------------------------------------------------------------------------
218/// @brief Performs a to lower operation onto on single character.
219/// @param in The character
220/// @return the character as lower-case.
221// ---------------------------------------------------------------------------------
222template <class char_t>
223AI_FORCE_INLINE char_t ai_tolower(char_t in) {
224 return (in >= (char_t)'A' && in <= (char_t)'Z') ? (char_t)(in + 0x20) : in;
225}
226
227// ---------------------------------------------------------------------------------
228/// @brief Performs a ToLower-operation and return the lower-case string.
229/// @param in The incoming string.
230/// @return The string as lowercase.
231// ---------------------------------------------------------------------------------
232AI_FORCE_INLINE std::string ai_tolower(const std::string &in) {
233 std::string out(in);
234 ai_trim_left(s&: out);
235 ai_trim_right(s&: out);
236 std::transform(first: out.begin(), last: out.end(), result: out.begin(), unary_op: [](unsigned char c) { return ai_tolower(in: c); });
237 return out;
238}
239
240// ---------------------------------------------------------------------------------
241/// @brief Performs a to upper operation onto on single character.
242/// @param in The character
243/// @return the character as upper-case.
244// ---------------------------------------------------------------------------------
245template <class char_t>
246AI_FORCE_INLINE char_t ai_toupper(char_t in) {
247 return (in >= (char_t)'a' && in <= (char_t)'z') ? (char_t)(in - 0x20) : in;
248}
249
250// ---------------------------------------------------------------------------------
251/// @brief Performs a ToLower-operation and return the upper-case string.
252/// @param in The incoming string.
253/// @return The string as uppercase.
254// ---------------------------------------------------------------------------------
255AI_FORCE_INLINE std::string ai_str_toupper(const std::string &in) {
256 std::string out(in);
257 std::transform(first: out.begin(), last: out.end(), result: out.begin(), unary_op: [](char c) { return ai_toupper(in: c); });
258 return out;
259}
260
261// ---------------------------------------------------------------------------------
262/// @brief Make a string printable by replacing all non-printable characters with
263/// the specified placeholder character.
264/// @param in The incoming string.
265/// @param placeholder Placeholder character, default is a question mark.
266/// @return The string, with all non-printable characters replaced.
267// ---------------------------------------------------------------------------------
268AI_FORCE_INLINE std::string ai_str_toprintable(const std::string &in, char placeholder = '?') {
269 std::string out(in);
270 std::transform(first: out.begin(), last: out.end(), result: out.begin(), unary_op: [placeholder] (unsigned char c) {
271 return isprint(c) ? (char)c : placeholder;
272 });
273 return out;
274}
275
276// ---------------------------------------------------------------------------------
277/// @brief Make a string printable by replacing all non-printable characters with
278/// the specified placeholder character.
279/// @param in The incoming string.
280/// @param len The length of the incoming string.
281/// @param placeholder Placeholder character, default is a question mark.
282/// @return The string, with all non-printable characters replaced. Will return an
283/// empty string if in is null or len is <= 0.
284// ---------------------------------------------------------------------------------
285AI_FORCE_INLINE std::string ai_str_toprintable(const char *in, int len, char placeholder = '?') {
286 return (in && len > 0) ? ai_str_toprintable(in: std::string(in, len), placeholder) : std::string();
287}
288
289#endif // INCLUDED_AI_STRINGUTILS_H
290

source code of qtquick3d/src/3rdparty/assimp/src/include/assimp/StringUtils.h