summaryrefslogtreecommitdiffstats
path: root/khexedit/lib/kwordbufferservice.cpp
blob: 0489139da36213f78164c4debcdab141ce4c15f4 (plain)
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
/***************************************************************************
                          kwordbufferservice.cpp  -  description
                             -------------------
    begin                : Di Jan 18 2005
    copyright            : (C) 2005 by Friedrich W. H. Kossebau
    email                : Friedrich.W.H@Kossebau.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License version 2 as published by the Free Software Foundation.       *
 *                                                                         *
 ***************************************************************************/


// lib specific
#include "kdatabuffer.h"
#include "kcharcodec.h"
#include "kwordbufferservice.h"

using namespace KHE;


bool KWordBufferService::isWordChar( unsigned int Index ) const
{
  KHEChar C = CharCodec->decode( Buffer->datum(Index) );
  return !C.isUndefined() && C.isLetterOrNumber();
}


int KWordBufferService::indexOfPreviousWordStart( unsigned int Index ) const
{
  unsigned int Size = Buffer->size();
  // already at the start or can the result only be 0?
  if( Index == 0 || Size < 3 )
    return 0;

  // search in two rounds: first for the next char, than for the next nonchar
  // after that return the index of the one before
  bool LookingForFirstWordChar = false;
  for( ; Index>0; --Index )
  {
    if( !isWordChar(Index-1) )
    {
      if( !LookingForFirstWordChar )
        continue;
      return( Index );
    }
    else if( !LookingForFirstWordChar )
      LookingForFirstWordChar = true;
  }
  return 0;
}


int KWordBufferService::indexOfNextWordStart( unsigned int Index ) const
{
  unsigned int Size = Buffer->size();
  bool LookingForFirstWordChar = false;
  for( ; Index<Size; ++Index )
  {
    if( isWordChar(Index) )
    {
      if( !LookingForFirstWordChar )
        continue;
      return Index;
    }
    else if( !LookingForFirstWordChar )
      LookingForFirstWordChar = true;
  }
  // if no more word found, go to the end
  return Size;
}


int KWordBufferService::indexOfBeforeNextWordStart( unsigned int Index ) const
{
  unsigned int Size = Buffer->size();
  bool LookingForFirstWordChar = false;
  for( ; Index<Size; ++Index )
  {
    if( isWordChar(Index) )
    {
      if( !LookingForFirstWordChar )
        continue;
      return Index-1;
    }
    else if( !LookingForFirstWordChar )
      LookingForFirstWordChar = true;
  }
  // if no more word found, go to the end
  return Size-1;
}


int KWordBufferService::indexOfWordStart( unsigned int Index ) const
{
  for( ; Index > 0; --Index )
  {
    if( !isWordChar(Index-1) )
      return( Index );
  }
  return 0;
}


int KWordBufferService::indexOfWordEnd( unsigned int Index ) const
{
  unsigned int Size = Buffer->size();
  for( ++Index; Index<Size; ++Index )
  {
    if( !isWordChar(Index) )
      return Index-1;
  }
  // word reaches the end
  return Size-1;
}


int KWordBufferService::indexOfLeftWordSelect( unsigned int Index ) const
{
  // word at Index?
  if( isWordChar(Index) )
  {
    // search for word start to the left
    for( ; Index>0; --Index )
    {
      if( !isWordChar(Index-1) )
        return Index;
    }
    // reached start, so return it
    return 0;
  }
  else
  {
    unsigned int Size = Buffer->size();
    // search for word start to the right
    for( ++Index; Index<Size; ++Index )
    {
      if( isWordChar(Index) )
        return Index;
    }
    // word reaches the end, so step behind
    return Size;
  }
}


int KWordBufferService::indexOfRightWordSelect( unsigned int Index ) const
{
  // no word at Index?
  if( !isWordChar(Index) )
  {
    // search for word end to the left
    for( ; Index>0; --Index )
    {
      if( isWordChar(Index-1) )
        return Index;
    }
    // reached start, so return it
    return 0;
  }
  else
  {
    unsigned int Size = Buffer->size();
    for( ++Index; Index<Size; ++Index )
    {
      // search for word end to the right
      if( !isWordChar(Index) )
        return Index;
    }
    // word reaches the end, so step behind
    return Size;
  }
}

/*
int KWordBufferService::indexOfBehindWordEnd( unsigned int Index ) const
{
  // no word at Index?
  return !::isWordChar(datum(Index)) ? indexOfBehindLeftWordEnd(Index) : indexOfBehindRightWordEnd(Index+1)
}


int KWordBufferService::indexOfBehindRightWordEnd( unsigned int Index ) const
{
  for( ; Index<size(); ++Index )
{
    if( !::isWordChar(datum(Index)) )
      return Index;
}
  // word reaches the end, so step behind
  return size();
}


int KWordBufferService::indexOfBehindLeftWordEnd( unsigned int Index ) const
{
  for( --Index; Index>=0; --Index )
{
    if( ::isWordChar(datum(Index)) )
      return Index+1;
}
  // word reaches the end, so step behind
  return 0;
}
*/