Avoid potential negative array index access to cached text.
[LibreOffice.git] / sc / inc / bigrange.hxx
blob139c32c6617423f8f8459cdded006592fd069f38
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #pragma once
22 #include "address.hxx"
23 #include "document.hxx"
25 // This is used by change tracking. References there may be located also outside of the document
26 // (see ScRefUpdate::Update()), and so it needs bigger range than ScAddress/ScRange.
28 class ScBigAddress
30 sal_Int64 nRow;
31 sal_Int64 nCol;
32 sal_Int64 nTab;
34 public:
35 ScBigAddress() : nRow(0), nCol(0), nTab(0) {}
36 ScBigAddress( sal_Int64 nColP, sal_Int64 nRowP, sal_Int64 nTabP )
37 : nRow( nRowP ), nCol( nColP ), nTab( nTabP ) {}
38 ScBigAddress( const ScBigAddress& r )
39 : nRow( r.nRow ), nCol( r.nCol ), nTab( r.nTab ) {}
40 ScBigAddress( ScBigAddress&& ) = default;
41 ScBigAddress( const ScAddress& r )
42 : nRow( r.Row() ), nCol( r.Col() ), nTab( r.Tab() ) {}
44 sal_Int64 Col() const { return nCol; }
45 sal_Int64 Row() const { return nRow; }
46 sal_Int64 Tab() const { return nTab; }
48 void Set( sal_Int64 nColP, sal_Int64 nRowP, sal_Int64 nTabP )
49 { nCol = nColP; nRow = nRowP; nTab = nTabP; }
50 void SetCol( sal_Int64 nColP ) { nCol = nColP; }
51 void SetRow( sal_Int64 nRowP ) { nRow = nRowP; }
52 void SetTab( sal_Int64 nTabP ) { nTab = nTabP; }
53 void IncCol( sal_Int64 n = 1 ) { nCol += n; }
54 void IncRow( sal_Int64 n = 1 ) { nRow += n; }
55 void IncTab( sal_Int64 n = 1 ) { nTab += n; }
57 void GetVars( sal_Int64& nColP, sal_Int64& nRowP, sal_Int64& nTabP ) const
58 { nColP = nCol; nRowP = nRow; nTabP = nTab; }
60 bool IsValid( const ScDocument& rDoc ) const;
61 inline ScAddress MakeAddress( const ScDocument& rDoc ) const;
63 ScBigAddress& operator=( const ScBigAddress& r )
64 { nCol = r.nCol; nRow = r.nRow; nTab = r.nTab; return *this; }
65 ScBigAddress& operator=( ScBigAddress&& ) = default;
66 ScBigAddress& operator=( const ScAddress& r )
67 { nCol = r.Col(); nRow = r.Row(); nTab = r.Tab(); return *this; }
68 bool operator==( const ScBigAddress& r ) const
69 { return nCol == r.nCol && nRow == r.nRow && nTab == r.nTab; }
70 bool operator!=( const ScBigAddress& r ) const
71 { return !operator==( r ); }
74 inline ScAddress ScBigAddress::MakeAddress( const ScDocument& rDoc ) const
76 SCCOL nColA;
77 SCROW nRowA;
78 SCTAB nTabA;
80 if ( nCol < 0 )
81 nColA = 0;
82 else if ( nCol > rDoc.MaxCol() )
83 nColA = rDoc.MaxCol();
84 else
85 nColA = static_cast<SCCOL>(nCol);
87 if ( nRow < 0 )
88 nRowA = 0;
89 else if ( nRow > rDoc.MaxRow() )
90 nRowA = rDoc.MaxRow();
91 else
92 nRowA = static_cast<SCROW>(nRow);
94 if ( nTab < 0 )
95 nTabA = 0;
96 else if ( nTab > MAXTAB )
97 nTabA = MAXTAB;
98 else
99 nTabA = static_cast<SCTAB>(nTab);
101 return ScAddress( nColA, nRowA, nTabA );
104 class ScBigRange
106 public:
108 ScBigAddress aStart;
109 ScBigAddress aEnd;
111 ScBigRange() : aStart(), aEnd() {}
112 ScBigRange( const ScBigRange& r )
113 : aStart( r.aStart ), aEnd( r.aEnd ) {}
114 ScBigRange( ScBigRange&& ) = default;
115 ScBigRange( const ScRange& r )
116 : aStart( r.aStart ), aEnd( r.aEnd ) {}
117 ScBigRange( sal_Int64 nCol1, sal_Int64 nRow1, sal_Int64 nTab1,
118 sal_Int64 nCol2, sal_Int64 nRow2, sal_Int64 nTab2 )
119 : aStart( nCol1, nRow1, nTab1 ),
120 aEnd( nCol2, nRow2, nTab2 ) {}
122 void Set( sal_Int64 nCol1, sal_Int64 nRow1, sal_Int64 nTab1,
123 sal_Int64 nCol2, sal_Int64 nRow2, sal_Int64 nTab2 )
124 { aStart.Set( nCol1, nRow1, nTab1 );
125 aEnd.Set( nCol2, nRow2, nTab2 ); }
127 void GetVars( sal_Int64& nCol1, sal_Int64& nRow1, sal_Int64& nTab1,
128 sal_Int64& nCol2, sal_Int64& nRow2, sal_Int64& nTab2 ) const
129 { aStart.GetVars( nCol1, nRow1, nTab1 );
130 aEnd.GetVars( nCol2, nRow2, nTab2 ); }
132 bool IsValid( const ScDocument& rDoc ) const
133 { return aStart.IsValid( rDoc ) && aEnd.IsValid( rDoc ); }
134 ScRange MakeRange( const ScDocument& rDoc ) const
135 { return ScRange( aStart.MakeAddress( rDoc ), aEnd.MakeAddress( rDoc ) ); }
137 inline bool Contains( const ScBigAddress& ) const; ///< is Address& in range?
138 inline bool Contains( const ScBigRange& ) const; ///< is Range& in range?
139 inline bool Intersects( const ScBigRange& ) const; ///< do two ranges overlap?
141 ScBigRange& operator=( const ScBigRange& r )
142 { aStart = r.aStart; aEnd = r.aEnd; return *this; }
143 ScBigRange& operator=( ScBigRange&& ) = default;
144 bool operator==( const ScBigRange& r ) const
145 { return (aStart == r.aStart) && (aEnd == r.aEnd); }
146 bool operator!=( const ScBigRange& r ) const
147 { return !operator==( r ); }
149 // These are used to define whole rows/cols/tabs.
150 constexpr static sal_Int64 nRangeMin = ::std::numeric_limits<sal_Int64>::min();;
151 constexpr static sal_Int64 nRangeMax = ::std::numeric_limits<sal_Int64>::max();;
154 inline bool ScBigRange::Contains( const ScBigAddress& rAddr ) const
156 return
157 aStart.Col() <= rAddr.Col() && rAddr.Col() <= aEnd.Col() &&
158 aStart.Row() <= rAddr.Row() && rAddr.Row() <= aEnd.Row() &&
159 aStart.Tab() <= rAddr.Tab() && rAddr.Tab() <= aEnd.Tab();
162 inline bool ScBigRange::Contains( const ScBigRange& r ) const
164 return
165 aStart.Col() <= r.aStart.Col() && r.aEnd.Col() <= aEnd.Col() &&
166 aStart.Row() <= r.aStart.Row() && r.aEnd.Row() <= aEnd.Row() &&
167 aStart.Tab() <= r.aStart.Tab() && r.aEnd.Tab() <= aEnd.Tab();
170 inline bool ScBigRange::Intersects( const ScBigRange& r ) const
172 return
173 aStart.Col() <= r.aEnd.Col() && r.aStart.Col() <= aEnd.Col() &&
174 aStart.Row() <= r.aEnd.Row() && r.aStart.Row() <= aEnd.Row() &&
175 aStart.Tab() <= r.aEnd.Tab() && r.aStart.Tab() <= aEnd.Tab();
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */