Mercurial > projects > dwt-addons
diff dwtx/jface/text/rules/BufferedRuleBasedScanner.d @ 129:eb30df5ca28b
Added JFace Text sources
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 23 Aug 2008 19:10:48 +0200 |
parents | |
children | c4fb132a086c |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dwtx/jface/text/rules/BufferedRuleBasedScanner.d Sat Aug 23 19:10:48 2008 +0200 @@ -0,0 +1,139 @@ +/******************************************************************************* + * Copyright (c) 2000, 2008 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + * Port to the D programming language: + * Frank Benoit <benoit@tionex.de> + *******************************************************************************/ + +module dwtx.jface.text.rules.BufferedRuleBasedScanner; + +import dwt.dwthelper.utils; + + +import dwtx.core.runtime.Assert; +import dwtx.jface.text.BadLocationException; +import dwtx.jface.text.IDocument; + +/** + * A buffered rule based scanner. The buffer always contains a section + * of a fixed size of the document to be scanned. Completely adheres to + * the contract of <code>RuleBasedScanner</code>. + */ +public class BufferedRuleBasedScanner : RuleBasedScanner { + + /** The default buffer size. Value = 500 */ + private final static int DEFAULT_BUFFER_SIZE= 500; + /** The actual size of the buffer. Initially set to <code>DEFAULT_BUFFER_SIZE</code> */ + private int fBufferSize= DEFAULT_BUFFER_SIZE; + /** The buffer */ + private char[] fBuffer= new char[DEFAULT_BUFFER_SIZE]; + /** The offset of the document at which the buffer starts */ + private int fStart; + /** The offset of the document at which the buffer ends */ + private int fEnd; + /** The cached length of the document */ + private int fDocumentLength; + + + /** + * Creates a new buffered rule based scanner which does + * not have any rule and a default buffer size of 500 characters. + */ + protected BufferedRuleBasedScanner() { + super(); + } + + /** + * Creates a new buffered rule based scanner which does + * not have any rule. The buffer size is set to the given + * number of characters. + * + * @param size the buffer size + */ + public BufferedRuleBasedScanner(int size) { + super(); + setBufferSize(size); + } + + /** + * Sets the buffer to the given number of characters. + * + * @param size the buffer size + */ + protected void setBufferSize(int size) { + Assert.isTrue(size > 0); + fBufferSize= size; + fBuffer= new char[size]; + } + + /** + * Shifts the buffer so that the buffer starts at the + * given document offset. + * + * @param offset the document offset at which the buffer starts + */ + private void shiftBuffer(int offset) { + + fStart= offset; + fEnd= fStart + fBufferSize; + if (fEnd > fDocumentLength) + fEnd= fDocumentLength; + + try { + + String content= fDocument.get(fStart, fEnd - fStart); + content.getChars(0, fEnd - fStart, fBuffer, 0); + + } catch (BadLocationException x) { + } + } + + /* + * @see RuleBasedScanner#setRange(IDocument, int, int) + */ + public void setRange(IDocument document, int offset, int length) { + + super.setRange(document, offset, length); + + fDocumentLength= document.getLength(); + shiftBuffer(offset); + } + + /* + * @see RuleBasedScanner#read() + */ + public int read() { + fColumn= UNDEFINED; + if (fOffset >= fRangeEnd) { + ++ fOffset; + return EOF; + } + + if (fOffset is fEnd) + shiftBuffer(fEnd); + else if (fOffset < fStart || fEnd < fOffset) + shiftBuffer(fOffset); + + return fBuffer[fOffset++ - fStart]; + } + + /* + * @see RuleBasedScanner#unread() + */ + public void unread() { + + if (fOffset is fStart) + shiftBuffer(Math.max(0, fStart - (fBufferSize / 2))); + + --fOffset; + fColumn= UNDEFINED; + } +} + +