Blender V4.3
usd_utils.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "usd_utils.hh"
6
7#include "BLI_string_utf8.h"
8
9#include <pxr/base/tf/stringUtils.h>
10#if PXR_VERSION >= 2403
11# include <pxr/base/tf/unicodeUtils.h>
12#endif
13
14namespace blender::io::usd {
15
16std::string make_safe_name(const std::string &name, [[maybe_unused]] bool allow_unicode)
17{
18#if PXR_VERSION >= 2403
19 if (!allow_unicode) {
20 return pxr::TfMakeValidIdentifier(name);
21 }
22
23 if (name.empty()) {
24 return "_";
25 }
26
27 std::string buf;
28 buf.resize(name.size()); /* We won't be exceeding the size of the incoming string. */
29
30 bool first = true;
31 size_t offset = 0;
32 for (auto cp : pxr::TfUtf8CodePointView{name}) {
33 constexpr pxr::TfUtf8CodePoint cp_underscore = pxr::TfUtf8CodePointFromAscii('_');
34 const bool cp_allowed = first ? (cp == cp_underscore || pxr::TfIsUtf8CodePointXidStart(cp)) :
35 pxr::TfIsUtf8CodePointXidContinue(cp);
36 if (!cp_allowed) {
37 offset += BLI_str_utf8_from_unicode(uint32_t('_'), buf.data() + offset, buf.size() - offset);
38 }
39 else {
40 offset += BLI_str_utf8_from_unicode(cp.AsUInt32(), buf.data() + offset, buf.size() - offset);
41 }
42
43 first = false;
44 }
45
46 /* Ensure the returned string is sized exactly to the number of required bytes. */
47 buf.resize(offset);
48 return buf;
49#else
50 return pxr::TfMakeValidIdentifier(name);
51#endif
52}
53
54} // namespace blender::io::usd
size_t BLI_str_utf8_from_unicode(unsigned int c, char *dst, size_t dst_maxncpy) ATTR_NONNULL(2)
std::string make_safe_name(const std::string &name, bool allow_unicode)
Definition usd_utils.cc:16
unsigned int uint32_t
Definition stdint.h:80