1 19 20 package org.netbeans.lib.lexer.inc; 21 22 import javax.swing.BoundedRangeModel ; 23 import org.netbeans.lib.editor.util.AbstractCharSequence; 24 import org.netbeans.lib.editor.util.CharSubSequence; 25 26 33 34 public final class OriginalText extends AbstractCharSequence.StringLike { 35 36 private final CharSequence currentText; 37 38 private final int offset; 39 40 private final int insertedTextLength; 41 42 private final CharSequence removedText; 43 44 private final int origLength; 45 46 public OriginalText(CharSequence currentText, int offset, CharSequence removedText, int insertedTextLength) { 47 this.currentText = currentText; 48 this.offset = offset; 49 this.removedText = (removedText != null) ? removedText : ""; this.insertedTextLength = insertedTextLength; 51 52 this.origLength = currentText.length() - insertedTextLength + this.removedText.length(); 53 } 54 55 public int length() { 56 return origLength; 57 } 58 59 public char charAt(int index) { 60 if (index < offset) { 61 return currentText.charAt(index); 62 } 63 index -= offset; 64 if (index < removedText.length()) { 65 return removedText.charAt(index); 66 } 67 return currentText.charAt(offset + index - removedText.length() + insertedTextLength); 68 } 69 70 public char[] toCharArray(int start, int end) { 71 char[] chars = new char[end - start]; 72 int charsIndex = 0; 73 if (start < offset) { 74 int bound = (end < offset) ? end : offset; 75 while (start < bound) { 76 chars[charsIndex++] = currentText.charAt(start++); 77 } 78 if (end == bound) { 79 return chars; 80 } 81 } 82 start -= offset; 83 end -= offset; 84 int bound = removedText.length(); 85 if (start < bound) { 86 if (end < bound) { 87 bound = end; 88 } 89 while (start < bound) { 90 chars[charsIndex++] = removedText.charAt(start++); 91 } 92 if (end == bound) { 93 return chars; 94 } 95 } 96 bound = offset - removedText.length() + insertedTextLength; 97 start += bound; 98 bound += end; 99 while (start < bound) { 100 chars[charsIndex++] = currentText.charAt(start++); 101 } 102 return chars; 103 } 104 105 } 106 | Popular Tags |