File: range.cpp

package info (click to toggle)
kde4libs 4%3A4.14.2-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 82,316 kB
  • sloc: cpp: 761,810; xml: 12,344; ansic: 6,295; java: 4,060; perl: 2,938; yacc: 2,507; python: 1,207; sh: 1,179; ruby: 337; lex: 278; makefile: 29
file content (337 lines) | stat: -rw-r--r-- 7,075 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* This file is part of the KDE libraries
 *  Copyright (C) 2007 Mirko Stocker <me@misto.ch>
 *  Copyright (C) 2003-2005 Hamish Rodda <rodda@kde.org>
 *  Copyright (C) 2002 Christian Couder <christian@kdevelop.org>
 *  Copyright (C) 2001, 2003 Christoph Cullmann <cullmann@kde.org>
 *  Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
 *  Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 */

#include "range.h"

using namespace KTextEditor;

Range::Range()
  : m_start(new Cursor())
  , m_end(new Cursor())
{
  m_start->setRange(this);
  m_end->setRange(this);
}

Range::Range(const Cursor& start, const Cursor& end)
{
  if (start <= end) {
    m_start = new Cursor(start);
    m_end = new Cursor(end);

  } else {
    m_start = new Cursor(end);
    m_end = new Cursor(start);
  }

  m_start->setRange(this);
  m_end->setRange(this);
}

Range::Range(const Cursor& start, int width)
  : m_start(new Cursor(start))
  , m_end(new Cursor(start.line(), start.column() + width))
{
  m_start->setRange(this);
  m_end->setRange(this);
}

Range::Range(const Cursor& start, int endLine, int endColumn)
  : m_start(new Cursor(start))
  , m_end(new Cursor(endLine, endColumn))
{
  if (*m_end < *m_start) {
    Cursor* temp = m_end;
    m_end = m_start;
    m_start = temp;
  }

  m_start->setRange(this);
  m_end->setRange(this);
}

Range::Range(int startLine, int startColumn, int endLine, int endColumn)
  : m_start(new Cursor(startLine, startColumn))
  , m_end(new Cursor(endLine, endColumn))
{
  if (*m_end < *m_start) {
    Cursor* temp = m_end;
    m_end = m_start;
    m_start = temp;
  }

  m_start->setRange(this);
  m_end->setRange(this);
}

Range::Range(Cursor* start, Cursor* end)
  : m_start(start)
  , m_end(end)
{
  if (*m_end < *m_start) {
    Cursor temp = *m_end;
    *m_end = *m_start;
    *m_start = temp;
  }

  m_start->setRange(this);
  m_end->setRange(this);
}

Range::Range(const Range& copy)
  : m_start(new Cursor(copy.start()))
  , m_end(new Cursor(copy.end()))
{
  m_start->setRange(this);
  m_end->setRange(this);
}

Range::~Range()
{
  delete m_start;
  delete m_end;
}

bool Range::isValid( ) const
{
  return start().isValid() && end().isValid();
}

Range Range::invalid()
{
  return Range (Cursor(-1, -1), Cursor(-1, -1));
}

void Range::setRange(const Range& range)
{
  *m_start = range.start();
  *m_end = range.end();
}

void Range::setRange( const Cursor & start, const Cursor & end )
{
  if (start > end)
    setRange(Range(end, start));
  else
    setRange(Range(start, end));
}

bool Range::containsLine(int line) const
{
  return (line > start().line() || (line == start().line() && !start().column())) && line < end().line();
}

bool Range::overlapsLine(int line) const
{
  return line >= start().line() && line <= end().line();
}

bool Range::overlapsColumn( int col ) const
{
  return start().column() <= col && end().column() > col;
}

bool Range::contains( const Cursor& cursor ) const
{
  return cursor >= start() && cursor < end();
}

bool Range::contains( const Range& range ) const
{
  return range.start() >= start() && range.end() <= end();
}

bool Range::containsColumn( int column ) const
{
  return column >= start().column() && column < end().column();
}

bool Range::overlaps( const Range& range ) const
{
  if (range.start() <= start())
    return range.end() > start();

  else if (range.end() >= end())
    return range.start() < end();

  else
    return contains(range);
}

bool Range::boundaryAtCursor(const Cursor& cursor) const
{
  return cursor == start() || cursor == end();
}

bool Range::boundaryOnLine(int line) const
{
  return start().line() == line || end().line() == line;
}

bool Range::confineToRange(const Range& range)
{
  if (start() < range.start())
    if (end() > range.end())
      setRange(range);
    else
      start() = range.start();
  else if (end() > range.end())
    end() = range.end();
  else
    return false;

  return true;
}

bool Range::expandToRange(const Range& range)
{
  if (start() > range.start())
    if (end() < range.end())
      setRange(range);
    else
      start() = range.start();
  else if (end() < range.end())
    end() = range.end();
  else
    return false;

  return true;
}

void Range::rangeChanged( Cursor * c, const Range& )
{
  if (c == m_start) {
    if (*c > *m_end)
      *m_end = *c;

  } else if (c == m_end) {
    if (*c < *m_start)
      *m_start = *c;
  }
}

void Range::setBothLines( int line )
{
  setRange(Range(line, start().column(), line, end().column()));
}

bool KTextEditor::Range::onSingleLine( ) const
{
  return start().line() == end().line();
}

int KTextEditor::Range::columnWidth( ) const
{
  return end().column() - start().column();
}

int KTextEditor::Range::numberOfLines( ) const
{
  return end().line() - start().line();
}

bool KTextEditor::Range::isEmpty( ) const
{
  return start() == end();
}

int Range::positionRelativeToCursor( const Cursor & cursor ) const
{
  if (end() <= cursor)
    return -1;

  if (start() > cursor)
    return +1;

  return 0;
}

int Range::positionRelativeToLine( int line ) const
{
  if (end().line() < line)
    return -1;

  if (start().line() > line)
    return +1;

  return 0;
}

void KTextEditor::Range::setBothColumns( int column )
{
  setRange(Range(start().line(), column, end().line(), column));
}

bool KTextEditor::Range::isSmartRange( ) const
{
  return false;
}

SmartRange* KTextEditor::Range::toSmartRange( ) const
{
  return 0L;
}

Cursor& KTextEditor::Range::start()
{
  return *m_start;
}

const Cursor& KTextEditor::Range::start() const
{
  return *m_start;
}

Cursor& KTextEditor::Range::end()
{
  return *m_end;
}

const Cursor& KTextEditor::Range::end() const
{
  return *m_end;
}

Range KTextEditor::Range::intersect( const Range & range ) const
{
  if (!isValid() || !range.isValid() || *this > range || *this < range)
    return invalid();

  return Range(qMax(start(), range.start()), qMin(end(), range.end()));
}

Range KTextEditor::Range::encompass( const Range & range ) const
{
  if (!isValid())
    if (range.isValid())
      return range;
    else
      return invalid();
  else if (!range.isValid())
    return *this;
  else
    return Range(qMin(start(), range.start()), qMax(end(), range.end()));
}

// kate: space-indent on; indent-width 2; replace-tabs on;