Debian Codegraph @

src/App/MappedName.h

raw
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 #ifndef APP_MAPPED_NAME_H
26 #define APP_MAPPED_NAME_H
27
28 #include <memory>
29 #include <string>
30
31 #include <boost/algorithm/string/predicate.hpp>
32
33 #include <QByteArray>
34 #include <QHash>
35 #include <QVector>
36 #include <utility>
37
38 #include "ElementNamingUtils.h"
39 #include "IndexedName.h"
40 #include "StringHasher.h"
41
42
43 namespace Data
44 {
45
46 // NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
47
48 /// The MappedName class maintains a two-part name: the first part ("data") is considered immutable
49 /// once created, while the second part ("postfix") can be modified/appended to by later operations.
50 /// It uses shared data when possible (see the fromRawData() members). Despite storing data and
51 /// postfix separately, they can be accessed via calls to size(), operator[], etc. as though they
52 /// were a single array.
53 class AppExport MappedName
54 {
55 public:
56 /// Create a MappedName from a C string, optionally prefixed by an element map prefix, which
57 /// will be omitted from the stored MappedName.
58 ///
59 /// \param name The new name. A deep copy is made.
60 /// \param size Optional, the length of the name string. If not provided, the string must be
61 /// null-terminated.
62 explicit MappedName(const char* name, int size = -1)
63 : raw(false)
64 {
65 if (!name) {
66 return;
67 }
68 if (boost::starts_with(name, ELEMENT_MAP_PREFIX)) {
69 name += ELEMENT_MAP_PREFIX_SIZE;
70 }
71
72 data = size < 0 ? QByteArray(name) : QByteArray(name, size);
73 }
74
75 /// Create a MappedName from a C++ std::string, optionally prefixed by an element map prefix,
76 /// which will be omitted from the stored MappedName.
77 ///
78 /// \param name The new name. A deep copy is made.
79 explicit MappedName(const std::string& nameString)
80 : raw(false)
81 {
82 auto size = nameString.size();
83 const char* name = nameString.c_str();
84 if (boost::starts_with(nameString, ELEMENT_MAP_PREFIX)) {
85 name += ELEMENT_MAP_PREFIX_SIZE;
86 size -= ELEMENT_MAP_PREFIX_SIZE;
87 }
88 data = QByteArray(name, static_cast<int>(size));
89 }
90
91 /// Create a MappedName from an IndexedName. If non-zero, the numerical part of the IndexedName
92 /// is appended as text to the MappedName. In that case the memory is *not* shared between the
93 /// original IndexedName and the MappedName.
94 explicit MappedName(const IndexedName& element)
95 : data(QByteArray::fromRawData(element.getType(),
96 static_cast<int>(qstrlen(element.getType()))))
97 , raw(true)
98 {
99 if (element.getIndex() > 0) {
100 this->data += QByteArray::number(element.getIndex());
101 this->raw = false;
102 }
103 }
104
105 explicit MappedName(const App::StringIDRef& sid)
106 : raw(false)
107 {
108 sid.toBytes(this->data);
109 }
110
111 MappedName()
112 : raw(false)
113 {}
114
115 MappedName(const MappedName& other) = default;
116
117 /// Copy constructor with start position offset and optional size. The data is *not* reused.
118 ///
119 /// \param other The MappedName to copy
120 /// \param startPosition an integer offset to start the copy from
121 /// \param size the number of bytes to copy.
122 /// \see append() for details about how the copy behaves for various sizes and start positions
123 MappedName(const MappedName& other, int startPosition, int size = -1)
124 : raw(false)
125 {
126 append(other, startPosition, size);
127 }
128
129 /// Copy constructor with additional postfix
130 ///
131 /// \param other The mapped name to copy. Its data and postfix become the new MappedName's data
132 /// \param postfix The postfix for the new MappedName
133 MappedName(const MappedName& other, const char* postfix)
134 : data(other.data + other.postfix)
135 , postfix(postfix)
136 , raw(false)
137 {}
138
139 /// Move constructor
140 MappedName(MappedName&& other) noexcept
141 : data(std::move(other.data))
142 , postfix(std::move(other.postfix))
143 , raw(other.raw)
144 {}
145
146 ~MappedName() = default;
147
148 /// Construct a MappedName from raw character data (including null characters, if size is
149 /// provided). No copy is made: the data is used in place.
150 ///
151 /// \param name The raw data to use.
152 /// \param size The number of bytes to access. If omitted, name must be null-terminated.
153 /// \return a new MappedName with name as its data.
154 static MappedName fromRawData(const char* name, int size = -1)
155 {
156 MappedName res;
157 if (name) {
158 res.data =
159 QByteArray::fromRawData(name, size >= 0 ? size : static_cast<int>(qstrlen(name)));
160 res.raw = true;
161 }
162 return res;
163 }
164
165 /// Construct a MappedName from QByteArray data (including any embedded null characters).
166 ///
167 /// \param data The original data. No copy is made, the data is shared with the other instance.
168 /// \return a new MappedName with data as its data.
169 static MappedName fromRawData(const QByteArray& data)
170 {
171 return fromRawData(data.constData(), data.size());
172 }
173
174 /// Construct a MappedName from another MappedName
175 ///
176 /// \param other The MappedName to copy from. The data is usually not copied, but in some
177 /// cases a partial copy may be made to support a slice that extends across other's data into
178 /// its postfix.
179 /// \param startPosition The position to start the reference at.
180 /// \param size The number of bytes to access. If omitted, continues from startPosition
181 /// to the end of available data (including postfix).
182 /// \return a new MappedName sharing (possibly a subset of) data with other.
183 /// \see append() for details about how the copy behaves for various sizes and start positions
184 static MappedName fromRawData(const MappedName& other, int startPosition, int size = -1)
185 {
186 if (startPosition < 0) {
187 startPosition = 0;
188 }
189
190 if (startPosition >= other.size()) {
191 return {};
192 }
193
194 if (startPosition >= other.data.size()) {
195 return {other, startPosition, size};
196 }
197
198 MappedName res;
199 res.raw = true;
200 if (size < 0) {
201 size = other.size() - startPosition;
202 }
203
204 if (size < other.data.size() - startPosition) {
205 res.data = QByteArray::fromRawData(other.data.constData() + startPosition, size);
206 }
207 else {
208 res.data = QByteArray::fromRawData(other.data.constData() + startPosition,
209 other.data.size() - startPosition);
210 size -= other.data.size() - startPosition;
211 if (size == other.postfix.size()) {
212 res.postfix = other.postfix;
213 }
214 else if (size != 0) {
215 res.postfix.append(other.postfix.constData(), size);
216 }
217 }
218 return res;
219 }
220
221 /// Share data with another MappedName
222 MappedName& operator=(const MappedName& other) = default;
223
224 /// Create a new MappedName from a std::string: the string's data is copied.
225 MappedName& operator=(const std::string& other)
226 {
227 *this = MappedName(other);
228 return *this;
229 }
230
231 /// Create a new MappedName from a const char *. The character data is copied.
232 MappedName& operator=(const char* other)
233 {
234 *this = MappedName(other);
235 return *this;
236 }
237
238
239 /// Move-construct a MappedName
240 MappedName& operator=(MappedName&& other) noexcept
241 {
242 this->data = std::move(other.data);
243 this->postfix = std::move(other.postfix);
244 this->raw = other.raw;
245 return *this;
246 }
247
248 /// Write to a stream as the name with postfix directly appended to it. Note that there is no
249 /// special handling for null or non-ASCII characters, they are simply written to the stream.
250 friend std::ostream& operator<<(std::ostream& stream, const MappedName& mappedName)
251 {
252 stream.write(mappedName.data.constData(), mappedName.data.size());
253 stream.write(mappedName.postfix.constData(), mappedName.postfix.size());
254 return stream;
255 }
256
257 /// Two MappedNames are equal if the concatenation of their data and postfix is equal. The
258 /// individual data and postfix may NOT be equal in this case.
259 bool operator==(const MappedName& other) const
260 {
261 if (this->size() != other.size()) {
262 return false;
263 }
264
265 if (this->data.size() == other.data.size()) {
266 return this->data == other.data && this->postfix == other.postfix;
267 }
268
269 const auto& smaller = this->data.size() < other.data.size() ? *this : other;
270 const auto& larger = this->data.size() < other.data.size() ? other : *this;
271
272 if (!larger.data.startsWith(smaller.data)) {
273 return false;
274 }
275
276 QByteArray tmp = QByteArray::fromRawData(larger.data.constData() + smaller.data.size(),
277 larger.data.size() - smaller.data.size());
278
279 if (!smaller.postfix.startsWith(tmp)) {
280 return false;
281 }
282
283 tmp = QByteArray::fromRawData(smaller.postfix.constData() + tmp.size(),
284 smaller.postfix.size() - tmp.size());
285
286 return tmp == larger.postfix;
287 }
288
289 bool operator!=(const MappedName& other) const
290 {
291 return !(this->operator==(other));
292 }
293
294 /// Returns a new MappedName whose data is the LHS argument's data and whose postfix is the LHS
295 /// argument's postfix with the RHS argument's data and postfix appended to it.
296 MappedName operator+(const MappedName& other) const
297 {
298 MappedName res(*this);
299 res += other;
300 return res;
301 }
302
303 /// Returns a new MappedName whose data is the LHS argument's data and whose postfix is the LHS
304 /// argument's postfix with the RHS argument appended to it. The character data is copied.
305 MappedName operator+(const char* other) const
306 {
307 MappedName res(*this);
308 res += other;
309 return res;
310 }
311
312 /// Returns a new MappedName whose data is the LHS argument's data and whose postfix is the LHS
313 /// argument's postfix with the RHS argument appended to it. The character data is copied.
314 MappedName operator+(const std::string& other) const
315 {
316 MappedName res(*this);
317 res += other;
318 return res;
319 }
320
321 /// Returns a new MappedName whose data is the LHS argument's data and whose postfix is the LHS
322 /// argument's postfix with the RHS argument appended to it.
323 MappedName operator+(const QByteArray& other) const
324 {
325 MappedName res(*this);
326 res += other;
327 return res;
328 }
329
330 /// Appends other to this instance's postfix. other must be a null-terminated C string. The
331 /// character data from the string is copied.
332 MappedName& operator+=(const char* other)
333 {
334 if (other && (other[0] != 0)) {
335 this->postfix.append(other, static_cast<int>(qstrlen(other)));
336 }
337 return *this;
338 }
339
340 /// Appends other to this instance's postfix. The character data from the string is copied.
341 MappedName& operator+=(const std::string& other)
342 {
343 if (!other.empty()) {
344 this->postfix.reserve(this->postfix.size() + static_cast<int>(other.size()));
345 this->postfix.append(other.c_str(), static_cast<int>(other.size()));
346 }
347 return *this;
348 }
349
350 /// Appends other to this instance's postfix. The data may be either copied or shared, depending
351 /// on whether this->postfix is empty (in which case the data is shared) or non-empty (in which
352 /// case it is copied).
353 MappedName& operator+=(const QByteArray& other)
354 {
355 this->postfix += other;
356 return *this;
357 }
358
359 /// Appends other to this instance's postfix, unless this is empty, in which case this acts
360 /// like operator=, and makes this instance's data equal to other's data, and this instance's
361 /// postfix equal to the other instance's postfix.
362 MappedName& operator+=(const MappedName& other)
363 {
364 append(other);
365 return *this;
366 }
367
368 /// Add dataToAppend to this MappedName. If the current name is empty, this becomes the new
369 /// data element. If this MappedName already has data, then the data is appended to the postfix.
370 ///
371 /// \param dataToAppend The data to add. A deep copy is made.
372 /// \param size The number of bytes to copy. If omitted, dataToAppend must be null-terminated.
373 void append(const char* dataToAppend, int size = -1)
374 {
375 if (dataToAppend && (size != 0)) {
376 if (size < 0) {
377 size = static_cast<int>(qstrlen(dataToAppend));
378 }
379 if (empty()) {
380 this->data.append(dataToAppend, size);
381 }
382 else {
383 this->postfix.append(dataToAppend, size);
384 }
385 }
386 }
387
388 /// Treating both this and other as single continuous byte arrays, append other to this. If this
389 /// is empty, then other's data is shared with this instance's data beginning at startPosition.
390 /// If this is *not* empty, then all data is appended to the postfix. If the copy crosses the
391 /// boundary between other's data and its postfix, then if this instance was empty, the new
392 /// data stops where other's data stops, and the remainder of the copy is placed in the suffix.
393 /// Otherwise the copy simply continues as though there was no distinction between other's
394 /// data and suffix.
395 ///
396 /// \param other The MappedName to obtain the data from. The data is shared when possible,
397 /// depending on the details of startPosition, size, and this->empty().
398 /// \param startPosition The byte to start the copy at. Must be a positive non-zero integer less
399 /// than the length of other's combined data + postfix.
400 /// \param size The number of bytes to copy. Must not overrun the end of other's combined data
401 /// storage when taking startPosition into consideration.
402 void append(const MappedName& other, int startPosition = 0, int size = -1)
403 {
404 // enforce 0 <= startPosition <= other.size
405 if (startPosition < 0) {
406 startPosition = 0;
407 }
408 else if (startPosition > other.size()) {
409 return;
410 }
411
412 // enforce 0 <= size <= other.size - startPosition
413 if (size < 0 || size > other.size() - startPosition) {
414 size = other.size() - startPosition;
415 }
416
417
418 if (startPosition < other.data.size()) // if starting inside data
419 {
420 int count = size;
421 // make sure count doesn't exceed data size and end up in postfix
422 if (count > other.data.size() - startPosition) {
423 count = other.data.size() - startPosition;
424 }
425
426 // if this is empty append in data else append in postfix
427 if (startPosition == 0 && count == other.data.size() && this->empty()) {
428 this->data = other.data;
429 this->raw = other.raw;
430 }
431 else {
432 append(other.data.constData() + startPosition, count);
433 }
434
435 // setup startPosition and count to continue appending the remainder to postfix
436 startPosition = 0;
437 size -= count;
438 }
439 else // else starting inside postfix
440 {
441 startPosition -= other.data.size();
442 }
443
444 // if there is still data to be added to postfix
445 if (size != 0) {
446 if (startPosition == 0 && size == other.postfix.size()) {
447 if (this->empty()) {
448 this->data = other.postfix;
449 }
450 else if (this->postfix.isEmpty()) {
451 this->postfix = other.postfix;
452 }
453 else {
454 this->postfix += other.postfix;
455 }
456 }
457 else {
458 append(other.postfix.constData() + startPosition, size);
459 }
460 }
461 }
462
463 /// Create a std::string from this instance, starting at startPosition, and extending len bytes.
464 ///
465 /// \param startPosition The offset into the data
466 /// \param len The number of bytes to output
467 /// \return A new std::string containing the bytes copied from this instance's data and postfix
468 /// (depending on startPosition and len).
469 /// \note No effort is made to ensure that these are valid ASCII characters, and it is possible
470 /// the data includes embedded null characters, non-ASCII data, etc.
471 std::string toString(int startPosition = 0, int len = -1) const
472 {
473 std::string res;
474 return appendToBuffer(res, startPosition, len);
475 }
476
477 /// Given a (possibly non-empty) std::string buffer, append this instance to it, starting at a
478 /// specified position, and continuing for a specified number of bytes.
479 ///
480 /// \param buffer The string buffer to append to.
481 /// \param startPosition The position in this instance's data/postfix to start at (defaults to
482 /// zero). Must be less than the total length of the data plus the postfix.
483 /// \param len The number of bytes to append. If omitted, defaults to appending all available
484 /// data starting at startPosition.
485 /// \return A pointer to the beginning of the appended data within buffer.
486 /// \note No effort is made to ensure that these are valid ASCII characters, and it is possible
487 /// the data includes embedded null characters, non-ASCII data, etc.
488 const char* appendToBuffer(std::string& buffer, int startPosition = 0, int len = -1) const
489 {
490 std::size_t offset = buffer.size();
491 int count = this->size();
492 if (startPosition < 0) {
493 startPosition = 0;
494 }
495 else if (startPosition >= count) {
496 return buffer.c_str() + buffer.size();
497 }
498 if (len < 0 || len > count - startPosition) {
499 len = count - startPosition;
500 }
501 buffer.reserve(buffer.size() + len);
502 if (startPosition < this->data.size()) {
503 count = this->data.size() - startPosition;
504 if (len < count) {
505 count = len;
506 }
507 buffer.append(this->data.constData() + startPosition, count);
508 len -= count;
509 }
510 buffer.append(this->postfix.constData(), len);
511 return buffer.c_str() + offset;
512 }
513
514 // if offset is inside data return data, if offset is > data.size
515 //(ends up in postfix) return postfix
516 const char* toConstString(int offset, int& size) const
517 {
518 if (offset < 0) {
519 offset = 0;
520 }
521 if (offset > this->data.size()) {
522 offset -= this->data.size();
523 if (offset > this->postfix.size()) {
524 size = 0;
525 return "";
526 }
527 size = this->postfix.size() - offset;
528 return this->postfix.constData() + offset;
529 }
530 size = this->data.size() - offset;
531 return this->data.constData() + offset;
532 }
533
534 /// Get access to raw byte data. When possible, data is shared between this instance and the
535 /// returned QByteArray. If the combination of offset and size results in data that crosses the
536 /// boundary between this->data and this->postfix, the data must be copied in order to provide
537 /// access as a continuous array of bytes.
538 ///
539 /// \param offset The start position of the raw data access.
540 /// \param size The number of bytes to access. If omitted, the resulting QByteArray includes
541 /// everything starting from offset to the end, including any postfix data.
542 /// \return A new QByteArray that shares data with this instance if possible, or is a new copy
543 /// if required by offset and size.
544 QByteArray toRawBytes(int offset = 0, int size = -1) const
545 {
546 if (offset < 0) {
547 offset = 0;
548 }
549 if (offset >= this->size()) {
550 return {};
551 }
552 if (size < 0 || size > this->size() - offset) {
553 size = this->size() - offset;
554 }
555 if (offset >= this->data.size()) {
556 offset -= this->data.size();
557 return QByteArray::fromRawData(this->postfix.constData() + offset, size);
558 }
559 if (size <= this->data.size() - offset) {
560 return QByteArray::fromRawData(this->data.constData() + offset, size);
561 }
562
563 QByteArray res(this->data.constData() + offset, this->data.size() - offset);
564 res.append(this->postfix.constData(), size - this->data.size() + offset);
565 return res;
566 }
567
568 /// Direct access to the stored QByteArray of data. A copy is never made.
569 const QByteArray& dataBytes() const
570 {
571 return this->data;
572 }
573
574 /// Direct access to the stored QByteArray of postfix. A copy is never made.
575 const QByteArray& postfixBytes() const
576 {
577 return this->postfix;
578 }
579
580 /// Convenience function providing access to the pointer to the beginning of the postfix data.
581 const char* constPostfix() const
582 {
583 return this->postfix.constData();
584 }
585
586 // No constData() because 'data' is allowed to contain raw data, which may not end with 0.
587
588 /// Provide access to the content of this instance. If either postfix or data is empty, no copy
589 /// is made and the original QByteArray is returned, sharing data with this instance. If this
590 /// instance contains both data and postfix, a new QByteArray is created and stores a copy of
591 /// the data and postfix concatenated together.
592 QByteArray toBytes() const
593 {
594 if (this->postfix.isEmpty()) {
595 return this->data;
596 }
597 if (this->data.isEmpty()) {
598 return this->postfix;
599 }
600 return this->data + this->postfix;
601 }
602
603 /// Create an IndexedName from the data portion of this MappedName. If this data has a postfix,
604 /// the function returns an empty IndexedName. The function will fail if this->data contains
605 /// anything other than the ASCII letter a-z, A-Z, and the underscore, with an optional integer
606 /// suffix, returning an empty IndexedName (e.g. an IndexedName that evaluates to boolean
607 /// false and isNull() == true).
608 ///
609 /// \return a new IndexedName that shares its data with this instance's data member.
610 IndexedName toIndexedName() const
611 {
612 if (this->postfix.isEmpty()) {
613 return IndexedName(this->data);
614 }
615 return IndexedName();
616 }
617
618 /// Create and return a string version of this MappedName prefixed by the ComplexGeoData element
619 /// map prefix, if this MappedName cannot be converted to an indexed name.
620 std::string toPrefixedString() const
621 {
622 std::string res;
623 appendToBufferWithPrefix(res);
624 return res;
625 }
626
627 /// Append this MappedName to a provided string buffer, including the ComplexGeoData element
628 /// map prefix if the MappedName cannot be converted to an IndexedName.
629 ///
630 /// \param buf A (possibly non-empty) string to append this MappedName to.
631 /// \return A pointer to the beginning of the buffer.
632 const char* appendToBufferWithPrefix(std::string& buf) const
633 {
634 if (!toIndexedName()) {
635 buf += ELEMENT_MAP_PREFIX;
636 }
637 appendToBuffer(buf);
638 return buf.c_str();
639 }
640
641 /// Equivalent to C++20 operator<=>. Performs byte-by-byte comparison of this and other,
642 /// starting at the first byte and continuing through both data and postfix, ignoring which is
643 /// which. If the combined data and postfix members are of unequal size but start with the same
644 /// data, the shorter array is considered "less than" the longer.
645 int compare(const MappedName& other) const
646 {
647 int thisSize = this->size();
648 int otherSize = other.size();
649 for (int i = 0, count = std::min(thisSize, otherSize); i < count; ++i) {
650 char thisChar = this->operator[](i);
651 char otherChar = other[i];
652 if (thisChar < otherChar) {
653 return -1;
654 }
655 if (thisChar > otherChar) {
656 return 1;
657 }
658 }
659 if (thisSize < otherSize) {
660 return -1;
661 }
662 if (thisSize > otherSize) {
663 return 1;
664 }
665 return 0;
666 }
667
668 /// \see compare()
669 bool operator<(const MappedName& other) const
670 {
671 return compare(other) < 0;
672 }
673
674 /// Treat this MappedName as a single continuous array of bytes, beginning with data and
675 /// continuing through postfix. No bounds checking is performed when compiled in release mode.
676 char operator[](int index) const
677 {
678 if (index < 0) {
679 index = 0;
680 }
681 if (index >= this->data.size()) {
682 if (index - this->data.size() > this->postfix.size() - 1) {
683 index = this->postfix.size() - 1;
684 }
685 return this->postfix[index - this->data.size()];
686 }
687 return this->data[index];
688 }
689
690 /// Treat this MappedName as a single continuous array of bytes, returning the combined size
691 /// of the data and postfix.
692 int size() const
693 {
694 return this->data.size() + this->postfix.size();
695 }
696
697 /// Treat this MappedName as a single continuous array of bytes, returning true only if both
698 /// data and prefix are empty.
699 bool empty() const
700 {
701 return this->data.isEmpty() && this->postfix.isEmpty();
702 }
703
704 /// Returns true if this is shared data, or false if a unique copy has been made.
705 /// It is safe to access data only if it has been copied prior. To force a copy
706 /// please \see compact()
707 bool isRaw() const
708 {
709 return this->raw;
710 }
711
712 /// If this is shared data, a new unshared copy is made and returned. If it is already unshared
713 /// no new copy is made, a new instance is returned that shares is data with the current
714 /// instance.
715 MappedName copy() const
716 {
717 if (!this->raw) {
718 return *this;
719 }
720 MappedName res;
721 res.data.append(this->data.constData(), this->data.size());
722 res.postfix = this->postfix;
723 return res;
724 }
725
726 /// Ensure that this data is unshared, making a copy if necessary.
727 void compact() const;
728
729 /// Boolean conversion is the inverse of empty(), returning true if there is data in either the
730 /// data or postfix, and false if there is nothing in either.
731 explicit operator bool() const
732 {
733 return !empty();
734 }
735
736 /// Reset this instance, clearing anything in data and postfix.
737 void clear()
738 {
739 this->data.clear();
740 this->postfix.clear();
741 this->raw = false;
742 }
743
744 /// Find a string of characters in this MappedName. The bytes must occur either entirely in the
745 /// data, or entirely in the postfix: a string that overlaps the two will not be found.
746 ///
747 /// \param searchTarget A null-terminated C string to search for.
748 /// \param startPosition A byte offset to start the search at.
749 /// \return The position of the target in this instance, or -1 if the target is not found.
750 int find(const char* searchTarget, int startPosition = 0) const
751 {
752 if (!searchTarget) {
753 return -1;
754 }
755 if (startPosition < 0) {
756 startPosition = 0;
757 }
758 if (startPosition < this->data.size()) {
759 int res = this->data.indexOf(searchTarget, startPosition);
760 if (res >= 0) {
761 return res;
762 }
763 startPosition = 0;
764 }
765 else {
766 startPosition -= this->data.size();
767 }
768 int res = this->postfix.indexOf(searchTarget, startPosition);
769 if (res < 0) {
770 return res;
771 }
772 return res + this->data.size();
773 }
774
775 /// Find a string of characters in this MappedName. The bytes must occur either entirely in the
776 /// data, or entirely in the postfix: a string that overlaps the two will not be found.
777 ///
778 /// \param searchTarget A string to search for.
779 /// \param startPosition A byte offset to start the search at.
780 /// \return The position of the target in this instance, or -1 if the target is not found.
781 int find(const std::string& searchTarget, int startPosition = 0) const
782 {
783 return find(searchTarget.c_str(), startPosition);
784 }
785
786 /// Find a string of characters in this MappedName, starting at the back of postfix and
787 /// proceeding in reverse through the data. The bytes must occur either entirely in the
788 /// data, or entirely in the postfix: a string that overlaps the two will not be found.
789 ///
790 /// \param searchTarget A null-terminated C string to search for.
791 /// \param startPosition A byte offset to start the search at. Negative numbers are supported
792 /// and count back from the end of the concatenated data (as in QByteArray::lastIndexOf()).
793 /// \return The position of the target in this instance, or -1 if the target is not found.
794 int rfind(const char* searchTarget, int startPosition = -1) const
795 {
796 if (!searchTarget) {
797 return -1;
798 }
799 if (startPosition < 0 || startPosition >= this->data.size()) {
800 if (startPosition >= data.size()) {
801 startPosition -= data.size();
802 }
803 int res = this->postfix.lastIndexOf(searchTarget, startPosition);
804 if (res >= 0) {
805 return res + this->data.size();
806 }
807 startPosition = -1;
808 }
809 return this->data.lastIndexOf(searchTarget, startPosition);
810 }
811
812 /// Find a string in this MappedName, starting at the back of postfix and proceeding in reverse
813 /// through the data. The bytes must occur either entirely in the data, or entirely in the
814 /// postfix: a string that overlaps the two will not be found.
815 ///
816 /// \param searchTarget A null-terminated C string to search for.
817 /// \param startPosition A byte offset to start the search at. Negative numbers are supported
818 /// and count back from the end of the concatenated data (as in QByteArray::lastIndexOf()).
819 /// \return The position of the target in this instance, or -1 if the target is not found.
820 int rfind(const std::string& searchTarget, int startPosition = -1) const
821 {
822 return rfind(searchTarget.c_str(), startPosition);
823 }
824
825 /// Returns true if this MappedName ends with the search target. If there is a postfix, only the
826 /// postfix is considered. If not, then only the data is considered. A search string that
827 /// overlaps the two will not be found.
828 bool endsWith(const char* searchTarget) const
829 {
830 if (!searchTarget) {
831 return false;
832 }
833 if (this->postfix.size() != 0) {
834 return this->postfix.endsWith(searchTarget);
835 }
836 return this->data.endsWith(searchTarget);
837 }
838
839 /// Returns true if this MappedName ends with the search target. If there is a postfix, only the
840 /// postfix is considered. If not, then only the data is considered. A search string that
841 /// overlaps the two will not be found.
842 bool endsWith(const std::string& searchTarget) const
843 {
844 return endsWith(searchTarget.c_str());
845 }
846
847 /// Returns true if this MappedName starts with the search target. If there is a postfix, only
848 /// the postfix is considered. If not, then only the data is considered. A search string that
849 /// overlaps the two will not be found.
850 ///
851 /// \param searchTarget An array of bytes to match
852 /// \param offset An offset to perform the match at
853 /// \return True if this MappedName begins with the target bytes
854 bool startsWith(const QByteArray& searchTarget, int offset = 0) const
855 {
856 if (searchTarget.size() > size() - offset) {
857 return false;
858 }
859 if ((offset != 0)
860 || ((this->data.size() != 0) && this->data.size() < searchTarget.size())) {
861 return toRawBytes(offset, searchTarget.size()) == searchTarget;
862 }
863 if (this->data.size() != 0) {
864 return this->data.startsWith(searchTarget);
865 }
866 return this->postfix.startsWith(searchTarget);
867 }
868
869 /// Returns true if this MappedName starts with the search target. If there is a postfix, only
870 /// the postfix is considered. If not, then only the data is considered. A search string that
871 /// overlaps the two will not be found.
872 ///
873 /// \param searchTarget An array of bytes to match
874 /// \param offset An offset to perform the match at
875 /// \return True if this MappedName begins with the target bytes
876 bool startsWith(const char* searchTarget, int offset = 0) const
877 {
878 if (!searchTarget) {
879 return false;
880 }
881 return startsWith(
882 QByteArray::fromRawData(searchTarget, static_cast<int>(qstrlen(searchTarget))),
883 offset);
884 }
885
886 /// Returns true if this MappedName starts with the search target. If there is a postfix, only
887 /// the postfix is considered. If not, then only the data is considered. A search string that
888 /// overlaps the two will not be found.
889 ///
890 /// \param searchTarget A string to match
891 /// \param offset An offset to perform the match at
892 /// \return True if this MappedName begins with the target bytes
893 bool startsWith(const std::string& searchTarget, int offset = 0) const
894 {
895 return startsWith(
896 QByteArray::fromRawData(searchTarget.c_str(), static_cast<int>(searchTarget.size())),
897 offset);
898 }
899
900 /// Extract tagOut and other information from a encoded element name
901 ///
902 /// \param tagOut: optional pointer to receive the extracted tagOut
903 /// \param lenOut: optional pointer to receive the length field after the tagOut field.
904 /// This gives the length of the previous hashed element name starting
905 /// from the beginning of the give element name.
906 /// \param postfixOut: optional pointer to receive the postfixOut starting at the found tagOut
907 /// field. \param typeOut: optional pointer to receive the element typeOut character \param
908 /// negative: return negative tagOut as it is. If disabled, then always return positive tagOut.
909 /// Negative tagOut is sometimes used for element disambiguation.
910 /// \param recursive: recursively find the last non-zero tagOut
911 ///
912 /// \return Return the end position of the tagOut field, or return -1 if not found.
913 int findTagInElementName(long* tagOut = nullptr,
914 int* lenOut = nullptr,
915 std::string* postfixOut = nullptr,
916 char* typeOut = nullptr,
917 bool negative = false,
918 bool recursive = true) const;
919
920 /// Get a hash for this MappedName
921 std::size_t hash() const
922 {
923 return qHash(data, qHash(postfix));
924 }
925
926 private:
927 QByteArray data;
928 QByteArray postfix;
929 bool raw;
930 };
931
932
933 using ElementIDRefs = QVector<::App::StringIDRef>;
934
935 struct MappedNameRef
936 {
937 MappedName name;
938 ElementIDRefs sids;
939 std::unique_ptr<MappedNameRef> next;
940
941 MappedNameRef() = default;
942
943 ~MappedNameRef() = default;
944
945 MappedNameRef(MappedName name, ElementIDRefs sids = ElementIDRefs())
946 : name(std::move(name))
947 , sids(std::move(sids))
948 {
949 compact();
950 }
951
952 MappedNameRef(const MappedNameRef& other)
953 : name(other.name)
954 , sids(other.sids)
955 {}
956
957 MappedNameRef(MappedNameRef&& other) noexcept
958 : name(std::move(other.name))
959 , sids(std::move(other.sids))
960 , next(std::move(other.next))
961 {}
962
963 MappedNameRef& operator=(const MappedNameRef& other) noexcept
964 {
965 name = other.name;
966 sids = other.sids;
967 return *this;
968 }
969
970 MappedNameRef& operator=(MappedNameRef&& other) noexcept
971 {
972 name = std::move(other.name);
973 sids = std::move(other.sids);
974 next = std::move(other.next);
975 return *this;
976 }
977
978 explicit operator bool() const
979 {
980 return !name.empty();
981 }
982
983 void append(const MappedName& _name, const ElementIDRefs _sids = ElementIDRefs())
984 {
985 if (!_name) {
986 return;
987 }
988 if (!this->name) {
989 this->name = _name;
990 this->sids = _sids;
991 compact();
992 return;
993 }
994 std::unique_ptr<MappedNameRef> mappedName(new MappedNameRef(_name, _sids));
995 if (!this->next) {
996 this->next = std::move(mappedName);
997 }
998 else {
999 this->next.swap(mappedName);
1000 this->next->next = std::move(mappedName);
1001 }
1002 }
1003
1004 void compact()
1005 {
1006 if (sids.size() > 1) {
1007 std::sort(sids.begin(), sids.end());
1008 sids.erase(std::unique(sids.begin(), sids.end()), sids.end());
1009 }
1010 }
1011
1012 bool erase(const MappedName& _name)
1013 {
1014 if (this->name == _name) {
1015 this->name.clear();
1016 this->sids.clear();
1017 if (this->next) {
1018 this->name = std::move(this->next->name);
1019 this->sids = std::move(this->next->sids);
1020 std::unique_ptr<MappedNameRef> tmp;
1021 tmp.swap(this->next);
1022 this->next = std::move(tmp->next);
1023 }
1024 return true;
1025 }
1026
1027 for (std::unique_ptr<MappedNameRef>* ptr = &this->next; *ptr; ptr = &(*ptr)->next) {
1028 if ((*ptr)->name == _name) {
1029 std::unique_ptr<MappedNameRef> tmp;
1030 tmp.swap(*ptr);
1031 *ptr = std::move(tmp->next);
1032 return true;
1033 }
1034 }
1035 return false;
1036 }
1037
1038 void clear()
1039 {
1040 this->name.clear();
1041 this->sids.clear();
1042 this->next.reset();
1043 }
1044 };
1045
1046
1047 // NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1048
1049
1050 } // namespace Data
1051
1052
1053 #endif // APP_MAPPED_NAME_H