Skip to content

Instantly share code, notes, and snippets.

@yomusu
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yomusu/51f04d84bb8051f54a8d to your computer and use it in GitHub Desktop.
Save yomusu/51f04d84bb8051f54a8d to your computer and use it in GitHub Desktop.
/****************************************************************
*
*
* this is scanner, light and simple, like one of Java1.5.
*
*
* [example]
*
* LiteScanner s = new LiteScanner( "one,two", ',' );
* while( s.hasNext() )
* System.out.println( s.next() );
*
* [result]
* one
* two
*
*
*/
public class LiteScanner {
/** スキャン先文字列 */
private String source = null;
/** position of scan */
private int pos = -1;
/** 分割文字 */
private char divider = ',';
/******************************************
*
* Constructor
*
* @param text 解析文字列(nullの場合、hasNextでfalseが返る)
* @param divider 分割文字
*
*/
public LiteScanner( String text, char divider ) {
this.source = text;
this.divider = divider;
}
/******************************************
*
* 文字列を後で設定するコンストラクタ
* @param divider
*/
public LiteScanner( char divider ) {
this.divider = divider;
}
/******************************************
*
* カンマ区切りのコンストラクタ
* 解析文字列は後で指定
*
*/
public LiteScanner() {
}
/*****************************************
*
* 文字列を再設定する。スキャン位置はリセット。
* Scannerのインスタンスを使いまわしたい時に使おう
*
* @param text
*/
public void set( String text ) {
this.source = text;
reset();
}
/*****************************************
*
* 次の文字列があるかどうかを判定する
*
* 文字列がnullか、終端に達しているかした場合、false
* 文字列が空文字の場合、falseを返します。ホントはEmptyって例外を出したほうが良いのかも
*
* @return
*/
public boolean hasNext() {
if( source!=null && source.length() > 0 ) {
if( source.length() > pos )
return true;
}
return false;
}
/****************************************
*
* 次の区切りまでの文字列を取得する
* 解析すべき文字列の終端に達した時はnullを返します
*
* @return
*/
public String next() {
if( hasNext() ) {
if( pos < 0 ) {
// 最初のnext
pos=0;
// 先頭が区切り文字の場合の考慮
if( source.charAt(0)==divider )
return "";
} else {
// 現在、区切り文字なら一文字進める
if( source.charAt(pos)==divider )
pos++;
}
// 切り出し開始位置
int start = pos;
// 文字列の最後までループ
while( source.length() > pos ) {
// 但し区切り文字が来たらその場で中止
char c = source.charAt(pos);
if( c == divider )
break;
pos++;
}
// 文字列を切り出して返す
return source.substring( start, pos );
}
return null;
}
/************************************************
*
*
* 次の区切り文字まで空読みする
*
*/
public void skip() {
if( hasNext() ) {
if( pos < 0 ) {
// 最初のnext
pos=0;
// 先頭が区切り文字の場合の考慮
if( source.charAt(0)==divider )
return;
} else {
// 現在、区切り文字なら一文字進める
if( source.charAt(pos)==divider )
pos++;
}
// 文字列の最後までループ
while( source.length() > pos ) {
// 但し区切り文字が来たらその場で中止
char c = source.charAt(pos);
if( c == divider )
break;
pos++;
}
return;
}
}
/**********************************************
*
* 解析位置をリセットする
*
*/
public void reset() {
pos = -1;
}
/*******
* 現在のカーソル位置から最後まで指定されたバッファに転写する
* バッファが足りないとArrayIndexBoundをThrowします
* @param buf
* @return
*/
public String[] toArray( String[] buf ) {
int i=0;
while( hasNext() ) {
buf[i] = next();
i++;
}
return buf;
}
/******
*
* 何かで区切られた文字列からArrayListを作り出します
*
* @param str
* @param sepa
* @return
*/
static public ArrayList<String> createListWithString( String str, char sepa ) {
ArrayList<String> z = new ArrayList<String>();
LiteScanner scan = new LiteScanner( str, sepa );
while( scan.hasNext() )
z.add( scan.next() );
return z;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment