| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/**************************************************************************** |
| 4 |
* Copyright (c) 2022 Zheng, Lei (realthunder) <[email protected]>* |
| 5 |
* Copyright (c) 2023 FreeCAD Project Association * |
| 6 |
* * |
| 7 |
* This file is part of FreeCAD. * |
| 8 |
* * |
| 9 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 10 |
* under the terms of the GNU Lesser General Public License as * |
| 11 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 12 |
* License, or (at your option) any later version. * |
| 13 |
* * |
| 14 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 17 |
* Lesser General Public License for more details. * |
| 18 |
* * |
| 19 |
* You should have received a copy of the GNU Lesser General Public * |
| 20 |
* License along with FreeCAD. If not, see * |
| 21 |
* <https://www.gnu.org/licenses/>. * |
| 22 |
* * |
| 23 |
***************************************************************************/ |
| 24 |
|
| 25 |
// NOLINTNEXTLINE |
| 26 |
|
| 27 |
#include <cstdlib> |
| 28 |
#include <unordered_set> |
| 29 |
|
| 30 |
#include "IndexedName.h" |
| 31 |
|
| 32 |
using namespace Data; |
| 33 |
|
| 34 |
/// Check whether the input character is an underscore or an ASCII letter a-Z or A-Z |
| 35 |
inline bool isInvalidChar(char test) |
| 36 |
{ |
| 37 |
return test != '_' && (test < 'a' || test > 'z') && (test < 'A' || test > 'Z'); |
| 38 |
} |
| 39 |
|
| 40 |
/// Get the integer suffix of name. Returns a tuple of (suffix, suffixPosition). Calling code |
| 41 |
/// should check to ensure that suffixPosition is not equal to nameLength (in which case there was |
| 42 |
/// no suffix). |
| 43 |
/// |
| 44 |
/// \param name The name to check |
| 45 |
/// \param nameLength The length of the string in name |
| 46 |
/// \returns An integer pair of the suffix itself and the position of that suffix in name |
| 47 |
std::pair<int, int> getIntegerSuffix(const char* name, int nameLength) |
| 48 |
{ |
| 49 |
int suffixPosition {nameLength - 1}; |
| 50 |
|
| 51 |
for (; suffixPosition >= 0; --suffixPosition) { |
| 52 |
// When we support C++20 we can use std::span<> to eliminate the clang-tidy warning |
| 53 |
// NOLINTNEXTLINE cppcoreguidelines-pro-bounds-pointer-arithmetic |
| 54 |
if (!isdigit(name[suffixPosition])) { |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
++suffixPosition; |
| 59 |
int suffix {0}; |
| 60 |
if (suffixPosition < nameLength) { |
| 61 |
// When we support C++20 we can use std::span<> to eliminate the clang-tidy warning |
| 62 |
// NOLINTNEXTLINE cppcoreguidelines-pro-bounds-pointer-arithmetic |
| 63 |
suffix = std::atoi(name + suffixPosition); |
| 64 |
} |
| 65 |
return std::make_pair(suffix, suffixPosition); |
| 66 |
} |
| 67 |
|
| 68 |
void IndexedName::set(const char* name, |
| 69 |
int length, |
| 70 |
const std::vector<const char*>& allowedNames, |
| 71 |
bool allowOthers) |
| 72 |
{ |
| 73 |
// Storage for names that we weren't given external storage for |
| 74 |
static std::unordered_set<ByteArray, ByteArrayHasher> NameSet; |
| 75 |
|
| 76 |
if (length < 0) { |
| 77 |
length = static_cast<int>(std::strlen(name)); |
| 78 |
} |
| 79 |
// Name typically ends with an integer: find that integer |
| 80 |
auto [suffix, suffixPosition] = getIntegerSuffix(name, length); |
| 81 |
if (suffixPosition < length) { |
| 82 |
this->index = suffix; |
| 83 |
} |
| 84 |
|
| 85 |
// Make sure that every character is either an ASCII letter (upper or lowercase), or an |
| 86 |
// underscore. If any other character appears, reject the entire string. |
| 87 |
// When we support C++20 we can use std::span<> to eliminate the clang-tidy warning |
| 88 |
// NOLINTNEXTLINE cppcoreguidelines-pro-bounds-pointer-arithmetic |
| 89 |
if (std::any_of(name, name + suffixPosition, isInvalidChar)) { |
| 90 |
this->type = ""; |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
// If a list of allowedNames was provided, see if our set name matches one of those |
| 95 |
// allowedNames: if it does, reference that memory location and return. |
| 96 |
for (const auto* typeName : allowedNames) { |
| 97 |
if (std::strncmp(name, typeName, suffixPosition) == 0) { |
| 98 |
this->type = typeName; |
| 99 |
return; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
// If the type was NOT in the list of allowedNames, but the caller has set the allowOthers flag |
| 104 |
// to true, then add the new type to the static NameSet (if it is not already there). |
| 105 |
if (allowOthers) { |
| 106 |
auto res = NameSet.insert(ByteArray(QByteArray::fromRawData(name, suffixPosition))); |
| 107 |
if (res.second /*The insert succeeded (the type was new)*/) { |
| 108 |
// Make sure that the data in the set is a unique (unshared) copy of the text |
| 109 |
res.first->ensureUnshared(); |
| 110 |
} |
| 111 |
this->type = res.first->bytes.constData(); |
| 112 |
} |
| 113 |
else { |
| 114 |
// The passed-in type is not in the allowed list, and allowOthers was not true, so don't |
| 115 |
// store the type |
| 116 |
this->type = ""; |
| 117 |
} |
| 118 |
} |