File indexing completed on 2024-05-19 13:31:51

0001 // clang-format off
0002 /*
0003  * KDiff3 - Text Diff And Merge Tool
0004  *
0005  * SPDX-FileCopyrightText: 2002-2011 Joachim Eibl, joachim.eibl at gmx.de
0006  * SPDX-FileCopyrightText: 2018-2020 Michael Reeves reeves.87@gmail.com
0007  * SPDX-License-Identifier: GPL-2.0-or-later
0008  */
0009 // clang-format on
0010 
0011 #include "selection.h"
0012 #include "TypeUtils.h"
0013 
0014 #include <QtGlobal>
0015 
0016 #include <utility>   // for swap
0017 
0018 QtSizeType Selection::firstPosInLine(LineRef l) const
0019 {
0020     assert(firstLine.isValid());
0021 
0022     LineRef l1 = firstLine;
0023     LineRef l2 = lastLine;
0024     QtSizeType p1 = firstPos;
0025     QtSizeType p2 = lastPos;
0026     if(l1 > l2)
0027     {
0028         std::swap(l1, l2);
0029         std::swap(p1, p2);
0030     }
0031     if(l1 == l2 && p1 > p2)
0032     {
0033         std::swap(p1, p2);
0034     }
0035 
0036     if(l == l1)
0037         return p1;
0038 
0039     return 0;
0040 }
0041 
0042 QtSizeType Selection::lastPosInLine(LineRef l) const
0043 {
0044     assert(firstLine.isValid());
0045 
0046     LineRef l1 = firstLine;
0047     LineRef l2 = lastLine;
0048     QtSizeType p1 = firstPos;
0049     QtSizeType p2 = lastPos;
0050 
0051     if(l1 > l2)
0052     {
0053         std::swap(l1, l2);
0054         std::swap(p1, p2);
0055     }
0056     if(l1 == l2 && p1 > p2)
0057     {
0058         std::swap(p1, p2);
0059     }
0060 
0061     if(l == l2)
0062         return p2;
0063 
0064     return limits<qint32>::max();
0065 }
0066 
0067 bool Selection::within(LineRef l, QtSizeType p) const
0068 {
0069     if(!firstLine.isValid())
0070         return false;
0071 
0072     LineRef l1 = firstLine;
0073     LineRef l2 = lastLine;
0074     QtSizeType p1 = firstPos;
0075     QtSizeType p2 = lastPos;
0076     if(l1 > l2)
0077     {
0078         std::swap(l1, l2);
0079         std::swap(p1, p2);
0080     }
0081     if(l1 == l2 && p1 > p2)
0082     {
0083         std::swap(p1, p2);
0084     }
0085     if(l1 <= l && l <= l2)
0086     {
0087         if(l1 == l2)
0088             return p >= p1 && p < p2;
0089         if(l == l1)
0090             return p >= p1;
0091         if(l == l2)
0092             return p < p2;
0093         return true;
0094     }
0095     return false;
0096 }
0097 
0098 bool Selection::lineWithin(LineRef l) const
0099 {
0100     if(!firstLine.isValid())
0101         return false;
0102     LineRef l1 = firstLine;
0103     LineRef l2 = lastLine;
0104 
0105     if(l1 > l2)
0106     {
0107         std::swap(l1, l2);
0108     }
0109 
0110     return (l1 <= l && l <= l2);
0111 }