File indexing completed on 2024-04-28 17:07:02

0001 /*
0002     This file is part of the Okteta Core library, made within the KDE community.
0003 
0004     SPDX-FileCopyrightText: 2008 Friedrich W. H. Kossebau <kossebau@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007 */
0008 
0009 #include "insertpiecetablechange.hpp"
0010 
0011 // lib
0012 #include "piecetable.hpp"
0013 //
0014 #include <arraychangemetrics.hpp>
0015 // KF
0016 #include <KLocalizedString>
0017 
0018 namespace KPieceTable {
0019 
0020 InsertPieceTableChange::~InsertPieceTableChange() = default;
0021 
0022 int InsertPieceTableChange::type() const { return InsertId; }
0023 
0024 QString InsertPieceTableChange::description() const
0025 {
0026     return i18nc("name of the change", "Insert");
0027 }
0028 
0029 Address InsertPieceTableChange::storageOffset() const { return mStorageOffset; }
0030 
0031 bool InsertPieceTableChange::merge(const AbstractPieceTableChange* other)
0032 {
0033 // TODO: remove me again after synching solved
0034 // return false;
0035     bool result = false;
0036 
0037     if (other->type() == InsertId) {
0038         const auto* otherInsertChange = static_cast<const InsertPieceTableChange*>(other);
0039         if (mInsertOffset + mInsertLength == otherInsertChange->mInsertOffset) {
0040             mInsertLength += otherInsertChange->mInsertLength;
0041             result = true;
0042         }
0043     }
0044 
0045     return result;
0046 }
0047 
0048 AddressRange InsertPieceTableChange::apply(PieceTable* pieceTable) const
0049 {
0050     pieceTable->insert(mInsertOffset, mInsertLength, mStorageOffset);
0051 
0052     return AddressRange(mInsertOffset, pieceTable->size() - 1);
0053 }
0054 
0055 AddressRange InsertPieceTableChange::revert(PieceTable* pieceTable) const
0056 {
0057     const Address oldLast = pieceTable->size() - 1;
0058     pieceTable->remove(AddressRange::fromWidth(mInsertOffset, mInsertLength));
0059     return AddressRange(mInsertOffset, oldLast);
0060 }
0061 
0062 ArrayChangeMetrics InsertPieceTableChange::metrics() const
0063 {
0064     return ArrayChangeMetrics::asReplacement(mInsertOffset, 0, mInsertLength);
0065 }
0066 
0067 Size InsertPieceTableChange::dataSize() const { return mInsertLength; }
0068 
0069 }