• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • core
wordbytearrayservice.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Core library, made within the KDE community.
3 
4  Copyright 2005,2008-2009 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "wordbytearrayservice.h"
24 
25 // lib
26 #include "abstractbytearraymodel.h"
27 #include "character.h"
28 #include "charcodec.h"
29 
30 
31 namespace Okteta
32 {
33 
34 WordByteArrayService::WordByteArrayService( const AbstractByteArrayModel* byteArrayModel, const CharCodec* charCodec )
35  : mByteArrayModel( byteArrayModel ),
36  mCharCodec( charCodec )
37 {}
38 
39 AddressRange WordByteArrayService::wordSection( Address index ) const
40 {
41  return isWordChar(index) ?
42  AddressRange( indexOfWordStart(index), indexOfWordEnd(index) ) :
43  AddressRange();
44 }
45 
46 
47 bool WordByteArrayService::isWordChar( Address index ) const
48 {
49  const Character decodedChar = mCharCodec->decode( mByteArrayModel->byte(index) );
50  return !decodedChar.isUndefined() && decodedChar.isLetterOrNumber();
51 }
52 
53 
54 Address WordByteArrayService::indexOfPreviousWordStart( Address index ) const
55 {
56  const Size size = mByteArrayModel->size();
57  // already at the start or can the result only be 0?
58  if( index == 0 || size < 3 )
59  return 0;
60 
61  // search in two rounds: first for the next char, than for the next nonchar
62  // after that return the index of the one before
63  bool lookingForFirstWordChar = false;
64  for( ; index>0; --index )
65  {
66  if( !isWordChar(index-1) )
67  {
68  if( !lookingForFirstWordChar )
69  continue;
70  return( index );
71  }
72  else if( !lookingForFirstWordChar )
73  lookingForFirstWordChar = true;
74  }
75 
76  return 0;
77 }
78 
79 
80 Address WordByteArrayService::indexOfNextWordStart( Address index ) const
81 {
82  const Size size = mByteArrayModel->size();
83  bool lookingForFirstWordChar = false;
84  for( ; index<size; ++index )
85  {
86  if( isWordChar(index) )
87  {
88  if( !lookingForFirstWordChar )
89  continue;
90  return index;
91  }
92  else if( !lookingForFirstWordChar )
93  lookingForFirstWordChar = true;
94  }
95  // if no more word found, go to the end
96  return size;
97 }
98 
99 
100 Address WordByteArrayService::indexOfBeforeNextWordStart( Address index ) const
101 {
102  const Size size = mByteArrayModel->size();
103  bool lookingForFirstWordChar = false;
104  for( ; index<size; ++index )
105  {
106  if( isWordChar(index) )
107  {
108  if( !lookingForFirstWordChar )
109  continue;
110  return index-1;
111  }
112  else if( !lookingForFirstWordChar )
113  lookingForFirstWordChar = true;
114  }
115 
116  // if no more word found, go to the end
117  return size-1;
118 }
119 
120 
121 Address WordByteArrayService::indexOfWordStart( Address index ) const
122 {
123  for( ; index > 0; --index )
124  {
125  if( !isWordChar(index-1) )
126  return( index );
127  }
128 
129  return 0;
130 }
131 
132 
133 Address WordByteArrayService::indexOfWordEnd( Address index ) const
134 {
135  const Size size = mByteArrayModel->size();
136  for( ++index; index<size; ++index )
137  {
138  if( !isWordChar(index) )
139  return index-1;
140  }
141  // word reaches the end
142  return size-1;
143 }
144 
145 
146 Address WordByteArrayService::indexOfLeftWordSelect( Address index ) const
147 {
148  // word at index?
149  if( isWordChar(index) )
150  {
151  // search for word start to the left
152  for( ; index>0; --index )
153  {
154  if( !isWordChar(index-1) )
155  return index;
156  }
157  // reached start, so return it
158  return 0;
159  }
160  else
161  {
162  const Size size = mByteArrayModel->size();
163  // search for word start to the right
164  for( ++index; index<size; ++index )
165  {
166  if( isWordChar(index) )
167  return index;
168  }
169  // word reaches the end, so step behind
170  return size;
171  }
172 }
173 
174 
175 Address WordByteArrayService::indexOfRightWordSelect( Address index ) const
176 {
177  // TODO: should this check be here or with the caller?
178  // the later would need another function to search the previous word end
179  const Size size = mByteArrayModel->size();
180  bool searchToLeft;
181  if( index >= size )
182  {
183  index = size;
184  searchToLeft = true;
185  }
186  else
187  searchToLeft = !isWordChar(index);
188  // no word at index?
189  if( searchToLeft )
190  {
191  // search for word end to the left
192  for( ; index>0; --index )
193  {
194  if( isWordChar(index-1) )
195  return index;
196  }
197  // reached start, so return it
198  return 0;
199  }
200  else
201  {
202  for( ++index; index<size; ++index )
203  {
204  // search for word end to the right
205  if( !isWordChar(index) )
206  return index;
207  }
208  // word reaches the end, so step behind
209  return size;
210  }
211 }
212 
213 /*
214 Address WordByteArrayService::indexOfBehindWordEnd( Address index ) const
215 {
216  // no word at index?
217  return !::isWordChar(byte(index)) ? indexOfBehindLeftWordEnd(index) : indexOfBehindRightWordEnd(index+1)
218 }
219 
220 
221 Address WordByteArrayService::indexOfBehindRightWordEnd( Address index ) const
222 {
223  for( ; index<size(); ++index )
224 {
225  if( !::isWordChar(byte(index)) )
226  return index;
227 }
228  // word reaches the end, so step behind
229  return size();
230 }
231 
232 
233 Address WordByteArrayService::indexOfBehindLeftWordEnd( Address index ) const
234 {
235  for( --index; index>=0; --index )
236 {
237  if( ::isWordChar(byte(index)) )
238  return index+1;
239 }
240  // word reaches the end, so step behind
241  return 0;
242 }
243 */
244 
245 // TODO: rename WordByteArrayService to TextByteArrayService or TextByteArrayAnalyser
246 QString WordByteArrayService::text( Address index, Address lastIndex ) const
247 {
248  QString result;
249 
250  const Address lastValidIndex = mByteArrayModel->size() - 1;
251  const Address behindLastIndex =
252  (( lastIndex < 0 || lastIndex > lastValidIndex ) ? lastValidIndex : lastIndex ) + 1;
253 
254  const Size maxTextLength = behindLastIndex - index;
255  result.reserve( maxTextLength );
256 
257  for( ; index<behindLastIndex; ++index )
258  {
259  const Character decodedChar = mCharCodec->decode( mByteArrayModel->byte(index) );
260  // TODO: handle line breaks, separators and spacing, controlled by flags given as parameter
261  const bool isTextChar = ( !decodedChar.isUndefined() &&
262  (decodedChar.isLetterOrNumber() || decodedChar.isSpace() || decodedChar.isPunct()) );
263 
264  if( !isTextChar )
265  break;
266 
267  result.append( decodedChar );
268  }
269 
270  result.squeeze();
271 
272  return result;
273 }
274 
275 WordByteArrayService::~WordByteArrayService() {}
276 
277 }
character.h
wordbytearrayservice.h
Okteta::Address
qint32 Address
Definition: address.h:34
Okteta::AbstractByteArrayModel
could it be useful to hide the data access behind an iterator? * class KDataBufferIterator { public: ...
Definition: abstractbytearraymodel.h:79
Okteta::WordByteArrayService::indexOfLeftWordSelect
Address indexOfLeftWordSelect(Address index) const
Definition: wordbytearrayservice.cpp:146
abstractbytearraymodel.h
Okteta::WordByteArrayService::mCharCodec
const CharCodec *const mCharCodec
Definition: wordbytearrayservice.h:131
Okteta::WordByteArrayService::indexOfNextWordStart
Address indexOfNextWordStart(Address index) const
searches for the start of the next word not including the given index.
Definition: wordbytearrayservice.cpp:80
Okteta::WordByteArrayService::indexOfWordStart
Address indexOfWordStart(Address index) const
searches for the start of the word including the given index.
Definition: wordbytearrayservice.cpp:121
KDE::NumberRange< Address, Size >
Okteta::Character::isUndefined
bool isUndefined() const
Definition: character.h:52
Okteta::CharCodec::decode
virtual Character decode(Byte byte) const =0
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
Okteta::CharCodec
Definition: charcodec.h:42
Okteta::WordByteArrayService::indexOfBeforeNextWordStart
Address indexOfBeforeNextWordStart(Address index) const
searches for the start of the next word not including the given index.
Definition: wordbytearrayservice.cpp:100
Okteta::WordByteArrayService::wordSection
AddressRange wordSection(Address index) const
returns the section with a word around index.
Definition: wordbytearrayservice.cpp:39
Okteta::AbstractByteArrayModel::byte
virtual Byte byte(Address offset) const =0
locates working range The idea behind is to tell buffer which range will be requested in the followin...
Okteta::AddressRange
KDE::NumberRange< Address, Size > AddressRange
Definition: addressrange.h:35
charcodec.h
Okteta::WordByteArrayService::mByteArrayModel
const AbstractByteArrayModel *const mByteArrayModel
Definition: wordbytearrayservice.h:130
Okteta::WordByteArrayService::WordByteArrayService
WordByteArrayService(const AbstractByteArrayModel *byteArrayModel, const CharCodec *charCodec)
Definition: wordbytearrayservice.cpp:34
Okteta::WordByteArrayService::indexOfRightWordSelect
Address indexOfRightWordSelect(Address index) const
searches for the first char after the end of the word including the given index.
Definition: wordbytearrayservice.cpp:175
Okteta::Size
qint32 Size
Definition: size.h:33
Okteta::WordByteArrayService::~WordByteArrayService
~WordByteArrayService()
Definition: wordbytearrayservice.cpp:275
Okteta::WordByteArrayService::isWordChar
bool isWordChar(Address index) const
if index is out of range the behaviour is undefined
Definition: wordbytearrayservice.cpp:47
Okteta::Character
Definition: character.h:35
Okteta::WordByteArrayService::indexOfPreviousWordStart
Address indexOfPreviousWordStart(Address index) const
searches for the first char after the end of the word including the given index.
Definition: wordbytearrayservice.cpp:54
Okteta::WordByteArrayService::indexOfWordEnd
Address indexOfWordEnd(Address index) const
searches for the end of the word including the given index.
Definition: wordbytearrayservice.cpp:133
Okteta::WordByteArrayService::text
QString text(Address index, Address lastIndex=-1) const
returns the text starting at the given index until the first non-text byte if there is no text byte a...
Definition: wordbytearrayservice.cpp:246
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal