langtools/src/share/classes/com/sun/tools/javac/util/Position.java
author jjg
Tue, 16 Sep 2008 18:35:18 -0700
changeset 1260 a772ba9ba43d
parent 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes Reviewed-by: darcy, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 1999-2006 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.BitSet;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import static com.sun.tools.javac.util.LayoutCharacters.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
/** A class that defines source code positions as simple character
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 *  offsets from the beginning of the file. The first character
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *  is at position 0.
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 *  Support is also provided for (line,column) coordinates, but tab
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *  expansion is optional and no Unicode excape translation is considered.
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 *  The first character is at location (1,1).
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *  you write code that depends on this, you do so at your own risk.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
public class Position {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    public static final int NOPOS        = -1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    public static final int FIRSTPOS     = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    public static final int FIRSTLINE    = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    public static final int FIRSTCOLUMN  = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    public static final int LINESHIFT    = 10;
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    public static final int MAXCOLUMN    = (1<<LINESHIFT) - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public static final int MAXLINE      = (1<<(Integer.SIZE-LINESHIFT)) - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public static final int MAXPOS       = Integer.MAX_VALUE;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * This is class is not supposed to be instantiated.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private Position() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /** A two-way map between line/column numbers and positions,
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     *  derived from a scan done at creation time.  Tab expansion is
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     *  optionally supported via a character map.  Text content
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     *  is not retained.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     *<p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     *  Notes:  The first character position FIRSTPOS is at
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     *  (FIRSTLINE,FIRSTCOLUMN).  No account is taken of Unicode escapes.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * @param   src         Source characters
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * @param   max         Number of characters to read
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * @param   expandTabs  If true, expand tabs when calculating columns
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    public static LineMap makeLineMap(char[] src, int max, boolean expandTabs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        LineMapImpl lineMap = expandTabs ?
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            new LineTabMapImpl(max) : new LineMapImpl();
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        lineMap.build(src, max);
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        return lineMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /** Encode line and column numbers in an integer as:
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
     *  line-number << LINESHIFT + column-number
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     *  {@link Position.NOPOS represents an undefined position.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     * @param  line  number of line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * @param  col   number of character on line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     * @return       an encoded position or {@link Position.NOPOS
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     *               if the line or column number is too big to
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     *               represent in the encoded format
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * @throws IllegalArgumentException if line or col is less than 1
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    public static int encodePosition(int line, int col) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        if (line < 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            throw new IllegalArgumentException("line must be greater than 0");
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        if (col < 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            throw new IllegalArgumentException("column must be greater than 0");
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        if (line > MAXLINE || col > MAXCOLUMN) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            return NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        return (line << LINESHIFT) + col;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public static interface LineMap extends com.sun.source.tree.LineMap {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        /** Find the start position of a line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
         * @param line number of line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
         * @return     position of first character in line
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
         * @throws  ArrayIndexOutOfBoundsException
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
         *           if <tt>lineNumber < 1</tt>
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
         *           if <tt>lineNumber > no. of lines</tt>
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        int getStartPosition(int line);
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        /** Find the position corresponding to a (line,column).
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
         * @param   line    number of line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
         * @param   column  number of character on line (first is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
         * @return  position of character
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
         * @throws  ArrayIndexOutOfBoundsException
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
         *           if <tt>line < 1</tt>
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
         *           if <tt>line > no. of lines</tt>
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        int getPosition(int line, int column);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        /** Find the line containing a position; a line termination
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
         * character is on the line it terminates.
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
         * @param   pos  character offset of the position
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
         * @return the line number on which pos occurs (first line is 1)
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        int getLineNumber(int pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        /** Find the column for a character position.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
         *  Note:  this method does not handle tab expansion.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
         *  If tab expansion is needed, use a LineTabMap instead.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
         * @param  pos   character offset of the position
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
         * @return       the column number at which pos occurs
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        int getColumnNumber(int pos);
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    static class LineMapImpl implements LineMap {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        protected int[] startPosition; // start position of each line
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
        protected LineMapImpl() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        protected void build(char[] src, int max) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            int c = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
            int[] linebuf = new int[max];
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            while (i < max) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
                linebuf[c++] = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                do {
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
                    char ch = src[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                    if (ch == '\r' || ch == '\n') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                        if (ch == '\r' && (i+1) < max && src[i+1] == '\n')
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                            i += 2;
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                            ++i;
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                        break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                    else if (ch == '\t')
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
                        setTabPosition(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
                } while (++i < max);
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
            this.startPosition = new int[c];
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
            System.arraycopy(linebuf, 0, startPosition, 0, c);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        public int getStartPosition(int line) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            return startPosition[line - FIRSTLINE];
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        public long getStartPosition(long line) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
            return getStartPosition(longToInt(line));
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        public int getPosition(int line, int column) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            return startPosition[line - FIRSTLINE] + column - FIRSTCOLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        public long getPosition(long line, long column) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            return getPosition(longToInt(line), longToInt(column));
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        // Cache of last line number lookup
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        private int lastPosition = Position.FIRSTPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        private int lastLine = Position.FIRSTLINE;
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        public int getLineNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            if (pos == lastPosition) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                return lastLine;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            lastPosition = pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
            int low = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            int high = startPosition.length-1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            while (low <= high) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
                int mid = (low + high) >> 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
                int midVal = startPosition[mid];
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
                if (midVal < pos)
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
                    low = mid + 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                else if (midVal > pos)
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
                    high = mid - 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
                else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
                    lastLine = mid + 1; // pos is at beginning of this line
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
                    return lastLine;
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            lastLine = low;
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
            return lastLine;  // pos is on this line
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        public long getLineNumber(long pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            return getLineNumber(longToInt(pos));
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        public int getColumnNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
            return pos - startPosition[getLineNumber(pos) - FIRSTLINE] + FIRSTCOLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        public long getColumnNumber(long pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            return getColumnNumber(longToInt(pos));
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        private static int longToInt(long longValue) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            int intValue = (int)longValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
            if (intValue != longValue)
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
                throw new IndexOutOfBoundsException();
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
            return intValue;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        protected void setTabPosition(int offset) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     * A LineMap that handles tab expansion correctly.  The cost is
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
     * an additional bit per character in the source array.
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    public static class LineTabMapImpl extends LineMapImpl {
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        private BitSet tabMap;       // bits set for tab positions.
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        public LineTabMapImpl(int max) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
            super();
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            tabMap = new BitSet(max);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        protected void setTabPosition(int offset) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            tabMap.set(offset);
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        public int getColumnNumber(int pos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            int lineStart = startPosition[getLineNumber(pos) - FIRSTLINE];
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            int column = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
            for (int bp = lineStart; bp < pos; bp++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                if (tabMap.get(bp))
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                    column = (column / TabInc * TabInc) + TabInc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                    column++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            return column + FIRSTCOLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
        public int getPosition(int line, int column) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            int pos = startPosition[line - FIRSTLINE];
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
            column -= FIRSTCOLUMN;
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
            int col = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            while (col < column) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                pos++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                if (tabMap.get(pos))
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    col = (col / TabInc * TabInc) + TabInc;
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                else
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                    col++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            return pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
}