File: Tab.cpp

package info (click to toggle)
subcommander 2.0.0~b5p2-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,344 kB
  • sloc: cpp: 63,594; sh: 4,050; xml: 1,992; makefile: 1,134; ansic: 786; ruby: 251; lisp: 24
file content (244 lines) | stat: -rw-r--r-- 4,015 bytes parent folder | download | duplicates (5)
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
/* ====================================================================
 * Copyright (c) 2003-2006, Martin Hauner
 *                          http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "Tab.h"
#include "util/utf8.h"


/**
 * \brief construct a Tab object. 
 *
 * \param tabWidth use tabwidth spaces for a single tab in the column
 * calculations.
 */

Tab::Tab( unsigned int tabWidth )
: _tabwidth(tabWidth)
{
}


/**
 * \brief calc the number of columns in a string.
 * A CRLF counts as a single column.
 *
 * \param s an utf-8 encoded 0 terminated string.
 */

unsigned int Tab::calcColumns( const char* s )
{
  unsigned int chars = 0;

  if( ! s )
  {
    return 0;
  }

  while( *s )
  {
    if( *s == '\t' && (chars % _tabwidth == 0) )
    {
      chars += _tabwidth;
    }
    else if( *s == '\r' || *s == '\n' )
    {
      // on any line end marker we are done
      break;
    }
    else
    {
      chars += 1;
    }

    s = utf8::next8( s );
  }

  return chars;
}


/**
 * \brief calc next column, on tab jump tabwidth.
 * A CRLF counts as a single column.
 *
 * \param s    an utf-8 encoded 0 terminated string.
 * \param pos  current column position.
 */

int Tab::calcColumnForNextChar( const char* s, int pos )
{
  int col = 0;

  if( ! s )
  {
    return pos;
  }

  while( *s && col <= pos )
  {
    if( *s == '\t' && (col % _tabwidth == 0) )
    {
      col += _tabwidth;
    }
    else 
    {
      col += 1;
      
      // on any line end marker we are done
      // next col is after end of line
      if( *s == '\r' || *s == '\n' )
      {
        break;
      }
    }

    s = utf8::next8( s );
  }

  return col;
}


/**
 * \brief calc prev column, on tab jump tabwidth.
 * A CRLF counts as a single column.
 *
 * \param s    an utf-8 encoded 0 terminated string.
 * \param pos  current column position.
 */

int Tab::calcColumnForPrevChar( const char* s, int pos )
{
  int col = 0;

  if( ! s )
  {
    return pos;
  }

  while( *s && col < pos )
  {
    int plus = 0;

    if( *s == '\t' && (col % _tabwidth == 0) )
    {
      plus = _tabwidth;
    }
    else if( *s == '\r' || *s == '\n' )
    {
      // shouldn't happen
    }
    else
    {
      plus = 1;
    }

    if( col + plus >= pos )
    {
      break;
    }

    col += plus;

    s = utf8::next8( s );
  }

  return col;
}

/**
 * \brief calc the character offset for the character at the given column.
 * A CRLF counts as a single column.
 *
 * \param s   an utf-8 encoded 0 terminated string.
 * \param col the column for which we calculate the character offset.
 */

int Tab::calcCharOffsetForColumn( const char* s, int col )
{
  int pos = 0;
  int chr = 0;

  if( ! s )
  {
    return pos;
  }

  while( *s && pos < col )
  {
    if( *s == '\t' && (pos % _tabwidth == 0) )
    {
      pos += _tabwidth;
    }
    else if( *s == '\r' || *s == '\n' )
    {
      // shouldn't happen
      break;
    }
    else
    {
      pos += 1;
    }

    s = utf8::next8( s );
    chr++;
  }

  return chr;
}

/**
 * \brief calc the nearest column position for the given column.
 * This is used if you click into the space of a tab to position
 * a cursor at the column where the tab begins or the tab ends.
 *
 * \param s   an utf-8 encoded 0 terminated string.
 * \param col the column for which we calculate the new column.
 */

int Tab::calcColumnForColumn( const char* s, int col )
{
  int ncol = 0;

  if( ! s )
  {
    return 0;
  }

  while( *s && ncol <= col )
  {
    int add = 0;

    if( *s == '\t' && (ncol % _tabwidth == 0) )
    {
      add = _tabwidth;
    }
    else if( *s == '\r' || *s == '\n' )
    {
      // count CRLF as one column
    }
    else
    {
      add = 1;
    }

    if( ncol + add > col )
    {
      break;
    }

    ncol += add;
    s = utf8::next8( s );
  }

  return ncol;
}