123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- package lib;
- import java.util.ArrayList;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- 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", "call", "function" };
- 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 )
- {
- System.out.print( s + " -> " );
- 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>";
- System.out.println( ret );
- return ret;
- }
-
- public static String[] getVariables( String s )
- {
- ArrayList<String> list = new ArrayList<>();
- Matcher m = Pattern.compile( "<font color=#3BB9FF>(.*?)</font>" ).matcher( s );
- while( m.find())
- {
- list.add( s.substring( m.start( 1 ), m.end( 1 ) ) );
- }
- return list.toArray( new String[ list.size() ] );
- }
- }
|