12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package lib;
- public class TextLayoutHelper {
- /**
- * Modifies the given string such that its lenght matches the given length.
- * If the string is too small, whitespace is appended on both sides equally.
- * Then, if the string is too large, it is cut off on the right.
- * @param s the string
- * @param lenght the target length
- * @return the modified string
- */
- public static String strToLen( String s, int lenght )
- {
- while( s.length() < lenght )
- {
- s = " " + s + " ";
- }
- if( s.length() > lenght )
- return s.substring( 0, lenght );
- return s;
- }
-
- private static String[] keywords = { "for", "do", "to", "then", "else", "if", "foreach", "while", "or", "and" };
- private static String[] delimiter = { "\\+", "\\-", "\\[", "\\]", "\\|", " ", "^", "$", "\\=", "\\,", "\\(", "\\;" };
-
- private static String getDelimiterRegex()
- {
- String reg = "(";
- for( String d : delimiter )
- {
- reg += d + "|";
- }
- reg = reg.substring( 0, reg.length() - 1 ) + ")";
- return reg;
- }
-
- public static String setupPseudoCodeStage( String s )
- {
- return "<html><font color=#FDD017>"+s+"</font></html>";
- }
-
- public static String setupPseudoCode( String s, String[] vars )
- {
- String delimiter = getDelimiterRegex();
- String ret = "<html>";
- String current = s.replaceAll( "&", "&" ).replaceAll( "<", "<").replaceAll( ">", ">");
- for( String k : keywords )
- {
- current = current.replaceAll( delimiter + "(" + k + ")" + delimiter, "$1<font color=orange>$2</font>$3" );
- }
- for( String v : vars )
- {
- current = current.replaceAll( delimiter + "(" + v + ")" + delimiter, "$1<font color=#3BB9FF>$2</font>$3" );
- }
- ret += current + "</html>";
- return ret;
- }
- }
|