package nl.bluering;
import java.util.*;
/**
* This library does all basic string funtions
*/
public class StringLib
implements Interpreter
{
public final static int CONCAT=1,SUB=2,STWITH=3,ENDSWITH=4,REVERSE=5;
public final static int TOUPPER=6,TOLOWER=7,LENGTH=8,REPLACE=9;
public final static int TRIM=10,CHARAT=11,INDEXOF=12,EQUALS=13,LASTINDEX=14,HREF=15;
RootLib root;
StringLib(RootLib r)
{root=r;
mountall();
}
public void setrootlib(RootLib r)
{root=r;
}
void m(String name,int arity,int index,String help)
{root.mount(new functionpointer(name,arity,this,index,help));}
public void mountall()
{
m("concat",2,CONCAT,"glues two strings together");
m("substring",3,SUB,"grabs anything between the given positions");
m("startswith",2,STWITH,"returns true if a1 starts with a2");
m("endswith",2,ENDSWITH,"returns true if a1 ends with a2");
m("reverse",1,REVERSE,"reverses the string");
m("toupper",1,TOUPPER,"convert to upper case");
m("tolower",1,TOLOWER,"convert to lower case");
m("length",1,LENGTH,"get the length of the string");
m("replace",3,REPLACE,"replace all occurences of char a2 in a1 for a3");
m("trim",1,TRIM,"remove spaces etc. on both ends");
m("charat",2,CHARAT,"return ascii value of the character");
m("indexof",2,INDEXOF,"get place number of a2 in a1");
m("stringequals",2,EQUALS,"returns true if the strings are equal");
m("lastindexof",2,LASTINDEX,"get place number of a2 in a1, searching backwards");
m("makehref",2,HREF,"make tag");
}
public Variable run(Variable result,Expression e,int index) throws Exception
{
double d1,d2;
switch(index)
{case CONCAT:
result.set(root.run(result,e.get(0)).getstring()+
root.run(result,e.get(1)).getstring());
break;
case SUB:
result.set(root.run(result,e.get(0)).getstring().substring(
root.run(result,e.get(1)).getint(),
root.run(result,e.get(2)).getint()
));
break;
case STWITH:
result.set(root.run(result,e.get(0)).getstring().startsWith(
root.run(result,e.get(1)).getstring()));
break;
case ENDSWITH:
result.set(root.run(result,e.get(0)).getstring().endsWith(
root.run(result,e.get(1)).getstring()));
break;
case REVERSE:
result.set(new StringBuffer(root.run(result,e.get(0)).getstring()).reverse().toString());
break;
case TOUPPER:
result.set(root.run(result,e.get(0)).getstring().toUpperCase());
break;
case TOLOWER:
result.set(root.run(result,e.get(0)).getstring().toLowerCase());
break;
case LENGTH:
result.set(root.run(result,e.get(0)).getstring().length());
break;
case REPLACE:
result.set(root.run(result,e.get(0)).getstring().replace(
(char)root.run(result,e.get(1)).getint(),
(char)root.run(result,e.get(2)).getint()
));
break;
case TRIM:
result.set(root.run(result,e.get(0)).getstring().trim());
break;
case CHARAT:
result.set(root.run(result,e.get(0)).getstring().charAt(
root.run(result,e.get(1)).getint()
));
break;
case INDEXOF:
result.set(root.run(result,e.get(0)).getstring().indexOf(
(char)root.run(result,e.get(1)).getint()
));
break;
case EQUALS:
result.set(root.run(result,e.get(0)).getstring().equals(
root.run(result,e.get(0)).getstring()
));
break;
case LASTINDEX:
result.set(root.run(result,e.get(0)).getstring().lastIndexOf(
(char)root.run(result,e.get(1)).getint()
));
break;
case HREF:
String target=root.run(result,e.get(0)).getstring();
String text=root.run(result,e.get(1)).getstring();
result.set(""+text+"");
break;
}
return result;
}
}