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", "false", "true", "undefined", "with" }; 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 setupPseudoCodeComment( String s ) { return ""+s+""; } public static String setupPseudoCode( String s, String[] vars ) { if( s.startsWith( "--" ) ) return setupPseudoCodeComment( s ); 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" ); } current = current.replaceAll( "\\'(.*?)\\'", "'$1'" ); ret += current + ""; return ret; } public static String[] getVariables( String s ) { ArrayList list = new ArrayList<>(); Matcher m = Pattern.compile( "(.*?)" ).matcher( s ); while( m.find()) { if( !list.contains( s.substring( m.start( 1 ), m.end( 1 ) ) ) ) list.add( s.substring( m.start( 1 ), m.end( 1 ) ) ); } return list.toArray( new String[ list.size() ] ); } }