129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13
|
|
14 module dwtx.jface.text.rules.BufferedRuleBasedScanner;
|
|
15
|
|
16 import dwt.dwthelper.utils;
|
|
17
|
|
18
|
|
19 import dwtx.core.runtime.Assert;
|
|
20 import dwtx.jface.text.BadLocationException;
|
|
21 import dwtx.jface.text.IDocument;
|
|
22
|
|
23 /**
|
|
24 * A buffered rule based scanner. The buffer always contains a section
|
|
25 * of a fixed size of the document to be scanned. Completely adheres to
|
|
26 * the contract of <code>RuleBasedScanner</code>.
|
|
27 */
|
|
28 public class BufferedRuleBasedScanner : RuleBasedScanner {
|
|
29
|
|
30 /** The default buffer size. Value = 500 */
|
|
31 private final static int DEFAULT_BUFFER_SIZE= 500;
|
|
32 /** The actual size of the buffer. Initially set to <code>DEFAULT_BUFFER_SIZE</code> */
|
|
33 private int fBufferSize= DEFAULT_BUFFER_SIZE;
|
|
34 /** The buffer */
|
|
35 private char[] fBuffer= new char[DEFAULT_BUFFER_SIZE];
|
|
36 /** The offset of the document at which the buffer starts */
|
|
37 private int fStart;
|
|
38 /** The offset of the document at which the buffer ends */
|
|
39 private int fEnd;
|
|
40 /** The cached length of the document */
|
|
41 private int fDocumentLength;
|
|
42
|
|
43
|
|
44 /**
|
|
45 * Creates a new buffered rule based scanner which does
|
|
46 * not have any rule and a default buffer size of 500 characters.
|
|
47 */
|
|
48 protected BufferedRuleBasedScanner() {
|
|
49 super();
|
|
50 }
|
|
51
|
|
52 /**
|
|
53 * Creates a new buffered rule based scanner which does
|
|
54 * not have any rule. The buffer size is set to the given
|
|
55 * number of characters.
|
|
56 *
|
|
57 * @param size the buffer size
|
|
58 */
|
|
59 public BufferedRuleBasedScanner(int size) {
|
|
60 super();
|
|
61 setBufferSize(size);
|
|
62 }
|
|
63
|
|
64 /**
|
|
65 * Sets the buffer to the given number of characters.
|
|
66 *
|
|
67 * @param size the buffer size
|
|
68 */
|
|
69 protected void setBufferSize(int size) {
|
|
70 Assert.isTrue(size > 0);
|
|
71 fBufferSize= size;
|
|
72 fBuffer= new char[size];
|
|
73 }
|
|
74
|
|
75 /**
|
|
76 * Shifts the buffer so that the buffer starts at the
|
|
77 * given document offset.
|
|
78 *
|
|
79 * @param offset the document offset at which the buffer starts
|
|
80 */
|
|
81 private void shiftBuffer(int offset) {
|
|
82
|
|
83 fStart= offset;
|
|
84 fEnd= fStart + fBufferSize;
|
|
85 if (fEnd > fDocumentLength)
|
|
86 fEnd= fDocumentLength;
|
|
87
|
|
88 try {
|
|
89
|
|
90 String content= fDocument.get(fStart, fEnd - fStart);
|
|
91 content.getChars(0, fEnd - fStart, fBuffer, 0);
|
|
92
|
|
93 } catch (BadLocationException x) {
|
|
94 }
|
|
95 }
|
|
96
|
|
97 /*
|
|
98 * @see RuleBasedScanner#setRange(IDocument, int, int)
|
|
99 */
|
|
100 public void setRange(IDocument document, int offset, int length) {
|
|
101
|
|
102 super.setRange(document, offset, length);
|
|
103
|
|
104 fDocumentLength= document.getLength();
|
|
105 shiftBuffer(offset);
|
|
106 }
|
|
107
|
|
108 /*
|
|
109 * @see RuleBasedScanner#read()
|
|
110 */
|
|
111 public int read() {
|
|
112 fColumn= UNDEFINED;
|
|
113 if (fOffset >= fRangeEnd) {
|
|
114 ++ fOffset;
|
|
115 return EOF;
|
|
116 }
|
|
117
|
|
118 if (fOffset is fEnd)
|
|
119 shiftBuffer(fEnd);
|
|
120 else if (fOffset < fStart || fEnd < fOffset)
|
|
121 shiftBuffer(fOffset);
|
|
122
|
|
123 return fBuffer[fOffset++ - fStart];
|
|
124 }
|
|
125
|
|
126 /*
|
|
127 * @see RuleBasedScanner#unread()
|
|
128 */
|
|
129 public void unread() {
|
|
130
|
|
131 if (fOffset is fStart)
|
|
132 shiftBuffer(Math.max(0, fStart - (fBufferSize / 2)));
|
|
133
|
|
134 --fOffset;
|
|
135 fColumn= UNDEFINED;
|
|
136 }
|
|
137 }
|
|
138
|
|
139
|