Mercurial > hg > Members > nobuyasu > TestNeo4j
view src/howtouse/CharReader.java @ 17:e30f2714686b draft
fix CharReader.java
author | one |
---|---|
date | Thu, 23 Aug 2012 17:36:46 +0900 |
parents | bf7863a55cd6 |
children | 2c3a10047ec6 |
line wrap: on
line source
package howtouse; public class CharReader { final static char EOFchar = (char) 0; private String text; private int textLength; private int index; private final char LBRANK = '['; private final char RBRANK = ']'; private final char VERBAR = '|'; private final char COLON= ':'; CharReader() { } public void setText(String str) { text = str; textLength = text.length(); index = 0; } char nextChar() { if (index < textLength) return text.charAt(index++); return EOFchar; } String getToken() { int nextState = 1; StringBuffer buf = new StringBuffer(256); char ch; int index = -1; while (true) { ch = nextChar(); if (ch == EOFchar) return null; switch (nextState) { case 1: if (ch == LBRANK) nextState = 2; break; case 2: if (ch == LBRANK) nextState = 3; else nextState = 1; break; case 3: if (ch == RBRANK) { nextState = 4; } else if (ch == VERBAR) { index = buf.length(); buf.append(ch); return buf.substring(0,index); } else if (ch == COLON) { index = 0; buf.delete(0,buf.length()); } else { buf.append(ch); } break; case 4: if (ch == RBRANK) { if (index == -1) { return buf.toString(); } else{ return buf.substring(0,index); } } else { return null; } } } } }