📄 parser.java
字号:
}
/** Gets the index of the first occurence of any string of array <i>ss</i> ignoring case. */
public int indexOfIgnoreCase(String[] ss)
{ Parser par=new Parser(str,index);
while (par.hasMore())
{ if (par.startsWithIgnoreCase(ss)) return par.getPos();
else par.skipChar();
}
return -1;
}
/** Gets the begin of next line */
public int indexOfNextLine()
{ Parser par=new Parser(str,index);
par.goToNextLine();
int i=par.getPos();
return (i<str.length())? i : -1;
}
// ********************* Starts with *********************
/** Whether next chars equal to a specific String <i>s</i>. */
public boolean startsWith(String s)
{ return str.startsWith(s,index);
}
/** Whether next chars equal to any string of array <i>ss</i>. */
public boolean startsWith(String[] ss)
{ for (int i=0; i<ss.length; i++)
if (str.startsWith(ss[i],index)) return true;
return false;
}
/** Whether next chars equal to a specific String <i>s</i> ignoring case. */
public boolean startsWithIgnoreCase(String s)
{ for (int k=0; k<s.length() && (index+k)<str.length(); k++)
{ if (compareIgnoreCase(s.charAt(k),str.charAt(index+k))!=0) return false;
}
return true;
}
/** Whether next chars equal to any string of array <i>ss</i> ignoring case. */
public boolean startsWithIgnoreCase(String[] ss)
{ for (int i=0; i<ss.length; i++)
{ boolean equal=true;
for (int k=0; k<ss[i].length() && (index+k)<str.length(); k++)
{ if (!(equal=(compareIgnoreCase(ss[i].charAt(k),str.charAt(index+k))==0))) break;
}
if (equal) return true;
}
return false;
}
// ************************ Skips ************************
/** Skips one char */
public Parser skipChar()
{ if(index<str.length()) index++;
return this;
}
/** Skips N chars */
public Parser skipN(int n)
{ index+=n;
if(index>str.length()) index=str.length();
return this;
}
/** Skips all spaces */
public Parser skipWSP()
{ while (index<str.length() && isSP(str.charAt(index))) index++;
return this;
}
/** The same as skipWSP() (for legacy) */
/*public Parser skipSP()
{ return skipWSP();
}*/
/** Skips return lines */
public Parser skipCRLF()
{ while (index<str.length() && isCRLF(str.charAt(index))) index++;
return this;
}
/** Skips white spaces or return lines */
public Parser skipWSPCRLF()
{ while (index<str.length() && isWSPCRLF(str.charAt(index))) index++;
return this;
}
/** Skips any selected chars */
public Parser skipChars(char[] cc)
{ while (index<str.length() && isAnyOf(cc,nextChar())) index++;
return this;
}
/** Skips a continuous string of char and go to the next "blank" char */
public Parser skipString()
{ getString();
return this;
}
// ************************ Gets ************************
/** Gets a continuous string of char and go to the next char */
public String getString()
{ int begin=index;
while (begin<str.length() && !isChar(str.charAt(begin))) begin++;
int end=begin;
while (end<str.length() && isChar(str.charAt(end))) end++;
index=end;
return str.substring(begin,end);
}
/** Gets a string of length <i>len</i> and move over. */
public String getString(int len)
{ int start=index;
index=start+len;
return str.substring(start,index);
}
/** Gets a string of chars separated by any of chars of <i>separators</i> */
public String getWord(char[] separators)
{ int begin=index;
while (begin<str.length() && isAnyOf(separators,str.charAt(begin))) begin++;
int end=begin;
while (end<str.length() && !isAnyOf(separators,str.charAt(end))) end++;
index=end;
return str.substring(begin,end);
}
/** Gets an integer and point to the next char */
public int getInt()
{ return Integer.parseInt(this.getString());
}
/** Gets a double and point to the next char */
public double getDouble()
{ return Double.parseDouble(this.getString());
}
/** Gets all chars until the end of the line (or the end of the parser)
* and go to the next line. */
public String getLine()
{ int end=index;
while (end<str.length() && !isCRLF(str.charAt(end))) end++;
String line=str.substring(index,end);
index=end;
// skip the end of the line (i.e. '\r' OR '\n' OR '\r\n')
if (index<str.length())
{ if (str.startsWith("\r\n",index)) index+=2;
else index++;
}
return line;
}
//********************** Vectors/arrays **********************
/** Gets all string of chars separated by any char belonging to <i>separators</i> */
public Vector getWordVector(char[] separators)
{ Vector list=new Vector();
do { list.addElement(getWord(separators)); } while (hasMore());
return list;
}
/** Gets all string of chars separated by any char belonging to <i>separators</i> */
public String[] getWordArray(char[] separators)
{ Vector list=getWordVector(separators);
String[] array=new String[list.size()];
for (int i=0; i<list.size(); i++) array[i]=(String)list.elementAt(i);
return array;
}
/** Gets all strings */
public Vector getStringVector()
{ Vector list=new Vector();
do { list.addElement(getString()); } while (hasMore());
return list;
}
/** Gets all string */
public String[] getStringArray()
{ Vector list=getStringVector();
String[] array=new String[list.size()];
for (int i=0; i<list.size(); i++) array[i]=(String)list.elementAt(i);
return array;
}
//********************** Quoted Strings **********************
/** Gets a string of chars separated by any of chars in <i>separators</i>
* , skipping any separator inside possible quoted texts. */
public String getWordSkippingQuoted(char[] separators)
{ int begin=index;
while (begin<str.length() && isAnyOf(separators,str.charAt(begin))) begin++;
boolean inside_quoted_string=false;
int end=begin;
while (end<str.length() && (!isAnyOf(separators,str.charAt(end)) || inside_quoted_string))
{ if (str.charAt(end)=='"') inside_quoted_string=!inside_quoted_string;
end++;
}
index=end;
return str.substring(begin,end);
}
/** Gets the first quatable string, that is a normal string, or text in quotes.
* <br>In the latter case, quotes are dropped. */
public String getStringUnquoted()
{ // jump possible "non-chars"
while (index<str.length() && !isChar(str.charAt(index))) index++;
if (index==str.length()) return str.substring(index,index);
// check whether is a quoted string
int next_qmark;
if (str.charAt(index)=='"' && (next_qmark=str.indexOf("\"",index+1))>0)
{ // is quoted text
String qtext=str.substring(index+1,next_qmark);
index=next_qmark+1;
return qtext;
}
else
{ // is not a quoted text
return getString();
}
}
/** Points to the next occurence of <i>char c</i> not in quotes. */
public Parser goToSkippingQuoted(char c)
{ boolean inside_quotes=false;
try
{ while (index<str.length() && (!(nextChar()==c) || inside_quotes))
{ if (nextChar()=='"') inside_quotes=!inside_quotes;
index++;
}
}
catch (RuntimeException e)
{ System.out.println("len= "+str.length());
System.out.println("index= "+index);
throw e;
}
return this;
}
//************************* toString *************************
/** convert the rest of the unparsed chars into a string */
public String toString()
{ return getRemainingString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -