Debian Codegraph @

rhino/toolsrc/org/mozilla/javascript/tools/idswitch/FileBody.java

raw symbols SCIP
1 /* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 package org.mozilla.javascript.tools.idswitch;
7
8 import java.io.IOException;
9 import java.io.Reader;
10 import java.io.Writer;
11
12 public class FileBody {
13
14 private static class ReplaceItem {
15 ReplaceItem next;
16 int begin;
17 int end;
18 String replacement;
19
20 ReplaceItem(int begin, int end, String text) {
21 this.begin = begin;
22 this.end = end;
23 this.replacement = text;
24 }
25 }
26
27 private char[] buffer = new char[1 << 14]; // 16K
28 private int bufferEnd;
29 private int lineBegin;
30 private int lineEnd;
31 private int nextLineStart;
32
33 private int lineNumber;
34
35 ReplaceItem firstReplace;
36 ReplaceItem lastReplace;
37
38
39 public char[] getBuffer() { return buffer; }
40
41 public void readData(Reader r) throws IOException {
42 int capacity = buffer.length;
43 int offset = 0;
44 for (;;) {
45 int n_read = r.read(buffer, offset, capacity - offset);
46 if (n_read < 0) { break; }
47 offset += n_read;
48 if (capacity == offset) {
49 capacity *= 2;
50 char[] tmp = new char[capacity];
51 System.arraycopy(buffer, 0, tmp, 0, offset);
52 buffer = tmp;
53 }
54 }
55 bufferEnd = offset;
56 }
57
58 public void writeInitialData(Writer w) throws IOException {
59 w.write(buffer, 0, bufferEnd);
60 }
61
62 public void writeData(Writer w) throws IOException {
63 int offset = 0;
64 for (ReplaceItem x = firstReplace; x != null; x = x.next) {
65 int before_replace = x.begin - offset;
66 if (before_replace > 0) {
67 w.write(buffer, offset, before_replace);
68 }
69 w.write(x.replacement);
70 offset = x.end;
71 }
72 int tail = bufferEnd - offset;
73 if (tail != 0) {
74 w.write(buffer, offset, tail);
75 }
76 }
77
78 public boolean wasModified() { return firstReplace != null; }
79
80 public boolean setReplacement(int begin, int end, String text) {
81 if (equals(text, buffer, begin, end)) { return false; }
82
83 ReplaceItem item = new ReplaceItem(begin, end, text);
84 if (firstReplace == null) {
85 firstReplace = lastReplace = item;
86 }
87 else if (begin < firstReplace.begin) {
88 item.next = firstReplace;
89 firstReplace = item;
90 }
91 else {
92 ReplaceItem cursor = firstReplace;
93 ReplaceItem next = cursor.next;
94 while (next != null) {
95 if (begin < next.begin) {
96 item.next = next;
97 cursor.next = item;
98 break;
99 }
100 cursor = next;
101 next = next.next;
102 }
103 if (next == null) {
104 lastReplace.next = item;
105 }
106 }
107
108 return true;
109 }
110
111 public int getLineNumber() { return lineNumber; }
112
113 public int getLineBegin() { return lineBegin; }
114
115 public int getLineEnd() { return lineEnd; }
116
117 public void startLineLoop() {
118 lineNumber = 0;
119 lineBegin = lineEnd = nextLineStart = 0;
120 }
121
122 public boolean nextLine() {
123 if (nextLineStart == bufferEnd) {
124 lineNumber = 0; return false;
125 }
126 int i; int c = 0;
127 for (i = nextLineStart; i != bufferEnd; ++i) {
128 c = buffer[i];
129 if (c == '\n' || c == '\r') { break; }
130 }
131 lineBegin = nextLineStart;
132 lineEnd = i;
133 if (i == bufferEnd) {
134 nextLineStart = i;
135 }
136 else if (c == '\r' && i + 1 != bufferEnd && buffer[i + 1] == '\n') {
137 nextLineStart = i + 2;
138 }
139 else {
140 nextLineStart = i + 1;
141 }
142 ++lineNumber;
143 return true;
144 }
145
146 private static boolean equals(String str, char[] array, int begin, int end)
147 {
148 if (str.length() == end - begin) {
149 for (int i = begin, j = 0; i != end; ++i, ++j) {
150 if (array[i] != str.charAt(j)) { return false; }
151 }
152 return true;
153 }
154 return false;
155 }
156
157 }
158
159