|
| 1 | +/* |
| 2 | + * This file is part of Notepad Next. |
| 3 | + * Copyright 2025 Justin Dailey |
| 4 | + * |
| 5 | + * Notepad Next is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * Notepad Next is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with Notepad Next. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +#pragma once |
| 19 | + |
| 20 | +#include <QByteArray> |
| 21 | +#include <QByteArrayView> |
| 22 | +#include <QList> |
| 23 | +#include <QSet> |
| 24 | +#include <cstring> |
| 25 | +#include <unordered_set> |
| 26 | + |
| 27 | +namespace ByteArrayUtils |
| 28 | +{ |
| 29 | + |
| 30 | +// Zero-copy split: returns views into 'source'. |
| 31 | +// The source QByteArray must remain alive while using the views. |
| 32 | +inline QList<QByteArrayView> split(const QByteArray& source, const QByteArray& delimiter) |
| 33 | +{ |
| 34 | + QList<QByteArrayView> out; |
| 35 | + |
| 36 | + const char* base = source.constData(); |
| 37 | + qsizetype len = source.size(); |
| 38 | + qsizetype dlen = delimiter.size(); |
| 39 | + |
| 40 | + qsizetype start = 0; |
| 41 | + |
| 42 | + while (true) { |
| 43 | + qsizetype pos = source.indexOf(delimiter, start); |
| 44 | + if (pos < 0) { |
| 45 | + out.append(QByteArrayView(base + start, len - start)); |
| 46 | + break; |
| 47 | + } |
| 48 | + |
| 49 | + out.append(QByteArrayView(base + start, pos - start)); |
| 50 | + start = pos + dlen; |
| 51 | + } |
| 52 | + |
| 53 | + return out; |
| 54 | +} |
| 55 | + |
| 56 | +// Remove duplicates and preserve order. |
| 57 | +inline void removeDuplicates(QList<QByteArrayView>& parts) |
| 58 | +{ |
| 59 | + std::unordered_set<QByteArray, std::hash<QByteArray>> seen; |
| 60 | + |
| 61 | + auto newEnd = std::remove_if(parts.begin(), parts.end(), [&seen](const QByteArrayView& v) { |
| 62 | + QByteArray key(v.data(), v.size()); // copy once per unique value |
| 63 | + if (seen.find(key) != seen.end()) |
| 64 | + return true; // duplicate, remove |
| 65 | + seen.insert(std::move(key)); |
| 66 | + return false; // first occurrence, keep |
| 67 | + }); |
| 68 | + |
| 69 | + parts.erase(newEnd, parts.end()); |
| 70 | +} |
| 71 | + |
| 72 | + |
| 73 | +// Remove only *consecutive* duplicates. |
| 74 | +// Example: A A B B B C A → A B C A |
| 75 | +inline void removeConsecutiveDuplicates(QList<QByteArrayView>& parts) |
| 76 | +{ |
| 77 | + auto newEnd = std::unique(parts.begin(), parts.end(), [](const QByteArrayView& a, const QByteArrayView& b) { |
| 78 | + return a == b; |
| 79 | + }); |
| 80 | + |
| 81 | + parts.erase(newEnd, parts.end()); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +inline QByteArray join(const QList<QByteArrayView>& parts, const QByteArray& delimiter) |
| 86 | +{ |
| 87 | + if (parts.isEmpty()) |
| 88 | + return QByteArray(); |
| 89 | + |
| 90 | + const qsizetype dlen = delimiter.size(); |
| 91 | + |
| 92 | + // Compute total size |
| 93 | + qsizetype total = 0; |
| 94 | + for (const auto& v : parts) |
| 95 | + total += v.size(); |
| 96 | + total += dlen * (parts.size() - 1); |
| 97 | + |
| 98 | + // Allocate final QByteArray |
| 99 | + QByteArray out; |
| 100 | + out.resize(total); |
| 101 | + |
| 102 | + // Copy segments |
| 103 | + char* dst = out.data(); |
| 104 | + for (int i = 0; i < parts.size(); ++i) { |
| 105 | + const auto& v = parts[i]; |
| 106 | + |
| 107 | + memcpy(dst, v.data(), v.size()); |
| 108 | + dst += v.size(); |
| 109 | + |
| 110 | + if (i + 1 < parts.size()) { |
| 111 | + memcpy(dst, delimiter.constData(), dlen); |
| 112 | + dst += dlen; |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + return out; |
| 117 | +} |
| 118 | + |
| 119 | +} // namespace ByteArrayViewUtils |
0 commit comments