11#include <pxr/base/tf/stringUtils.h>
12#include <pxr/base/tf/unicodeUtils.h>
13#include <pxr/usd/usd/prim.h>
14#include <pxr/usd/usd/stage.h>
18static bool is_safe_char(
const pxr::TfUtf8CodePoint cp,
bool is_first,
bool allow_unicode)
20 constexpr pxr::TfUtf8CodePoint cp_underscore = pxr::TfUtf8CodePointFromAscii(
'_');
22 if (cp == cp_underscore) {
27 return is_first ? pxr::TfIsUtf8CodePointXidStart(cp) : pxr::TfIsUtf8CodePointXidContinue(cp);
30 constexpr uint32_t cp_A = pxr::TfUtf8CodePointFromAscii(
'A').AsUInt32();
31 constexpr uint32_t cp_Z = pxr::TfUtf8CodePointFromAscii(
'Z').AsUInt32();
32 constexpr uint32_t cp_a = pxr::TfUtf8CodePointFromAscii(
'a').AsUInt32();
33 constexpr uint32_t cp_z = pxr::TfUtf8CodePointFromAscii(
'z').AsUInt32();
34 constexpr uint32_t cp_0 = pxr::TfUtf8CodePointFromAscii(
'0').AsUInt32();
35 constexpr uint32_t cp_9 = pxr::TfUtf8CodePointFromAscii(
'9').AsUInt32();
37 const uint32_t cp_u32 = cp.AsUInt32();
38 const bool is_letter = (cp_u32 >= cp_A && cp_u32 <= cp_Z) || (cp_u32 >= cp_a && cp_u32 <= cp_z);
39 const bool is_digit = cp_u32 >= cp_0 && cp_u32 <= cp_9;
40 return is_first ? is_letter : (is_letter || is_digit);
49 const bool has_leading_digit = std::isdigit(name[0]);
50 const bool need_leading_underscore = has_leading_digit;
55 const int64_t adjust = (need_leading_underscore ? 1 : 0);
62 if (need_leading_underscore) {
68 for (
auto cp : pxr::TfUtf8CodePointView{name}) {
69 const bool cp_allowed =
is_safe_char(cp, first, allow_unicode);
80 return {buf.
data(), offset};
91 const std::string original(name);
92 std::vector<std::string> tokens = pxr::TfStringSplit(original,
":");
97 std::string safe_name;
98 for (
size_t i = 0; i < tokens.size(); i++) {
99 const std::string &token = tokens[i];
101 if (i != tokens.size() - 1) {
111 std::string unique_path = path;
113 while (stage->GetPrimAtPath(pxr::SdfPath(unique_path)).IsValid()) {
114 unique_path = path + std::to_string(suffix++);
117 return pxr::SdfPath(unique_path);
constexpr int64_t size() const
constexpr T * data() const
constexpr bool is_empty() const
constexpr int64_t size() const
#define storage(slot, qualifiers)
static bool is_safe_char(const pxr::TfUtf8CodePoint cp, bool is_first, bool allow_unicode)
pxr::SdfPath get_unique_path(pxr::UsdStageRefPtr stage, const std::string &path)
std::string make_safe_name(const StringRef name, bool allow_unicode)
static std::string make_safe_identifier(const StringRef name, bool allow_unicode)
std::string make_safe_primvar_name(const StringRef name, bool allow_unicode)
size_t BLI_str_utf8_from_unicode(unsigned int c, char *dst, size_t dst_maxncpy) ATTR_NONNULL(2)