📄 quotedtext.java
字号:
package lolo.scans;/** A class to scan for quoted text. The quoted text can span more then one line. * * @author <a href="http://www.inf.uos.de/bernd" target="_blank">Bernd Kühl</a> (<a href="mailto:bernd@informatik.uni-osnabrueck.de">bernd@informatik.uni-osnabrueck.de</a>) */public class QuotedText extends Scan { /** Marks if the recognition is after the quote character. */ protected transient boolean inside; public void reset() { if (reset) return; inside = false; last = ' '; // not \ reset = true; } /** The quote character. */ protected final char quote; /** Returns the quote character. * * @return the quote character. */ protected char getQuote() { return quote; } /** Just calls <tt>this('"')</tt>. */ public QuotedText() { this('"'); } /** Constructs a recognizer who uses <tt>quote</tt> as the quote character. * * @param quote the quote character. */ public QuotedText(char quote) { if (quote == '\\') throw new IllegalArgumentException("illegal quote character \\"); this.quote = quote; } /** Holds the last scanned character. Used to handle escaped characters. */ protected transient char last; public State nextChar(char ch) { reset = false; if (!inside) { inside = true; return stateObject.set( ch == quote, // more false // found ); } if (ch == quote && last != '\\') return stateObject.set( false, // more true // found ); last = last == '\\' ? ' ' // not \ : ch ; return stateObject.set ( true, // more false // found ); } /** Returns a hash code value for this object. * * @return a hash code value for this object. */ public int hashCode() { return new Character(quote).hashCode(); } /** Indicates whether some other object is "equal to" this one. * * @return <tt>true</tt> the argument is a <tt>QuotedText</tt> and uses the same quotre character, * <tt>false</tt> otherwise. */ public boolean equals(Object o) { if (o == null || !(o instanceof QuotedText)) return false; return this == o || quote == ((QuotedText) o).quote; } /** Used the collect the unescaped scanned characters. */ protected transient char [] help; public void action(char [] buffer, int off, int len) { if (action != null) { if (help == null) help = new char[len]; if (help.length < len) help = new char[len]; int length = 0; boolean escape = false; for (int i = 1; i < len-1 ; i++) { char ch = buffer[off+i]; if (escape) { switch (ch) { case 'r': help[length++] = '\r'; break; case 'n': help[length++] = '\n'; break; case 'b': help[length++] = '\b'; break; case 't': help[length++] = '\t'; break; case '\\': help[length++] = '\\'; break; case '"': help[length++] = '"'; break; default: help[length++] = '\\'; help[length++] = ch; break; } escape = false; } else if (ch == '\\') escape = true; else help[length++] = ch; } if (escape) throw new RuntimeException("escape is true"); action.action(this, help, 0, length); } } /** Returns a string representation of the object. * * @return a string representation of the object. */ public String toString() { return getClass().getName()+"["+quote+"]"; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -