langtools/src/share/classes/com/sun/tools/javac/util/Position.java
author briangoetz
Wed, 18 Dec 2013 10:29:25 -0500
changeset 22159 682da512ec17
parent 14259 fb94a1df0d53
permissions -rw-r--r--
8030253: Update langtools to use strings-in-switch 8030262: Update langtools to use foreach loops 8030245: Update langtools to use try-with-resources and multi-catch Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
     2
 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
10
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
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
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
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
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
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    39
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    40
 *  If you write code that depends on this, you do so at your own risk.
10
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:
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
    82
     *  {@code line-number << LINESHIFT + column-number }.
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 13844
diff changeset
    83
     *  {@link Position#NOPOS} represents an undefined position.
10
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)
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 13844
diff changeset
    87
     * @return       an encoded position or {@link Position#NOPOS}
10
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
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
   110
         *           if {@code lineNumber < 1}
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
   111
         *           if {@code lineNumber > no. of lines}
10
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
13844
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
   122
         *           if {@code line < 1}
56339cf983a3 7177970: fix issues in langtools doc comments
jjg
parents: 5847
diff changeset
   123
         *           if {@code line > no. of lines}
10
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
}