001/*
002 * Copyright (C) 2009-2011 Mathias Doenitz
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.parboiled.buffers;
018
019import org.parboiled.support.IndexRange;
020import org.parboiled.support.Position;
021import org.parboiled.common.Preconditions;
022import org.parboiled.support.IndexRange;
023import org.parboiled.support.Position;
024
025import java.util.Arrays;
026
027import static org.parboiled.common.Preconditions.checkArgument;
028
029/**
030 * An InputBuffer wrapping another InputBuffer and providing for the ability to insert (and undo) characters at
031 * certain index positions. Inserted chars do not appear in extracted text and have the same positions as the
032 * original chars at their indices.
033 * Note that this implementation is optimized for a rather small number of insertions and will perform badly with
034 * a large number of insertions.
035 */
036public class MutableInputBuffer implements InputBuffer {
037    private final InputBuffer buffer;
038    private int[] inserts = new int[0];
039    private char[] chars = new char[0];
040
041    public MutableInputBuffer(InputBuffer buffer) {
042        this.buffer = buffer;
043    }
044
045    public char charAt(int index) {
046        int j = Arrays.binarySearch(inserts, index);
047        if (j >= 0) return chars[j];
048        return buffer.charAt(index + (j + 1));
049    }
050
051    public boolean test(int index, char[] characters) {
052        throw new UnsupportedOperationException();
053    }
054
055    public Position getPosition(int index) {
056        return buffer.getPosition(map(index));
057    }
058
059    public int getOriginalIndex(int index) {
060        return buffer.getOriginalIndex(map(index));
061    }
062
063    public String extractLine(int lineNumber) {
064        return buffer.extractLine(lineNumber);
065    }
066
067    public String extract(int start, int end) {
068        return buffer.extract(map(start), map(end));
069    }
070
071    public String extract(IndexRange range) {
072        return buffer.extract(map(range.start), map(range.end));
073    }
074
075    public int getLineCount() {
076        return buffer.getLineCount();
077    }
078
079    private int map(int index) {
080        int j = Arrays.binarySearch(inserts, index);
081        if (j < 0) j = -(j + 1);
082        return index - j;
083    }
084
085    public void insertChar(int index, char c) {
086        int j = Arrays.binarySearch(inserts, index);
087        if (j < 0) j = -(j + 1);
088
089        char[] newChars = new char[chars.length + 1];
090        System.arraycopy(chars, 0, newChars, 0, j);
091        newChars[j] = c;
092        System.arraycopy(chars, j, newChars, j + 1, chars.length - j);
093        chars = newChars;
094
095        int[] newInserts = new int[inserts.length + 1];
096        System.arraycopy(inserts, 0, newInserts, 0, j);
097        newInserts[j] = index;
098        for (int i = j; i < inserts.length; i++) {
099            newInserts[i + 1] = inserts[i] + 1;
100        }
101        inserts = newInserts;
102    }
103
104    public char undoCharInsertion(int index) {
105        int j = Arrays.binarySearch(inserts, index);
106        Preconditions.checkArgument(j >= 0, "Cannot undo a non-existing insertion");
107        char removedChar = chars[j];
108
109        char[] newChars = new char[chars.length - 1];
110        System.arraycopy(chars, 0, newChars, 0, j);
111        System.arraycopy(chars, j + 1, newChars, j, newChars.length - j);
112        chars = newChars;
113
114        int[] newInserts = new int[inserts.length - 1];
115        System.arraycopy(inserts, 0, newInserts, 0, j);
116        for (int i = j + 1; i < inserts.length; i++) {
117            newInserts[i - 1] = inserts[i] - 1;
118        }
119        inserts = newInserts;
120        return removedChar;
121    }
122    
123    public void replaceInsertedChar(int index, char c) {
124        int j = Arrays.binarySearch(inserts, index);
125        Preconditions.checkArgument(j >= 0, "Can only replace chars that were previously inserted");
126        chars[j] = c;
127    }
128}