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 ""+s+""; } public static String setupPseudoCode( String s, String[] vars ) { String delimiter = getDelimiterRegex(); String ret = ""; String current = s.replaceAll( "&", "&" ).replaceAll( "<", "<").replaceAll( ">", ">"); for( String k : keywords ) { current = current.replaceAll( delimiter + "(" + k + ")" + delimiter, "$1$2$3" ); } for( String v : vars ) { current = current.replaceAll( delimiter + "(" + v + ")" + delimiter, "$1$2$3" ); } ret += current + ""; return ret; } }