import java.util.*; import java.util.zip.*; import java.util.List; import java.util.regex.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; import java.util.concurrent.locks.*; import javax.swing.*; import javax.swing.event.*; import javax.swing.text.*; import javax.swing.table.*; import java.io.*; import java.net.*; import java.lang.reflect.*; import java.lang.ref.*; import java.lang.management.*; import java.security.*; import java.security.spec.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import java.math.*; import javax.net.ssl.*; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.text.SimpleDateFormat; import java.text.NumberFormat; import java.text.DecimalFormat; public class main { static interface Function { public Object process(Object in); public void toJava_process(Code code); } static interface ReversibleFunction extends Function { public Object unprocess(Object in); public void toJava_unprocess(Code code); } // generic learner (works on objects) static interface Learner { public void processInOut(Object in, Object out); public Object processIn(Object in); public void toJava(Code code); public void tryAgain(); } abstract static class Base { void printVars() { StringBuilder buf = new StringBuilder(); Field[] fields = getClass().getDeclaredFields(); for (Field field : fields) { if ((field.getModifiers() & Modifier.STATIC) != 0) continue; Object value; try { value = field.get(this); } catch (Exception e) { value = "?"; } if (main.nempty(buf)) buf.append(", "); buf.append(field.getName() + "=" + value); } System.out.println(buf.toString()); } String name() { return getClass().getName().replaceAll("^main\\$", ""); } void debug(String s) { System.out.println(name() + ": " + s); } } abstract static class LearnerImpl extends Base implements Learner { public void tryAgain() { throw new RuntimeException("No try again"); } public void toJava(Code code) { main.todo(); } } abstract static class FunctionImpl extends Base implements Function { public void toJava_process(Code code) { main.todo(); } } abstract static class ReversibleFunctionImpl extends FunctionImpl implements ReversibleFunction { public void toJava_unprocess(Code code) { main.todo(); } } static class Code { StringBuilder buf = new StringBuilder(); String var = "in"; String indent = ""; List translators = new ArrayList(); List varStack = new ArrayList(); int varCounter; Code() { translators.add("!636"); } void line(String line) { buf.append(indent).append(line).append('\n'); } void indent() { indent += " "; } void unindent() { indent = indent.substring(0, indent.length()-2); } void translators(String... ids) { for (String id : ids) if (! translators.contains(id)) // space is needed otherwise translator 636 is fooled :) translators.add(id); } String getTranslators() { // TODO: We should really fix the "standard functions" translator // to properly find the main class. // // As a hack, we move it upwards (before classes adding) int i = translators.indexOf("!standard functions"); if (i >= 0) { translators.remove(i); translators.add(0, "!standard functions"); } return main.fromLines(translators); } String s() { return "((String) " + var + ")"; } String list() { return "((List) " + var + ")"; } void assign(String exp) { line(var + " = " + exp + ";"); } void newVar() { varStack.add(var); var = "_v" + (++varCounter); line("Object " + var + ";"); } void oldVar() { var = varStack.get(varStack.size()-1); varStack.remove(varStack.size()-1); } } // The following field is filled by JavaX. static Object androidContext; static final String ALL = "#681"; static final String DEFAULT_STRATEGY = "#1000464"; static int maxCaseSize = 100000; static boolean classic = false; static List cases = new ArrayList(); static HashMap casesByID = new HashMap(); static boolean testJava = false, showFails, execTasks = false; static boolean collectClosest = false; // saves time static boolean verboseMonitor = false, extensiveMode = false; static boolean verbose = false; static HashSet parseErrors = new HashSet(); static int caseIdx; static HashSet debuggedClasses = new HashSet(); static List strategyParseErrors = new ArrayList(); static Learner trying; static String user, pass; static boolean edit, stayAlive, printStrategy; static List strategies = new ArrayList(); static List backStrategies = new ArrayList(); static { strategies.add(DEFAULT_STRATEGY); // need to do here because there are multiple entry points, // such as quickSolve() } public static void main(final String[] args) throws Exception { try { keepAlive(); setOpt(getJavaX(), "androidContext", androidContext); long startTime = System.currentTimeMillis(); List snippetIDs = new ArrayList(); List inputs = new ArrayList(); List inputDocs = new ArrayList(); List userCases = new ArrayList(); for (int i = 0; i < args.length; i++) { String arg = args[i]; if (arg.equals("debug")) debugOn(args[++i]); else if (arg.equals("user")) user = args[++i]; else if (arg.equals("pass")) { pass = args[++i]; args[i] = "hidden"; } else if (arg.equals("edit")) edit = true; else if (arg.equals("in")) { String in = args[++i]; //System.out.println("in=" + quote(in)); String quoteUnquote = unquote("\"" + in + "\""); //System.out.println("now in=" + quote(quoteUnquote)); inputs.add(quoteUnquote); } else if (arg.equals("in-doc")) inputDocs.add(args[++i]); else if (arg.equals("-notestjava")) testJava = false; else if (arg.equals("-testjava")) testJava = true; else if (arg.equals("verbose")) verbose = true; else if (arg.equals("closest")) collectClosest = true; else if (arg.equals("-withexec")) execTasks = true; else if (arg.equals("-showfails")) showFails = true; else if (arg.equals("-classic")) classic = true; else if (arg.equals("-stayalive")) stayAlive = true; else if (arg.equals("-printstrategy")) printStrategy = true; else if (arg.equals("strategy")) strategies.add(args[++i]); else if (arg.equals("backstrategy")) backStrategies.add(args[++i]); else if (arg.equals("onlystrategy")) { strategies.clear(); strategies.add(args[++i]); } else if (arg.equals("extensive") || arg.equals("-extensive")) extensiveMode = true; else if (arg.equals("big")) maxCaseSize = Integer.MAX_VALUE; else if (arg.equals("all")) snippetIDs.add(ALL); else if (isSnippetID(arg)) snippetIDs.add(arg); else System.err.println("Unknown argument: " + arg + ", ignoring"); } strategies.addAll(backStrategies); backStrategies.clear(); if (snippetIDs.isEmpty()) { String s = loadTextFile("input/input.txt", null); if (s != null) parse(s); else parse(ALL); } for (String snippetID : snippetIDs) try { Case _case = parse(snippetID); if (_case != null) { _case.halfExamples.addAll(inputs); for (String docID : inputDocs) _case.halfExamples.add(loadSnippet(docID)); userCases.add(_case); } } catch (Throwable e) { e.printStackTrace(); parseErrors.add(snippetID); } int solved = 0, n = cases.size(), goodJava = 0; for (caseIdx = 0; caseIdx < n; caseIdx++) { Case _case = cases.get(caseIdx); try { calculate(_case); } catch (Throwable e) { e.printStackTrace(); } if (_case.winner != null) ++solved; if (_case.goodJava) ++goodJava; if (caseIdx+1 == solved) System.out.println((caseIdx+1) + " case(s) processed & solved."); else System.out.println((caseIdx+1) + " case(s) processed, " + solved + " solved."); if (testJava && goodJava < solved) System.out.println((solved-goodJava) + " case(s) with BAD JAVA."); } print(""); print("----"); List sorted = casesSortedByID(); boolean allSolved = solved == n; if (!allSolved) { System.out.println(); System.out.println("Unsolved:"); for (Case _case : sorted) if (_case.winner == null) System.out.println(" " + _case.id + (_case.name == null ? "" : " - " + _case.name)); } if (solved != 0) { System.out.println(); System.out.println("Solved:"); for (Case _case : sorted) if (_case.winner != null) System.out.println(" " + _case.id + (_case.name == null ? "" : " - " + _case.name)); } if (testJava && solved > goodJava) { System.out.println(); System.out.print("Bad Java: "); for (Case _case : sorted) if (_case.winner != null && !_case.goodJava) System.out.print(_case.id + " "); System.out.println(); } if (!parseErrors.isEmpty()) { System.out.print("\nParse errors: "); for (String id : parseErrors) System.out.print(id + " "); System.out.println(); } if (!strategyParseErrors.isEmpty()) System.out.println("Strategy parse errors!"); System.out.println(); if (allSolved && testJava && goodJava < solved) System.out.println("ALL SOLVED (" + solved + "), but some BAD JAVA."); else { System.out.println(allSolved ? "ALL SOLVED (" + solved + ")" : "Solved " + solved + " out of " + n + "."); if (testJava) if (goodJava == solved) System.out.println("All Java code OK" + (allSolved ? "" : " (for solved cases)") + "."); else System.out.println("Some bad Java."); else System.out.println("Java not tested."); } print(""); if (edit) { if (userCases.size() != 1) throw fail("Edit: More than one user case, confused"); Case _case = userCases.get(0); for (String docID : inputDocs) { Object _out = _case.processIn(loadSnippet(docID)); if (!(_out instanceof String)) throw fail("Case did not generate a string: " + structure(_out)); String out = (String) _out; String editInfo = "Solver #" + programID() + " " + join(" ", args); editSnippetText(docID, out, editInfo); } } /*long time = getUserTime(); if (time >= 0) System.out.println("User time: " + formatDouble(time/1e9, 3) + "s");*/ long time = System.currentTimeMillis()-startTime; System.out.println("Real time: " + formatDouble(time/1e3, 3) + "s"); System.gc(); Runtime runtime = Runtime.getRuntime(); System.out.println("Used memory: " + (runtime.totalMemory() - runtime.freeMemory()+1024*1024-1) / (1024*1024) + " MB" + ", total: " + (runtime.totalMemory()+1024*1024-1)/(1024*1024) + " MB"); if (stayAlive) { System.out.println("Staying alive."); Thread.sleep(1000*60*60*24); // for VisualVM and such } } catch (Throwable e) { e.printStackTrace(); } } // end of main method public static String formatDouble(double d, int digits) { String format = "0."; for (int i = 0; i < digits; i++) format += "#"; String s = new DecimalFormat(format).format(d); return s.replace(',', '.'); // hack german -> english } /** Get user time in nanoseconds. */ public static long getUserTime( ) { ThreadMXBean bean = ManagementFactory.getThreadMXBean(); return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadUserTime() : -1L; } static class Case { String id, name; List fullExamples = new ArrayList(); List halfExamples = new ArrayList(); List examples1, examples2; Learner winner; RunnersUp runnersUp = collectClosest ? new RunnersUp() : null; boolean goodJava = false; List combined; int splitPoint = -1; // stats int learnersTried, retries; void split() { if (examples1 != null) return; // already done if (fullExamples.isEmpty()) throw new RuntimeException("No full examples"); if (splitPoint < 0) splitPoint = fullExamples.size()-1; System.out.println("Full examples: " + fullExamples.size() + ", splitPoint: " + splitPoint + (halfExamples.isEmpty() ? "" : ", half examples: " + halfExamples.size())); examples1 = fullExamples.subList(0, splitPoint); examples2 = fullExamples.subList(splitPoint, fullExamples.size()); } void add(Case _case) { combined.add(_case); fullExamples.addAll(_case.fullExamples); halfExamples.addAll(_case.halfExamples); } Object processIn(Object in) { return winner.processIn(in); } int size() { return structureSize(fullExamples); } static int structureSize(Object o) { if (o == null) return 0; if (o instanceof String) return ((String) o).length(); int size = 0; if (o instanceof Collection) for (Object x : (Collection) o) size += structureSize(x); else if (o.getClass().isArray()) { int n = Array.getLength(o); for (int i = 0; i < n; i++) size += structureSize(Array.get(o, i)); } return size; } void checkSize() { int size = size(); if (size > maxCaseSize) throw fail("Ignoring case - too big: " + id); } boolean solved() { return winner != null; } } static Case parse(String arg) throws Exception { try { if (arg == null) return null; arg = arg.trim(); if (arg.length() == 0) return null; Case _case = new Case(); String text; if (casesByID.containsKey(arg)) return casesByID.get(arg); if (arg.startsWith("Combine")) { _case.id = "Combine"; _case.combined = new ArrayList(); List tok = javaTok(arg); List ids = new ArrayList(); for (int i = 5; i < tok.size(); i += 6) { // skip # and "and" if (verbose) System.out.println("Combine: Parsing " + tok.get(i)); Case case2 = parse("#" + tok.get(i)); _case.id += " #" + tok.get(i); cases.remove(case2); _case.add(case2); } addCase(_case); return _case; } if (verbose) System.out.println("parse: testing snippet ID"); if (isSnippetID(arg)) { _case.id = arg; String[] x = loadSnippetAndTitle(arg); text = x[0]; _case.name = x[1]; } else { _case.id = "direct (" + shorten(arg, 10) + ")"; text = arg; } if (verbose) System.out.println("parse: testing snippet ID done " + quote(text)); // it's a collection of cases! if (text.trim().startsWith("#") || text.trim().startsWith("Combine")) { for (String line : toLines(text)) if (isSnippetID(line)) parse(line); else if (line.trim().length() != 0) throw fail("Unknown line: " + line); return null; } // it's a "Continue:" task - transform to I/O format if (text.trim().startsWith("Continue:")) { List lines = toLines(text); StringBuilder buf = new StringBuilder(); for (int i = 1; i < lines.size(); i++) { buf.append("In: " + quote("" + i) + "\n"); buf.append("Out: " + quote(lines.get(i)) + "\n"); } int numAsking = 3; for (int i = lines.size(); i < lines.size()+numAsking; i++) buf.append("In: " + quote("" + i) + "\n"); text = buf.toString(); } // it's an "Execute." task - run Java(X) and transform to I/O format if (text.trim().startsWith("Execute.")) { if (!execTasks) return null; List tok = javaTok(text); StringBuilder buf = new StringBuilder(); for (int i = 5; i < tok.size(); i += 2) { if (tok.get(i).equals("-") && tok.get(i+2).equals("-")) { i += 2; buf.append("--\n"); } else { String code = unquote_fixSpaces(tok.get(i)); String result = execute("!636\n" + code); buf.append("In: " + quote(code) + "\n"); buf.append("Out: " + quote(result) + "\n"); } } text = buf.toString(); } if (text.trim().startsWith("Marking.")) { List tok = javaTok(text); StringBuilder buf = new StringBuilder(); if (verbose) System.out.println("Is marking."); for (int i = 5; i < tok.size(); i += 2) { if (tok.get(i).equals("-") && tok.get(i+2).equals("-")) { i += 2; buf.append("--\n"); } else if (tok.get(i).equals("Doc") && tok.get(i+2).equals(":") && tok.get(i+4).equals("#")) { if (verbose) System.out.println("Loading subsnippet: " + tok.get(i+6)); String out = loadSnippet(tok.get(i+6)); i += 6; String in = out.replace("[[", "").replace("]]", ""); buf.append("In: " + quote(in) + "\n"); buf.append("Out: " + quote(out) + "\n"); } else { String out = unquote_fixSpaces(tok.get(i)); String in = out.replace("[[", "").replace("]]", ""); buf.append("In: " + quote(in) + "\n"); buf.append("Out: " + quote(out) + "\n"); } } text = buf.toString(); } //System.out.println(text); String in = null; if (verbose) System.out.println("parse: JavaToking " + quote(text)); // parse a standard IOIOI document (Ins and Outs) List tok = javaTok(text); for (int i = 1; i < tok.size(); i += 2) { String t = tok.get(i), t2 = i+2 < tok.size() ? tok.get(i+2) : ""; if (t.equalsIgnoreCase("Cmd") && t2.equals("-") && tok.get(i+4).equalsIgnoreCase("Hint") && tok.get(i+6).equals(":")) { // "Cmd-Hint:" i += 8; int j = findNextLine(tok, i); String cmdHint = unquote_fixSpaces(join(tok.subList(i, j-1))); // result unused as of now i = j-2; } else if (t.equals("-") && t2.equals("-")) { i += 2; _case.splitPoint = _case.fullExamples.size(); //System.out.println("t=" + t + ", t2= " + t2); } else if (t.toUpperCase().startsWith("I") && t2.equals("-") && tok.get(i+4).toUpperCase().equals("DOC") && tok.get(i+6).equals(":")) { // "In-Doc:" if (in != null) _case.halfExamples.add(in); i += 8; int j = findNextLine(tok, i); String inID = unquote_fixSpaces(join(tok.subList(i, j-1))); in = loadSnippet(inID); i = j-2; } else if (t.toUpperCase().startsWith("I") && t2.equals(":")) { // "In:" or "I:" if (in != null) _case.halfExamples.add(in); i += 4; int j = findNextLine(tok, i); in = unquote_fixSpaces(join(tok.subList(i, j-1))); i = j-2; } else if ((swic(t, "O") || eqic(t, "Expect")) && t2.equals(":")) { // "Out: " or "O: " or "Expect: " i += 4; int j = findNextLine(tok, i); String out = unquote_fixSpaces(join(tok.subList(i, j-1))); i = j-2; if ((in + out).indexOf('\t') >= 0) System.err.println("WARNING: Tab character used!"); System.out.println(shorten(quote(in), 80) + " => " + shorten(quote(out), 80)); _case.fullExamples.add(new Object[] {in, out}); _case.checkSize(); in = null; } else if (t.toUpperCase().startsWith("O") && t2.equals("-") && tok.get(i+4).toUpperCase().equals("LIST") && tok.get(i+6).equals(":")) { // "Out-List:" i += 8; if (!tok.get(i).equals("{")) throw fail("Syntax error in Out-List"); i += 2; List outList = new ArrayList(); while (!tok.get(i).equals("}")) { if (tok.get(i).equals(",")) i += 2; if (tok.get(i).equals("}")) break; String listEntry = unquote(tok.get(i)); outList.add(listEntry); i += 2; } System.out.println(shorten(quote(in), 80) + " => list of " + outList.size()); _case.fullExamples.add(new Object[] {in, outList}); _case.checkSize(); in = null; } else { int j = findNextLine(tok, i); String line = join(tok.subList(i, j-1)); i = j-2; System.out.println("-- Ignoring line: " + line); } } if (in != null) _case.halfExamples.add(in); addCase(_case); return _case; } catch (Throwable e) { parseErrors.add(arg); e.printStackTrace(); return null; } } static String unquote_fixSpaces(String s) { String __ = unquote(s); if (s.startsWith("[[")) __ = fixSpaces(__); return __; } // remove invisible spaces at end of line - this will otherwise confuse the engine(s) greatly... static String fixSpaces(String s) { System.out.println(quote(s)); s = s.replaceAll("[\t ]+(\r?\n)", "$1"); s = s.replaceAll("[\t ]+$", ""); //System.out.println("_fixSpaces => " + quote(s)); return s; } // i is a code-token (odd index) // return value is also a code token, or end of list static int findNextLine(List tok, int i) { while (i < tok.size() && tok.get(i-1).indexOf('\n') < 0) i += 2; return i; } static void addCase(Case _case) { cases.add(_case); casesByID.put(_case.id, _case); } static void calculate(Case _case) throws Exception { System.out.println("\n== CASE " + newLinesToBars(_case.id) + " =="); _case.split(); Learner learner = findOKLearner(_case); if (learner == null) { String stat = ""; if (_case.retries != 0) stat = " + " + _case.retries + " retries"; System.out.println("\nProblem not solved (" + _case.learnersTried + " learners tried" + stat + ")"); RunnersUp ru = _case.runnersUp; if (ru != null && ru.winner != null) System.out.println("Closest result: " + quote(ru.bestResult) + " instead of " + quote(ru.expected) + " (score " + ru.bestScore + ") by " + structure(ru.winner)); } else { print("\nSolved!"); structure_Data data = new structure_Data(); data.stringSizeLimit = 40; print(" " + structure(learner, data) + "\n"); _case.winner = learner; String java = null; Code code = new Code(); if (testJava) try { learner.toJava(code); if (testJava) testJava(_case, code); // prints "GOOD JAVA" or "BAD JAVA" else print("Java:"); java = code.getTranslators() + code.buf.toString(); System.out.println(indent(" ", java)); } catch (Throwable e) { print("BAD JAVA"); } if (isSnippetID(_case.id)) updateMyCommentOnSnippet(_case.id, "Solved!\n\nDetails:\n" + structure(learner) + (java == null ? "" : "\n\nJava:\n" + indent(" ", java))); for (Object in : _case.halfExamples) { Object out = learner.processIn(in); System.out.println(structure(in) + " =>! " + structure(out)); } } } static void choice_orderList(List l) { } static Learner findOKLearner(Case _case) throws Exception { List list = makeLearners(_case); choice_orderList(list); for (Learner learner : list) try { if (learnerOK(learner, _case)) return learner; } catch (Throwable e) { if (showFails) e.printStackTrace(); } if (_case.combined != null) { Case switchCase = makeSwitchCase(_case); calculate(switchCase); Learner learner = switchCase.winner; if (learner != null) return new LSwitch(_case, learner); } return null; } static Case makeSwitchCase(Case _case) { int i = 0; Case s = new Case(); s.id = "Switch " + _case.id; s.examples1 = new ArrayList(); s.examples2 = new ArrayList(); for (Case c : _case.combined) { ++i; for (Object[] e : c.examples1) s.examples1.add(new Object[] {e[0], String.valueOf(i)}); for (Object[] e : c.examples2) s.examples2.add(new Object[] {e[0], String.valueOf(i)}); for (Object[] e : c.fullExamples) s.fullExamples.add(new Object[] {e[0], String.valueOf(i)}); } return s; } static boolean learnerOK(Learner learner, Case _case) { trying = learner; try { Object[] _e = null; try { if (_case.examples1 == null) throw fail("Case not calculated: " + _case.id); ++_case.learnersTried; for (Object[] e : _case.examples1) { _e = e; learner.processInOut(e[0], e[1]); } // full validation against all examples - but starting with the unknown ones to make coding easier for learners int retry = 0; retryLoop: while (true) { int n = _case.fullExamples.size(); for (int i = 0; i < n; i++) { Object[] e = _case.fullExamples.get((i+_case.examples1.size()) % n); _e = e; Object out = learner.processIn(e[0]); if (!resultsEqual(e[1], out)) { if (_case.runnersUp != null && e[1] instanceof String && out instanceof String) _case.runnersUp.add((String) e[1], (String) out, leven((String) out, (String) e[1]), learner); if (debuggedClasses.contains(learner.getClass()) || showFails) { System.out.println("[fail] " + structure(learner) + " on " + structure(e[0]) + " - got: " + structure(out) + " rather than: " + structure(e[1])); if (e[1] instanceof String && out instanceof String) System.out.println("Leven distance: " + leven((String) e[1], (String) out)); } ++retry; ++_case.retries; if (retry % 1000 == 0) System.err.println("Retry " + retry); learner.tryAgain(); continue retryLoop; } } return true; // all test examples passed } } catch (Throwable e) { if (debuggedClasses.contains(learner.getClass()) || showFails) { e.printStackTrace(); System.err.println("[fail] " + structure(learner) + " on " + (_e == null ? "?" : structure(_e[0])) + " - " + e); } return false; } } finally { trying = null; } } static boolean resultsEqual(Object a, Object b) { if (a instanceof String && b instanceof String) return ((String) a).replace("\r", "").equals(((String) b).replace("\r", "")); else return a.equals(b); } static boolean validate(String[] example, Learner learner) { try { String out = (String) learner.processIn(example[0]); if (!example[1].equals(out)) { //System.out.println("[fail] " + learner + " on " + quote(e[0]) + " - got: " + quote(out) + " rather than: " + quote(e[1])); return false; } return true; } catch (Throwable e) { silentException(e); return false; } } static void silentException(Throwable e) { } static List makeLearners() { return makeLearners(null); } static List makeLearners(Case _case) { try { List list = new ArrayList(); // process hints in comments on IOIOI snippet if (_case != null && isSnippetID(_case.id)) { List comments = getSnippetComments(_case.id); System.out.println(_case.id + ": " + comments.size() + " comment(s)."); for (Comment c : comments) try { if (verbose) System.out.println("Processing hint: " + c.text); processHint(c.text, list); } catch (Throwable e) { e.printStackTrace(); } } // apply strategies for (String strategy : strategies) { String s = isSnippetID(strategy) ? loadSnippet(strategy) : strategy; s += "\n" + combineClasses(); if (printStrategy) System.out.println("Strategy:\n" + s); list.addAll(parseStrategy(s)); if (classic) list.addAll(classicLearners()); // Endlosschleife!? } // return list return list; } catch (Exception __e) { throw rethrow(__e); } } static void processHint(String text, List list) { List tok = javaTok(text); if (tok.size() > 5) if (tok.get(1).equals("try") && tok.get(3).equals("regexp")) { String re = unquote(tok.get(5)); System.out.println("re: " + re); list.add(new LUse(new FRegExp(re))); } Matcher m = Pattern.compile("(?:LHotwire\\(programID=\"||hotwire\\s+)(#\\d+)").matcher(text); if (m.find()) { String programID = m.group(1); if (isJavaxSafe(programID)) list.add(new LHotwire(programID)); } } static String[] extensiveLearners = { "LFunctionPlusPrevious", "LCombineTwoFunctions", "LCombineTwoFunctions2" }; static List extensiveLearnerClasses; static boolean isExtensive(Class c) { if (extensiveLearnerClasses == null) { extensiveLearnerClasses = new ArrayList(); for (String s : extensiveLearners) { Class cc = findClassFlex(s); if (cc != null) extensiveLearnerClasses.add(cc); } } return extensiveLearnerClasses.contains(c); }; static boolean extensiveOK(Class c) { return extensiveMode || !isExtensive(c); } static String combineClasses() { StringBuilder buf = new StringBuilder(); for (Class c : myNonAbstractClassesImplementing(Learner.class)) { { if (!(extensiveOK(c))) continue; } //System.out.println("combineClasses: " + c); if (hasConstructor(false, c)) buf.append(getName(c) + "\n"); for (String s2 : myInnerClasses()) { Class c2 = findClass(s2); if (c2 == null) continue; if (isAbstract(c2) || !hasConstructor(false, c2)) continue; // no default constructor boolean ctr = hasConstructor(false, c, c2); /*if (c == LUse.class) System.out.println("combineClasses: Checking LUse " + c2+ ", ctr=" + ctr);*/ if (c2 != null && ctr && extensiveOK(c2)) buf.append(getName(c) + " " + getName(c2) + "\n"); } } return buf.toString(); } static List parseStrategy(String strategyText) { try { List lines = toLines(strategyText); List list = new ArrayList(); for (String s : lines) try { s = s.trim(); List parts = javaTok(s); if (parts.size() == 1) continue; Stack stack = new Stack(); for (int i = parts.size()-2; i >= 1; i -= 2) { String p = parts.get(i); if (p.startsWith("\"")) { stack.add(unquote(p)); continue; } if (isInteger(p)) { if (i > 1 && parts.get(i-2).equals("#")) { i -= 2; p = "#" + p; } stack.add(p); continue; } Class c = findClassFlex(p); if (c == null) throw fail("Strategy element " + p + " not known"); Constructor ctr = c.getDeclaredConstructors()[0]; makeAccessible(ctr); Class[] types = ctr.getParameterTypes(); try { if (types.length == 0) stack.add(ctr.newInstance()); else if (types.length == 1) { if (stack.isEmpty()) throw fail("Too few arguments for " + c.getName()); stack.add(ctr.newInstance(stack.pop())); } else if (types.length == 2) { if (stack.size() < 2) throw fail("Too few arguments for " + c.getName()); Object a = stack.pop(); Object b = stack.pop(); stack.add(ctr.newInstance(a, b)); } else throw fail("bla"); } catch (Throwable _e) { print("Was calling constructor: " + ctr); throw rethrow(_e); } } list.add((Learner) stack.peek()); } catch (Throwable e) { print("Strategy parse error."); printIndent("STRAT> ", strategyText); e.printStackTrace(); strategyParseErrors.add(e); } return list; } catch (Exception __e) { throw rethrow(__e); } } static List classicLearners() { List list = new ArrayList(); list.addAll(level1()); list.add(new LBox(new LMulti(level1()))); list.add(new LBox2(new LMulti(level1()))); list.add(new LChange(new FCommonPrefix(), new LOutPattern())); list.add(new LChange(new FCommonSuffix(), new LOutPattern())); list.add(new LEmptyBox(new LMulti(level1()))); list.add(new LChange(new RFToLines(), new LFixedFunction(new FLength()))); // for #1000455 list.add(new LFindOutInIn(new LFixedSubstring())); for (int I = 1; I <= 2; I++) // for #1000457 list.add(new LChange(new FDropFirstLines(I), new LMulti( new LFixWhitespace(l1()), level1()))); // for #1000458 list.add(new LFixWhitespace(l1())); return list; } static Learner l1() { return new LMulti(level1()); } static List level1() { List list = new ArrayList(); list.add(new LId()); // always good to have as included learner list.add(new LPrefixSuffix()); list.add(new LSplitInput(new LOutPattern())); list.add(new LInputPattern()); list.add(new LFixed()); list.add(new LWrap(new RFJavaTok(), new LEach(new LFixedFunction(new EscapeCase())))); list.add(new LCharShift()); list.add(new LOutSuffix(new LFirst(new LCharShift()))); list.add(new LChange(new RFJavaTok(), new LMulti( new LChange(new FStringsOnly(), new LGetListElement()), new LChange(new FNumbersOnly(), new LMath()), new LChange(new FNumbersOnly(), new LMath2()) ))); // TODO - for #1000378 list.add(new LChange(new RFJavaTok(), new LDistinguishList(new FIsString()))); list.add(new LFixedSubstring()); return list; } static void debugOn(String name) { try { Class c = findClassFlex(name); debuggedClasses.add(c); Field field; while (true) try { field = c.getDeclaredField("debug"); break; } catch (NoSuchFieldException e) { c = c.getSuperclass(); } field.setBoolean(null, true); } catch (Exception e) { e.printStackTrace(); System.err.println("Cannot debug class " + name); } } static int choice(String text, int i) { return i; } // splits the input at some point, takes only one part static class LSplitInput extends LearnerImpl { int splitIdx = 1; // split after first character Learner baseLearner; LSplitInput(Learner baseLearner) { this.baseLearner = baseLearner; splitIdx = choice("LSplitInput.i", splitIdx); } public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; in = in.substring(splitIdx); baseLearner.processInOut(in, out); } public Object processIn(Object _in) { String in = (String) _in; in = in.substring(splitIdx); return baseLearner.processIn(in); } public void toJava(Code code) { if (splitIdx != 0) code.line(code.var + " = ((String) " + code.var + ").substring(" + splitIdx + ");"); baseLearner.toJava(code); } } static class FDropFirstLines implements Function { int n; FDropFirstLines(int n) { this.n = n;} public Object process(Object _in) { String in = (String)_in; for (int I=0; I < n; I++) in = in.substring(in.indexOf('\n')+1); return in; } public void toJava_process(Code code) { throw todo(); } } // removes common suffix from out, delegates to base learner // just demo (only removes "!") static class LOutSuffix extends LearnerImpl { String suffixOut = ""; Learner baseLearner; LOutSuffix(Learner baseLearner) { this.baseLearner = baseLearner; } public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; if (out.endsWith("!")) suffixOut = "!"; if (out.endsWith(suffixOut)) out = out.substring(0, out.length()-suffixOut.length()); baseLearner.processInOut(in, out); } public Object processIn(Object _in) { String in = (String) _in; return baseLearner.processIn(in) + suffixOut; } public void toJava(Code code) { baseLearner.toJava(code); if (suffixOut.length() != 0) code.line(code.var + " = " + code.s() + "+" + quote(suffixOut) + ";"); } } // if input appears in output in fixed pattern static class LOutPattern extends LearnerImpl { static boolean debug = false; String pattern = "%!%"; public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; pattern = out.replace(in, "%!%"); if (debug) System.out.println("LOutPattern: in=" + quote(in) + ", out=" + quote(out) + ", pattern=" + quote(pattern)); } public String processIn(Object _in) { String in = (String) _in; return pattern.replace("%!%", in); } public void toJava(Code code) { code.line(code.var + " = " + quote(pattern) + ".replace(" + quote("%!%") + ", (String) " + code.var + ");"); } } // simplets learner - only knows the "id" function static class LId extends LearnerImpl { public void processInOut(Object in, Object out) { } public Object processIn(Object in) { return in; } public void toJava(Code code) { } } // for "find" tasks (e.g. "abcde" to "[[abc]]de") static class LInputPattern extends LearnerImpl { static boolean debug = false; String regexp = "", findMarker1 = "[[", findMarker2 = "]]"; public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; int i = out.indexOf(findMarker1), j = out.indexOf(findMarker2, i+1); if (j < 0) return; String s = out.substring(i+2, j); regexp = s.replaceAll("\\d+", Matcher.quoteReplacement("\\d+")); if (debug) debug("regexp: " + regexp); } public String processIn(Object _in) { String in = (String) _in; if (regexp.length() == 0) return in; else return in.replaceAll("(" + regexp + ")", findMarker1 + "$1" + findMarker2); } public void toJava(Code code) { code.line(code.var + " = ((String) " + code.var + ").replaceAll(" + quote("(" + regexp + ")") + ", \"[[$1]]\");"); } } static class LFixed extends LearnerImpl { static boolean debug = false; Object value; public void processInOut(Object in, Object out) { value = out; if (debug) printVars(); } public Object processIn(Object in) { return value; } public void toJava(Code code) { code.line(code.var + " = " + quote((String) value) + ";"); } } static void assertSameSize(List a, List b) { if (a.size() != b.size()) throw fail("wrong list sizes"); } // process lists in parallel // (in and out must be a list of same length) static class LEach extends LearnerImpl { static boolean debug = false; Learner base; LEach(Learner base) { this.base = base; } public void processInOut(Object _in, Object _out) { List in = (List) _in, out = (List) _out; assertSameSize(in, out); for (int i = 0; i < in.size(); i++) base.processInOut(in.get(i), out.get(i)); if (debug) printVars(); } public Object processIn(Object _in) { List in = (List) _in; List out = new ArrayList(); for (Object x : in) out.add(base.processIn(x)); return out; } public void toJava(Code code) { code.line("List out = new ArrayList();"); code.line("for (Object x : (List) in) {"); code.indent(); code.line("in = x;"); base.toJava(code); code.line("out.add(in);"); code.unindent(); code.line("}"); code.line("in = out;"); } } static class LChange extends LearnerImpl { Function f; Learner base; LChange(Function f, Learner base) { this.f = f; this.base = base; } public void processInOut(Object in, Object out) { in = f.process(in); base.processInOut(in, out); } public Object processIn(Object in) { in = f.process(in); return base.processIn(in); } public void toJava(Code code) { f.toJava_process(code); base.toJava(code); } } static class LFindOutInIn extends LearnerImpl { static boolean debug = false; Learner base; String findMarker1 = "{{", findMarker2 = "}}"; LFindOutInIn(Learner base) { this.base = base;} public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; if (out.length() == 0) out = in; else { int i = in.indexOf(out); if (i < 0) throw new RuntimeException("error"); int j = i+out.length(); out = in.substring(0, i) + findMarker1 + in.substring(i, j) + findMarker2 + in.substring(j); if (debug) System.out.println("LFindOutInIn: index=" + i); } base.processInOut(in, out); if (debug) System.out.println("LFindOutInIn: Base learned. " + base); } public Object processIn(Object _in) { if (debug) System.out.println("LFindOutInIn: processIn"); String in = (String) _in; String out = (String) base.processIn(in); int i = out.indexOf(findMarker1), j = out.indexOf(findMarker2, i)-2; String result = i < 0 ? "" : in.substring(i, j); if (debug) System.out.println("LFindOutInIn: processIn " + i + " " + j + " " + quote(result)); return result; } public void tryAgain() { base.tryAgain(); } } static class RFJavaTok implements ReversibleFunction { public Object process(Object in) { return javaTok((String) in); } public Object unprocess(Object in) { return join((List) in); } public void toJava_process(Code code) { code.translators("!636", "!class JavaTok"); code.line(code.var + " = javaTok((String) " + code.var + ");"); } public void toJava_unprocess(Code code) { code.translators("!636", "!class JavaTok"); code.line(code.var + " = join((List) " + code.var + ");"); } } static class RFToLines implements ReversibleFunction { public Object process(Object in) { return toLines((String) in); } public Object unprocess(Object in) { return fromLines((List) in); } public void toJava_process(Code code) { code.translators("!636", "!standard functions"); code.assign("toLines(" + code.s() + ");"); } public void toJava_unprocess(Code code) { code.translators("!636", "!standard functions"); code.assign("fromLines(" + code.list() + ");"); } } // works on a token list - makes a list of only the string constants (unquoted) static class FStringsOnly implements Function { static boolean debug = false; public Object process(Object _in) { List tok = new ArrayList(); for (String s : (List) _in) { boolean isString = s.startsWith("\"") || s.startsWith("[["); if (isString) tok.add(unquote(s)); if (debug) System.out.println("FStringsOnly - isString: " + isString + " - " + s); } return tok; } public void toJava_process(Code code) { code.translators("!standard functions"); code.line("List tok = new ArrayList();"); code.line("for (String s : (List) " + code.var + ")"); code.line(" if (s.startsWith(\"\\\"\") || s.startsWith(\"[[\")) tok.add(unquote(s));"); code.assign("tok"); } } // works on a token list - makes a list of only the number constants static class FNumbersOnly implements Function { static boolean debug = false; public Object process(Object _in) { List tok = new ArrayList(); for (String s : (List) _in) { boolean isNumber = s.length() != 0 && (Character.isDigit(s.charAt(0)) || (s.charAt(0) == '-' && s.length() > 1)); if (isNumber) tok.add(s); if (debug) System.out.println("FNumbersOnly - isNumber: " + isNumber + " - " + s); } return tok; } public void toJava_process(Code code) { code.line("List tok = new ArrayList();"); code.line("for (String s : (List) " + code.var + ")"); code.line(" if (s.length() != 0 && (Character.isDigit(s.charAt(0)) || (s.charAt(0) == '-' && s.length() > 1))) tok.add(s);"); code.assign("tok"); } } static class FLength implements Function { static boolean debug = false; public Object process(Object in) { Object result = String.valueOf(in instanceof List ? ((List) in).size() : ((String) in).length()); if (debug) System.out.println("FLength: " + result); return result; } public void toJava_process(Code code) { code.assign("String.valueOf(" + code.var + " instanceof List ? " + code.list() + ".size() : " + code.s() + ".length())"); } } static class LFixedFunction extends LearnerImpl { Function f; LFixedFunction(Function f) { this.f = f; } public void processInOut(Object in, Object out) { } public Object processIn(Object in) { return f.process(in); } public void toJava(Code code) { f.toJava_process(code); } } // trivial stuff like taking the one element out of a 1-element list static class LTrivial extends LearnerImpl { public void processInOut(Object in, Object out) { } public Object processIn(Object in) { return ((List) in).get(0); } public void toJava(Code code) { code.assign(code.list() + ".get(0)"); } } // get element of a list at fixed index static class LGetListElement extends LearnerImpl { static boolean debug = false; int i; public void processInOut(Object _in, Object out) { List in = (List) _in; i = in.indexOf(out); if (debug) System.out.println("LGetListElement: " + i + " " + out); } public Object processIn(Object in) { return ((List) in).get(i); } public void toJava(Code code) { code.assign(code.list() + ".get(" + i + ")"); } } // math operations. // input: L (list of numbers) // output: S (number) static class LMath extends LearnerImpl { static boolean debug = false; String allOperations = "+ - * /"; TreeSet possibleOperations = new TreeSet(); LMath() { possibleOperations.addAll(Arrays.asList(allOperations.split(" +"))); } public void processInOut(Object _in, Object _out) { List in = (List) _in; String out = (String) _out; BigInteger[] inNumbers = makeBigIntArray(in); BigInteger[] outNumbers = new BigInteger[] {bigint(out)}; findOperation(inNumbers, outNumbers); if (debug) System.out.println("Operations: " + possibleOperations); } public void findOperation(BigInteger[] in, BigInteger[] out) { filterOperations(in, out); if (possibleOperations.isEmpty()) throw fail("tilt"); } public void filterOperations(BigInteger[] in, BigInteger[] out) { for (Iterator i = possibleOperations.iterator(); i.hasNext(); ) { String op = i.next(); BigInteger[] out2 = doOperation(op, in); if (out2 == null || !arraysEqual(out, out2)) i.remove(); // keep only matching operations } } public BigInteger[] doOperation(String op, BigInteger[] in) { op = op.intern(); try { if (in.length == 2) { BigInteger a = in[0], b = in[1], x = null; if (op == "+") x = a.add(b); else if (op == "-") x = a.subtract(b); else if (op == "*") x = a.multiply(b); else if (op == "/") x = a.divide(b); return x != null ? new BigInteger[] {x} : null; } return null; } catch (Throwable e) { return null; } } public String processIn(Object _in) { List in = (List) _in; String op = possibleOperations.iterator().next(); if (debug) System.out.println("op: " + op); BigInteger[] inNumbers = makeBigIntArray(in); BigInteger[] outNumbers = doOperation(op, inNumbers); return outNumbers[0].toString(); } String BI = BigInteger.class.getName(); public void toJava(Code code) { String op = possibleOperations.iterator().next(); String a = "new " + BI + "((String) " + code.list() + ".get(0))"; String b = "new " + BI + "((String) " + code.list() + ".get(1))"; if (op.equals("+")) code.assign(a + ".add(" + b + ").toString()"); else throw todo(); } } // can multiply a number with a fixed other number // input: S (number) // output: S (number) static class LMath2 extends LearnerImpl { static boolean debug = false; BigInteger factor; public void processInOut(Object _in, Object _out) { if (debug) print("LMath2: in = " + struct(_in)); String in = (String) _in; String out = (String) _out; BigInteger[] inNumbers = new BigInteger[] {bigint(in)}; BigInteger[] outNumbers = new BigInteger[] {bigint(out)}; findOperation(inNumbers, outNumbers); } public void findOperation(BigInteger[] in, BigInteger[] out) { if (l(in) != 1 || l(out) != 1) tilt(); BigInteger f = out[0].divide(in[0]); if (!eq(out[0], in[0].multiply(f))) tilt(); if (factor != null && neq(factor, f)) tilt(); factor = f; } public String processIn(Object _in) { String in = (String) _in; BigInteger[] inNumbers = new BigInteger[] {bigint(in)}; return str(inNumbers[0].multiply(factor)); } } static class EscapeCase implements Function { static boolean debug = false; public Object process(Object _in) { if (debug) System.out.println("EscapeCase: " + _in); String in = (String) _in; return in.equals("case") ? "_case" : in; } public void toJava_process(Code code) { code.line("if (\"case\".equals(" + code.var + ")) " + code.var + " = " + quote("_case") + ";"); } } static class LCharShift extends LearnerImpl { int shift; public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; shift = (int) out.charAt(0) - (int) in.charAt(0); } public Object processIn(Object _in) { String in = (String) _in; char[] c = new char[in.length()]; for (int i = 0; i < c.length; i++) c[i] = (char) ((int) in.charAt(i) + shift); return new String(c); } public void toJava(Code code) { code.line("char[] c = new char[((String) " + code.var + ").length()];"); code.line("for (int i = 0; i < c.length; i++)"); code.line(" c[i] = (char) ((int) ((String) " + code.var + ").charAt(i)" + (shift < 0 ? "" + shift : "+" + shift) + ");"); code.line(code.var + " = new String(c);"); } } // applies base learner to first char of string // (or first element of list, TODO) static class LFirst extends LearnerImpl { Learner baseLearner; LFirst(Learner baseLearner) { this.baseLearner = baseLearner; } public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; if (in.length() == 0) return; String firstIn = in.substring(0, 1), firstOut = out.substring(0, 1); baseLearner.processInOut(firstIn, firstOut); } public Object processIn(Object _in) { String in = (String) _in; if (in.length() == 0) return in; String firstIn = in.substring(0, 1); return baseLearner.processIn(firstIn) + in.substring(1); } public void toJava(Code code) { code.line("if (" + code.s() + ".length() != 0) {"); code.indent(); code.line("String rest = " + code.s() + ".substring(1);"); code.line(code.var + " = " + code.s() + ".substring(0, 1);"); baseLearner.toJava(code); code.line(code.var + " = " + code.s() + "+rest;"); code.unindent(); code.line("}"); } } static Method findMainMethod(Class theClass) { for (Method method : theClass.getMethods()) if (method.getName().equals("main") && method.getParameterTypes().length == 1) return method; throw new RuntimeException("Method 'main' with 1 parameter not found in " + theClass.getName()); } static Method compileJavaInToOut(Code code) { try { String java = code.buf.toString(); String prelude = /*"import java.util.*;\n\n" +*/ "public class main { public static Object main(Object in) throws Exception {\n"; String postlude = "\nreturn in;\n}}"; String src = code.getTranslators() + "\n" + prelude + java + postlude; Class mainClass = compileAndLoadMainClass(src); return findMainMethod(mainClass); } catch (Exception e) { throw new RuntimeException(e); } } static Method findCalcMethod(Class theClass) { for (Method method : theClass.getMethods()) if (method.getName().equals("calc")) return method; throw new RuntimeException("Method 'calc' not found in " + theClass.getName()); } // for simplejava stuff (execute tasks) static String execute(String src) { try { Class mainClass = compileAndLoadMainClass(src); Method m = findCalcMethod(mainClass); return String.valueOf(m.invoke(null)); } catch (Exception e) { throw new RuntimeException(e); } } static void testJava(Case _case, Code code) { try { Method m = compileJavaInToOut(code); for (Object[] e : _case.fullExamples) { String out = (String) m.invoke(null, e[0]); if (!e[1].equals(out)) { throw new RuntimeException("[fail] Java code on " + structure(e[0]) + " - got: " + structure(out) + " rather than: " + structure(e[1])); } } System.out.println("\nGOOD JAVA."); _case.goodJava = true; } catch (Throwable e) { if (showFails) e.printStackTrace(); System.out.println("\nBAD JAVA."); } } static class LSwitch extends LearnerImpl { Case _case; Learner switcher; LSwitch(Case _case, Learner switcher) { this._case = _case; this.switcher = switcher; } public void processInOut(Object in, Object out) { } public Object processIn(Object in) { int i = Integer.parseInt((String) switcher.processIn(in)); return _case.combined.get(i-1).winner.processIn(in); } public void toJava(Code code) { throw todo(); } } static class LMulti extends LearnerImpl { static boolean debug = false; List candidates = new ArrayList(); LMulti(Learner... learners) { for (Learner l : learners) candidates.add(l); } LMulti(List learners) { for (Learner l : learners) candidates.add(l); } LMulti(Learner l, List ll) { candidates.add(l); candidates.addAll(ll); } public void processInOut(Object in, Object out) { if (debug) System.err.println("LMulti candidates: " + candidates.size()); for (ListIterator i = candidates.listIterator(); i.hasNext(); ) { Learner l = i.next(); try { l.processInOut(in, out); } catch (Throwable e) { if (debug) { e.printStackTrace(); System.err.println("Removing candidate: " + structure(l)); } silentException(e); i.remove(); } } if (debug) System.err.println("LMulti candidates now: " + candidates.size()); if (candidates.isEmpty()) throw fail("no candidates left"); } public Object processIn(Object in) { while (true) { // fails or returns eventually Learner l = candidates.get(0); if (debug) System.err.println("Using candidate: " + structure(l) + ", " + candidates.size() + " left"); try { return l.processIn(in); } catch (Throwable e) { if (debug) { e.printStackTrace(); System.err.println("Removing candidate: " + structure(l)); } silentException(e); candidates.remove(0); } } } public void tryAgain() { candidates.remove(0); } public void toJava(Code code) { candidates.get(0).toJava(code); } } static class FIsString implements Function { static boolean debug = false; public Object process(Object _in) { String in = (String) _in; return in.startsWith("\"") || in.startsWith("[[") ? "1" : "0"; } public void toJava_process(Code code) { code.assign(code.s() + ".startsWith(\"\\\"\") || " + code.s() + ".startsWith(\"[[\") ? \"1\" : \"0\""); } } static class FCommonPrefix implements Function { static boolean debug = false; public Object process(Object _in) { String in = (String) _in, prefix = null; for (String line : toLines(in)) if (line.length() != 0) { prefix = prefix == null ? line : commonPrefix(prefix, line); if (debug) System.out.println("FCommonPrefix: line=" + quote(line) + ", prefix=" + quote(prefix)); } return prefix; } public void toJava_process(Code code) { throw todo(); } } static class FCommonSuffix implements Function { static boolean debug = false; public Object process(Object _in) { String in = (String) _in, suffix = null; for (String line : toLines(in)) if (line.length() != 0) { suffix = suffix == null ? line : commonSuffix(suffix, line); if (debug) System.out.println("FCommonSuffix: line=" + quote(line) + ", suffix=" + quote(suffix)); } return suffix; } public void toJava_process(Code code) { throw todo(); } } /*static class Pair { Object a, b; public boolean equals(Object o) { return o instanceof Pair && a.equals(((Pair) o).a) && b.equals((Pair) o).b); } public int hashCode() { return a.hashCode()+b.hashCode()*2; } }*/ static class Matrix { HashMap dataByCol = new HashMap(); void put(Object col, Object row, Object value) { //data.put(new Pair(col, row), value); getCol(col).put(row, value); } Map getCol(Object col) { Map map = dataByCol.get(col); if (map == null) dataByCol.put(col, map = new HashMap()); return map; } Object get(Object col, Object row) { //return data.get(new Pair(col, row)); return getCol(col).get(row); } Set cols() { return dataByCol.keySet(); } Set rowsFor(Object col) { return getCol(col).keySet(); } } static class LDistinguishList extends LearnerImpl { static boolean debug = false; Function helper; int idx; Matrix matrix = new Matrix(); LDistinguishList(Function helper) { this.helper = helper; } public void processInOut(Object _in, Object out) { List in = (List) _in; for (int i = 0; i < in.size(); i++) { Object y = helper.process(in.get(i)); matrix.put(i, y, out); } } public Object processIn(Object _in) { // find idx for (Object i : matrix.cols()) { Collection ys = matrix.rowsFor(i); if (ys.size() > 1) { idx = (Integer) i; break; } } List in = (List) _in; Object y = helper.process(in.get(idx)); return matrix.get(idx, y); } public void toJava(Code code) { List ys = new ArrayList(matrix.rowsFor(idx)); //String v = code.var; //code.newVar(); //String vy = code.var; //code.oldVar(); code.assign(code.list() + ".get(" + idx + ")"); helper.toJava_process(code); for (int i = 0; i < ys.size(); i++) { Object y = ys.get(i); if (i < ys.size()) code.line((i == 0 ? "" : "else ") + "if (" + quote((String) y) + ".equals(" + code.var + "))"); else code.line("else"); code.line(" " + code.var + " = " + quote((String) matrix.get(idx, y)) + ";"); } } } static class RunnersUp { String expected; int bestScore = -1; String bestResult; Learner winner; void add(String expected, String result, int score, Learner learner) { if (!expected.equals(this.expected) || bestScore == -1 || score < bestScore) { bestScore = score; bestResult = result; winner = learner; } this.expected = expected; } } static List casesSortedByID() { TreeMap map = new TreeMap(); List rest = new ArrayList(); for (Case c : cases) if (isSnippetID(c.id)) map.put(parseSnippetID(c.id), c); else rest.add(c); List list = new ArrayList(map.values()); list.addAll(rest); return list; } // These end up inside the main class static class LBox extends LearnerImpl { static boolean debug = false; Learner middleLearner; char topChar, bottomChar; LBox(Learner middleLearner) { this.middleLearner = middleLearner; } // An optimization, but a bit of a dangerous one: /*public void forwardSecretExample(Object _in, Object _out) { String in = (String) _in, out = (String) _out; L l = toLines(out); String middle = l.get(2); middleLearner.processInOut(in, middle); }*/ public void tryAgain() { middleLearner.tryAgain(); } public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; List l = toLines(out); String middle = l.get(2); middleLearner.processInOut(in, middle); topChar = l.get(1).charAt(0); bottomChar = l.get(3).charAt(0); } public Object processIn(Object in) { String middle = (String) middleLearner.processIn(in); return "\n" + main.repeat(topChar, middle.length()) + "\n" + middle + "\n" + main.repeat(bottomChar, middle.length()) + "\n"; } public void toJava(Code code) { throw todo(); } } static class LBox2 extends LearnerImpl { static boolean debug = false; Learner middleLearner; char topChar, bottomChar, tlChar, trChar, blChar, brChar; LBox2(Learner middleLearner) { this.middleLearner = middleLearner; } public void tryAgain() { middleLearner.tryAgain(); } public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; List l = toLines(out); String middle = l.get(2); if (debug) System.out.println("Forwarding to middle learner: " + quote(in) + " => " + quote(middle)); middleLearner.processInOut(in, middle); String top = l.get(1), bottom = l.get(3); tlChar = top.charAt(0); topChar = top.charAt(1); trChar = top.charAt(top.length()-1); blChar = bottom.charAt(0); bottomChar = bottom.charAt(1); brChar = bottom.charAt(bottom.length()-1); } public Object processIn(Object in) { String middle = (String) middleLearner.processIn(in); return "\n" + tlChar + main.repeat(topChar, middle.length()-2) + trChar + "\n" + middle + "\n" + blChar + main.repeat(bottomChar, middle.length()-2) + brChar + "\n"; } public void toJava(Code code) { throw todo(); } } // These end up inside the main class static class LEmptyBox extends LearnerImpl { static boolean debug = false; Learner inputLearner; char c; LEmptyBox(Learner inputLearner) { this.inputLearner = inputLearner;} public void tryAgain() { inputLearner.tryAgain(); } public void processInOut(Object in, Object _out) { String out = (String) _out; List l = toLines(out); int w = l.get(1).length(), h = l.size()-1; String input = w + "*" + h; if (debug) System.out.println("LEmptyBox: Feeding to input learner: " + in + " => " + input); inputLearner.processInOut(in, input); if (debug) System.out.println("Input learner: " + structure(inputLearner)); c = l.get(1).charAt(0); if (debug) System.out.println("LEmptyBox: c=" + c); } public Object processIn(Object in) { String input = (String) inputLearner.processIn(in); if (debug) System.out.println("LEmptyBox: in=" + in + ", input=" + input); String[] split = input.split("\\*"); int w = Integer.parseInt(split[0]), h = Integer.parseInt(split[1]); StringBuilder buf = new StringBuilder(); buf.append("\n"); buf.append(main.repeat(c, w) + "\n"); for (int y = 1; y < h-1; y++) buf.append(c + main.repeat(' ', w-2) + c + "\n"); buf.append(main.repeat(c, w) + "\n"); return buf.toString(); } public void toJava(Code code) { throw todo(); } } static class LFixedSubstring extends LearnerImpl { static boolean debug = false; String findMarker1 = "{{", findMarker2 = "}}"; int i, j; public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; i = out.indexOf(findMarker1); j = out.indexOf(findMarker2, i)-2; if (debug && j >= 0) { boolean correct = in.substring(i, j).equals(out.substring(i+2, j+2)); System.out.println("LFixedSubstring: i, j = " + i + ", " + j + " " + correct + " " + quote(in.substring(i, j))); } } public String processIn(Object _in) { String in = (String) _in; return in.substring(0, i) + findMarker1 + in.substring(i, j) + findMarker2 + in.substring(j); } } // These end up inside the main class static class LFixWhitespace extends LearnerImpl { static boolean debug = false; Learner base; String s; LFixWhitespace(Learner base) { this.base = base;} public void tryAgain() { base.tryAgain(); } public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; int i = count(in), j = count(out); in = in.substring(i); s = out.substring(0, j); out = out.substring(j); if (debug) System.out.println("LFixWhitespace: Feeding to input learner: " + in + " => " + out); base.processInOut(in, out); } public Object processIn(Object _in) { String in = (String) _in; int i = count(in); in = in.substring(i); in = (String) base.processIn(in); in = s + in; return in; } int count(String s) { int I = 0; while (I < s.length () && "\r\n\t ".indexOf(s.charAt(I)) >= 0) ++I; return I; } } static Object newInstance(Class c, Object... args) { try { Constructor m = findConstructor(c, args); m.setAccessible(true); return m.newInstance(args); } catch (Exception __e) { throw rethrow(__e); } } static boolean hasConstructor(boolean debug, Class c, Class... args) { for (Constructor m : c.getDeclaredConstructors()) { if (!checkArgs(m.getParameterTypes(), args, debug)) continue; return true; } return false; } static boolean hasConstructor(boolean debug, Class c, Object... args) { for (Constructor m : c.getDeclaredConstructors()) { if (!checkArgs(m.getParameterTypes(), args, debug)) continue; return true; } return false; } static Constructor findConstructor(Class c, Object... args) { for (Constructor m : c.getDeclaredConstructors()) { if (!checkArgs(m.getParameterTypes(), args, false)) continue; return m; } throw new RuntimeException("Constructor with " + args.length + " matching parameter(s) not found in " + c.getName()); } static Class findClass(String name) { for (String c : myInnerClasses()) if (c.equalsIgnoreCase(name)) try { return Class.forName("main$" + c); } catch (ClassNotFoundException e) { return null; } return null; } static boolean isSubclass(Class a, Class b) { return b.isAssignableFrom(a); } static boolean checkArgs(Class[] types, Object[] args, boolean debug) { if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || types[i].isInstance(args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } static boolean checkArgs(Class[] types, Class[] args, boolean debug) { if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isSubclass(args[i], types[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } // finds a fixed element, e.g. a Java token static class LCertainElement extends LearnerImpl { static boolean debug = false; ReversibleFunction f; // e.g. JavaTok String element; LCertainElement(ReversibleFunction f) { this.f = f;} public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; Set set = new HashSet(getMarked(out)); if (debug) System.out.println("LCertainElement: set=" + set); if (set.size() == 1) element = set.iterator().next(); if (debug) System.out.println("LCertainElement: element=" + element); } public Object processIn(Object _in) { List l = (List) f.process(_in); List l2 = new ArrayList(); for (String x : l) l2.add(x.equals(element) ? "[[" + x + "]]" : x); return f.unprocess(l2); } // returns positions in marked string (from/to/from/to...) static List getFindMarkers2(String s) { List l = new ArrayList(); int i = 0; while (i < s.length()) { int j = s.indexOf("[[", i); if (j < 0) break; int k = s.indexOf("]]", j+2); if (k < 0) break; l.add(j); l.add(k+2); i = k+2; } return l; } static List getMarked(String s) { List l = getFindMarkers2(s); List result = new ArrayList(); for (int i = 0; i < l.size(); i += 2) result.add(s.substring(l.get(i)+2, l.get(i+1)-2)); return result; } } static Class findClassFlex(String p) { Class c = findClass(p); if (c == null) c = findClass("L" + p); if (c == null) c = findClass("F" + p); if (c == null) c = findClass("RF" + p); return c; } static class LOneWordChanged extends LearnerImpl { static boolean debug = false; Function preprocessor; // e.g. JavaTok List lastIn; // only while learning String lastOut; // only while learning int index; String outPattern; LOneWordChanged(Function preprocessor) { this.preprocessor = preprocessor;} public void processInOut(Object _in, Object _out) { if (debug) System.out.println("LOneWordChanged: i/o " + _in + " " + _out); String in = (String) _in, out = (String) _out; List l = (List) preprocessor.process(_in); lastIn = l; lastOut = out; } public Object processIn(Object _in) { if (debug) System.out.println("LOneWordChanged: i " + _in); List l = (List) preprocessor.process(_in); if (outPattern == null) { if (l.size() != lastIn.size()) throw fail(); for (int i = 0; i < l.size(); i++) if (!l.get(i).equals(lastIn.get(i))) { index = i; String word = lastIn.get(index); outPattern = lastOut.replace(word, "{*}"); // delete learning data lastIn = null; lastOut = null; break; } if (outPattern == null) { if (debug) System.out.println("LOneWordChanged: last=" + lastIn + ", this=" + l); throw fail("not applicable - all words identical"); } } if (debug) System.out.println("LOneWordChanged: index=" + index + ", outPattern=" + quote(outPattern)); String word = l.get(index); return outPattern.replace("{*}", word); } static List getFindMarkers2(String s) { List l = new ArrayList(); int i = 0; while (i < s.length()) { int j = s.indexOf("[[", i); if (j < 0) break; int k = s.indexOf("]]", j+2); if (k < 0) break; l.add(j); l.add(k+2); i = k+2; } return l; } static List getMarked(String s) { List l = getFindMarkers2(s); List result = new ArrayList(); for (int i = 0; i < l.size(); i += 2) result.add(s.substring(l.get(i)+2, l.get(i+1)-2)); return result; } public void toJava(Code code) { preprocessor.toJava_process(code); String word = code.list() + ".get(" + index + ")"; if (outPattern.equals("{*}")) code.assign(word); else code.assign(javaQuote(outPattern) + ".replace(" + javaQuote("{*}") + ", (String) (" + word + "))"); } } static class LBla extends LearnerImpl { static boolean debug = false; String inPattern, outPattern; public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; int i = in.indexOf("bla"), j = out.indexOf("bla"); if (i < 0 || j < 0) throw fail(); inPattern = "^" + patternQuote(in.substring(0, i)) + "(.+)" + patternQuote(in.substring(i+3)) + "$"; outPattern = Matcher.quoteReplacement(out.substring(0, j)) + "$1" + Matcher.quoteReplacement(out.substring(j+3)); } public Object processIn(Object _in) { String in = (String) _in; return in.replaceAll(inPattern, outPattern); } } static String shorten(String s, int max) { return s.length() <= max ? s : s.substring(0, Math.min(s.length(), max)) + "..."; } // for stuff like #1000478 static class LIntersperse extends LearnerImpl { static boolean debug = false; ReversibleFunction preprocessor; // e.g. JavaTok List outPrefix, outSuffix, filler; LIntersperse(ReversibleFunction preprocessor) { this.preprocessor = preprocessor;} public void processInOut(Object _in, Object _out) { if (debug) debug("i/o " + _in + " " + _out); List in = (List) (preprocessor.process(_in)); List out = (List) (preprocessor.process(_out)); int i = out.indexOf(in.get(1)); outPrefix = out.subList(0, i); int j = out.lastIndexOf(in.get(in.size()-2)); outSuffix = out.subList(j+1, out.size()); int newLength = j-i+1; int realOldLength = (in.size()-1)/2; /*int intersperse = 0; if (realOldLength > 1) intersperse = newLength-realOldLength*3-1;*/ int intersperse = 3; // just assume this for now if (realOldLength > 1) filler = out.subList(i+1, i+1+intersperse); if (debug) debug("oldLength= " + in.size() + ", newLength=" + newLength +", realOldLength=" + realOldLength + ", intersperse=" + intersperse + ", filler length=" + (filler == null ? 0 : filler.size())); } public Object processIn(Object _in) { List l = (List) (preprocessor.process(_in)); if (debug) debug("i " + structure(l)); List list = new ArrayList(); list.addAll(outPrefix); if (debug) debug("ranging from " + 1 + " to " + (l.size()-2)); for (int i = 1; i < l.size()-1; i += 2) { list.add(l.get(i)); if (i < l.size()-2) list.addAll(filler); } list.addAll(outSuffix); Object out = preprocessor.unprocess(list); if (debug) debug("output: " + structure(list) + " = " + structure(out)); return out; } } static String getName(Class c) { return c.getName().replaceAll("^main\\$", ""); } static void keepAlive() { startDaemonThread(new Runnable() { public void run() { try { Object printed = null; while (true) { Object t = trying; Thread.sleep(1000); if (t != null && (verboseMonitor || (t == trying && printed != t))) { // still trying System.err.println("* "+ shorten(structure(t), 200)); printed = t; } else printed = null; } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "Object printed = null;\r\n while (true) {\r\n Object t = trying;\r\n ..."; }}); } // for stuff like #1000349 static class LFixedPositions extends LearnerImpl { static boolean debug = false; Map map = new TreeMap(); public void processInOut(Object _in, Object _out) { // Only looks at out... funny, eh? String out = (String) _out; for (int i = 0; i < out.length(); i++) { String s = map.get(i); String c = out.substring(i, i+1); if (s == null) map.put(i, c); else if (s.equals("")) { } else if (!s.equals(c)) map.put(i, ""); // TILT } for (int i : map.keySet()) if (i >= out.length()) map.put(i, ""); } public Object processIn(Object _in) { String in = (String) _in; List l = new ArrayList(); for (int i = 0; i < in.length(); i++) l.add(in.substring(i, i+1)); for (int i : map.keySet()) { String s = map.get(i); if (!s.equals("")) { while (i >= l.size()) l.add(" "); l.set(i, s); } } return join("", l); } } // for stuff like #1000351 static class LMap extends LearnerImpl { static boolean debug = false; Map map = new TreeMap(); public void processInOut(Object _in, Object _out) { List in = (List) _in; List out = (List) _out; if (in.size() != out.size()) throw fail(); for (int i = 0; i < in.size(); i++) { String a = in.get(i), b = out.get(i); if (!a.equals(b)) map.put(a, b); } } public Object processIn(Object _in) { List in = (List) _in; List out = new ArrayList(); for (int i = 0; i < in.size(); i++) { String a = in.get(i), b = map.get(a); out.add(b != null ? b : a); } return out; } } // learns to exchange common prefixes and suffixes. // maybe it's not perfect... see #1000491 for a problem case static class LPrefixSuffix extends LearnerImpl { static boolean debug = false; String prefixIn, suffixIn, prefixOut, suffixOut; public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; updateIn(in); prefixOut = prefixOut == null ? out : commonPrefix(prefixOut, out); suffixOut = suffixOut == null ? out : commonSuffix(suffixOut, out); if (debug) printState("processInOut(" + quote(in) + ", " + quote(out) + ")"); } void updateIn(String in) { prefixIn = prefixIn == null ? in : commonPrefix(prefixIn, in); suffixIn = suffixIn == null ? in : commonSuffix(suffixIn, in); if (debug) printState("updateIn(" + quote(in) + ")"); } public String processIn(Object _in) { String in = (String) _in; //System.out.println("[before last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut)); //System.out.println("[last info] " + quote(in)); // use latest information String p = prefixIn, s = suffixIn; updateIn(in); prefixOut = prefixOut.substring(0, prefixOut.length()-(p.length()-prefixIn.length())); suffixOut = suffixOut.substring(s.length()-suffixIn.length()); //System.out.println("[after last info] " + quote(prefixIn) + " " + quote(suffixIn) + " " + quote(prefixOut) + " " + quote(suffixOut)); String core = in.substring(prefixIn.length(), in.length()-suffixIn.length()); return prefixOut + core + suffixOut; } public void toJava(Code code) { if (prefixIn.length() != 0 || suffixIn.length() != 0) code.line(code.var + " = ((String) " + code.var + ").substring(" + prefixIn.length() + ", " + code.s() + ".length()-" + suffixIn.length() + ");"); if (prefixOut.length() != 0 || suffixOut.length() != 0) code.line(code.var + " = " + quote(prefixOut) + " + " + code.var + " + " + quote(suffixOut) + ";"); } void printState(String text) { System.out.println(text); printVars(); } } // learns to remove input suffixes. static class LRemoveInputSuffix extends LearnerImpl { static boolean debug = false; String suffix; static boolean failFast = false; public void processInOut(Object _in, Object _out) { String in = (String) _in, out = (String) _out; if (!in.startsWith(out)) throw fail(); suffix = overwrite(suffix, in.substring(out.length())); } public String processIn(Object _in) { String in = (String) _in; if (!in.endsWith(suffix)) if (failFast) throw fail(); else return in; return in.substring(0, in.length()-suffix.length()); } } static T overwrite(T existing, T newValue) { if (existing != null && !existing.equals(newValue)) throw fail("Overwrite"); return newValue; } static void getPass() { try { if (user != null) { File pwFile = new File(userHome(), ".javax/pw-" + user); if (pass == null) { List lines = toLines(readTextFile(pwFile, "")); if (!lines.isEmpty()) pass = lines.get(0).trim(); if ("".equals(pass)) pass = null; //System.out.println("Pass: " + quote(pass)); if (pass != null) System.out.println("Password read from " + pwFile.getAbsolutePath()); } if (pass == null) System.out.println("You can put your password in: " + pwFile.getAbsolutePath()); } } catch (Exception __e) { throw rethrow(__e); } } static void editSnippetText(String docID, String newText, String editInfo) { try { getPass(); System.out.println("Editing " + docID); URL url = new URL(tb_mainServer() + "/tb-int/auto-edit.php"); String postData = "user=" + urlencode(user) + "&pass=" + urlencode(pass) + "&id=" + urlencode(docID) + "&text=" + urlencode(newText) + "&editinfo=" + urlencode(editInfo); System.out.println(postData); String result = doPost(postData, url.openConnection(), url); System.out.println("Edit result: " + result); } catch (Exception __e) { throw rethrow(__e); } } static class FBeforeColon extends FunctionImpl { public Object process(Object _in) { String in = (String) _in; return in.substring(0, in.indexOf(':')); } } static class FAfterColon extends FunctionImpl { public Object process(Object _in) { String in = (String) _in; return in.substring(in.indexOf(':')+1); } } static class FUnquote extends FunctionImpl { public Object process(Object _in) { String in = (String) _in; return unquote(in); } } static class LCombineTwoFunctions extends LearnerImpl { static boolean debug = false; Function f, g; public void processInOut(Object in, Object out) { if (f != null) return; List functions = allFunctions(); // simple version without really checking multiple candidates for (Function f : functions) { Object o = tryProcess(f, in); if (o != null) { if (debug) debug("in=" + structure(in) + ", f=" + structure(f) + ", o=" + structure(o)); for (Function g : functions) { Object x = tryProcess(g, o); if (out.equals(x)) { if (debug) debug("bingo! " + structure(g)); this.f = f; this.g = g; return; } } } } } public Object processIn(Object in) { return g.process(f.process(in)); } static Object tryProcess(Function f, Object in) { try { return f.process(in); } catch (Throwable e) { return null; } } } // extensive version (better results) with full search static class LCombineTwoFunctions2 extends LearnerImpl { static boolean debug = false; static long instances; Stepper stepper; List examples = new ArrayList(); LCombineTwoFunctions2() { ++instances; if (debug) debug("Instances: " + instances); } public void finalize() {--instances; } public void processInOut(Object in, Object out) { examples.add(new Object[] {in, out}); if (stepper == null) stepper = new Stepper(allFunctions()); while (!stepper.ended()) { long time = System.currentTimeMillis(); boolean b = check(stepper.current()); time = System.currentTimeMillis()-time; if (debug) debug("check time: " + time); if (b) return; // ok, keep else stepper.step(); } throw fail(); } boolean check(Function[] fg) { if (debug) { debug("Examples: " + examples.size() + ", stepper size: " + stepper.functions.size()); debug("Checking " + structure(fg)); } for (Object[] e : examples) { Object o = tryProcess(fg[0], e[0]); if (o != null) { Object x = tryProcess(fg[1], o); if (e[1].equals(x)) continue; // example ok } return false; } return true; // all examples ok } public Object processIn(Object in) { Function[] fg = stepper.current(); return fg[1].process(fg[0].process(in)); } static Object tryProcess(Function f, Object in) { try { return f.process(in); } catch (Throwable __e) { return null; } } static class Stepper { List functions; int i1, i2; Stepper(List functions) { this.functions = functions;} boolean ended() { return i1 >= functions.size(); } Function[] current() { return new Function[] {functions.get(i1), functions.get(i2)}; } void step() { if (i2 < functions.size()-1) ++i2; else { ++i1; i2 = 0; } } } } static List allFunctions() { List list = new ArrayList(); for (String s : myInnerClasses()) { Class c = findClass(s); if (c == null) continue; if (isSubclass(c, Function.class) && hasConstructor(false, c)) { //System.out.println("allFunctions: " + c); list.add((Function) newInstance(c)); } } return list; } static List solvedCases() { List list = new ArrayList(); for (int i = caseIdx-1; i >= 0; i--) if (cases.get(i).winner != null) list.add(cases.get(i)); return list; } static Object tryProcess(Function f, Object in) { try { return f.process(in); } catch (Throwable __e) { return null; } } static Object tryProcess(Learner l, Object in) { try { return l.processIn(in); } catch (Throwable __e) { return null; } } static class LFunctionPlusPrevious extends LearnerImpl { static boolean debug = false; Stepper stepper; List examples = new ArrayList(); public void processInOut(Object in, Object out) { examples.add(new Object[] {in, out}); if (stepper == null) stepper = new Stepper(allFunctions(), solvedCases()); while (!stepper.ended()) { if (check(stepper.current())) return; // ok, keep else stepper.step(); } throw fail(); } boolean check(Object[] fg) { if (debug) debug("Checking " + structure(fg)); for (Object[] e : examples) { Object o = tryProcess((Function) fg[0], e[0]); if (o != null) { Object x = tryProcess(((Case) fg[1]).winner, o); if (e[1].equals(x)) continue; // example ok } return false; } return true; // all examples ok } public Object processIn(Object in) { Object[] fg = stepper.current(); return ((Case) fg[1]).processIn(((Function) fg[0]).process(in)); } static class Stepper { List functions; List cases; int i1, i2; Stepper(List functions, List cases) { this.cases = cases; this.functions = functions;} boolean ended() { return i1 >= functions.size(); } Object[] current() { return new Object[] {functions.get(i1), cases.get(i2)}; } void step() { if (i2 < cases.size()-1) ++i2; else { ++i1; i2 = 0; } } } } static class FNonMarkedAsList extends FunctionImpl { public Object process(Object _in) { String in = (String) _in; List m = getFindMarkers2(in); List list = new ArrayList(); for (int i = 0; i < m.size(); i += 2) { int from = i == 0 ? 0 : m.get(i-1); int to = i == m.size()-1 ? in.length() : m.get(i); if (to > from) list.add(in.substring(from, to)); } return list; } static List getFindMarkers2(String s) { List l = new ArrayList(); int i = 0; while (i < s.length()) { int j = s.indexOf("[[", i); if (j < 0) break; int k = s.indexOf("]]", j+2); if (k < 0) break; l.add(j); l.add(k+2); i = k+2; } return l; } } static class LWrap extends LearnerImpl { ReversibleFunction f; Learner base; LWrap(ReversibleFunction f, Learner base) { this.base = base; this.f = f;} public void processInOut(Object in, Object out) { in = f.process(in); out = f.process(out); base.processInOut(in, out); } public Object processIn(Object in) { in = f.process(in); in = base.processIn(in); in = f.unprocess(in); return in; } public void toJava(Code code) { f.toJava_process(code); base.toJava(code); f.toJava_unprocess(code); } } // The idea is to postprocess to match out // e.g. out="a, b, c" // and f=FJoin(", ") static class LPost extends LearnerImpl { ReversibleFunction f; Learner base; LPost(ReversibleFunction f, Learner base) { this.base = base; this.f = f;} public void processInOut(Object in, Object out) { out = f.unprocess(out); base.processInOut(in, out); } public Object processIn(Object in) { in = base.processIn(in); in = f.process(in); return in; } public void toJava(Code code) { base.toJava(code); f.toJava_process(code); } } // TODO: why doesn't it extend ReversibleFunctionImpl? static class FJoin extends FunctionImpl { String glue; FJoin(String glue) { this.glue = glue;} public Object process(Object _in) { return join(glue, (List) _in); } public Object unprocess(Object _in) { String in = (String) _in; return new ArrayList(Arrays.asList(in.split(glue))); } } static class FStringAsChars extends ReversibleFunctionImpl { public Object process(Object _in) { String in = (String) _in; List list = new ArrayList(); for (int i = 0; i < in.length(); i++) list.add(in.substring(i, i+1)); return list; } public Object unprocess(Object _in) { List l = (List) _in; return join("", l); } } static class LUse extends LearnerImpl { Function f; LUse(Function f) { this.f = f;} public void processInOut(Object in, Object out) { } public Object processIn(Object in) { in = f.process(in); return in; } public void toJava(Code code) { f.toJava_process(code); } } static class FRegExp extends FunctionImpl { static boolean debug = false; String re; FRegExp(String re) { this.re = re;} public Object process(Object _in) { String in = (String) _in; Matcher matcher = Pattern.compile(re).matcher(in); boolean found = matcher.find(); if (debug) debug("found: " + found + ", regexp: " + re); if (found) return matcher.group(1); else return ""; } public void toJava_process(Code code) { code.line("Matcher m = Pattern.compile(" + javaQuote(re) + ").matcher(" + code.s() + ");"); code.assign("m.find() ? m.group(1) : \"\""); } } static class LHotwire extends LearnerImpl { static boolean cacheSnippets = true; static TreeMap cache = new TreeMap(); static boolean debug = false; String programID; Class prog; LHotwire(String programID) { this.programID = programID;} public void processInOut(Object in, Object out) { } public Object processIn(Object in) { if (prog == null) { prog = cache.get(parseSnippetID(programID)); if (prog == null) { if (debug) debug("Loading " + programID); prog = hotwire(programID); if (cacheSnippets) cache.put(parseSnippetID(programID), prog); } } set(prog, "in", in); call(prog, "main", new Object[] {new String[0]}); in = get(prog, "in"); if (debug) debug(programID + " returned: " + structure(in)); return in; } } public static String unquote(String s) { if (s.startsWith("[")) { int i = 1; while (i < s.length() && s.charAt(i) == '=') ++i; if (i < s.length() && s.charAt(i) == '[') { String m = s.substring(1, i); if (s.endsWith("]" + m + "]")) return s.substring(i+1, s.length()-i-1); } } if (s.startsWith("\"") && s.endsWith("\"") && s.length() > 1) { String st = s.substring(1, s.length()-1); StringBuilder sb = new StringBuilder(st.length()); for (int i = 0; i < st.length(); i++) { char ch = st.charAt(i); if (ch == '\\') { char nextChar = (i == st.length() - 1) ? '\\' : st .charAt(i + 1); // Octal escape? if (nextChar >= '0' && nextChar <= '7') { String code = "" + nextChar; i++; if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' && st.charAt(i + 1) <= '7') { code += st.charAt(i + 1); i++; if ((i < st.length() - 1) && st.charAt(i + 1) >= '0' && st.charAt(i + 1) <= '7') { code += st.charAt(i + 1); i++; } } sb.append((char) Integer.parseInt(code, 8)); continue; } switch (nextChar) { case '\\': ch = '\\'; break; case 'b': ch = '\b'; break; case 'f': ch = '\f'; break; case 'n': ch = '\n'; break; case 'r': ch = '\r'; break; case 't': ch = '\t'; break; case '\"': ch = '\"'; break; case '\'': ch = '\''; break; // Hex Unicode: u???? case 'u': if (i >= st.length() - 5) { ch = 'u'; break; } int code = Integer.parseInt( "" + st.charAt(i + 2) + st.charAt(i + 3) + st.charAt(i + 4) + st.charAt(i + 5), 16); sb.append(Character.toChars(code)); i += 5; continue; default: ch = nextChar; // added by Stefan } i++; } sb.append(ch); } return sb.toString(); } else return s; // return original } static Case quickSolve(String[] ioioi) { try { Case _case = produceCase(ioioi); calculate(_case); return _case; } catch (Exception __e) { throw rethrow(__e); } } static Case produceCase(String[] ioioi) { try { StringBuilder buf = new StringBuilder(); for (int i = 0; i < ioioi.length; i++) { buf.append((i % 2) == 0 ? "In: " : "Out: "); buf.append(quote(ioioi[i]) + "\n"); } return parse(buf.toString()); } catch (Exception __e) { throw rethrow(__e); } } static class FToUpper extends FunctionImpl { public Object process(Object _in) { String in = (String) _in; return upper(in); } } static Class getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; } } static Class getClass(Object o) { return o instanceof Class ? (Class) o : o.getClass(); } static Class getClass(Object realm, String name) { try { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (ClassNotFoundException e) { return null; } } catch (Exception __e) { throw rethrow(__e); } } static boolean nempty(Collection c) { return !empty(c); } static boolean nempty(CharSequence s) { return !empty(s); } static boolean nempty(Object[] o) { return !empty(o); } static boolean nempty(byte[] o) { return !empty(o); } static boolean nempty(int[] o) { return !empty(o); } static boolean nempty(Map m) { return !empty(m); } static boolean nempty(Iterator i) { return i != null && i.hasNext(); } static boolean nempty(Object o) { return !empty(o); } static RuntimeException todo() { throw new RuntimeException("TODO"); } static RuntimeException todo(Object msg) { throw new RuntimeException("TODO: " + msg); } // usually L static String fromLines(Iterable lines) { StringBuilder buf = new StringBuilder(); if (lines != null) for (Object line : lines) buf.append(str(line)).append('\n'); return buf.toString(); } static String fromLines(String... lines) { return fromLines(asList(lines)); } static Field setOpt_findField(Class c, String field) { HashMap map; synchronized(getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } return map.get(field); } static void setOpt(Object o, String field, Object value) { try { if (o == null) return; Class c = o.getClass(); HashMap map; if (getOpt_cache == null) map = getOpt_makeCache(c); // in class init else synchronized(getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } if (map == getOpt_special) { if (o instanceof Class) { setOpt((Class) o, field, value); return; } // It's probably a subclass of Map. Use raw method setOpt_raw(o, field, value); return; } Field f = map.get(field); if (f != null) { smartSet(f, o, value); return; } // possible improvement: skip setAccessible if (o instanceof DynamicObject) setDyn(((DynamicObject) o), field, value); } catch (Exception __e) { throw rethrow(__e); } } static void setOpt(Class c, String field, Object value) { if (c == null) return; try { Field f = setOpt_findStaticField(c, field); // TODO: optimize if (f != null) smartSet(f, null, value); } catch (Exception e) { throw new RuntimeException(e); } } static Field setOpt_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) { makeAccessible(f); return f; } _c = _c.getSuperclass(); } while (_c != null); return null; } static Class __javax; static Class getJavaX() { try { return __javax; } catch (Exception __e) { throw rethrow(__e); } } public static boolean isSnippetID(String s) { try { parseSnippetID(s); return true; } catch (RuntimeException e) { return false; } } static String loadTextFile(String fileName) { return loadTextFile(fileName, null); } static String loadTextFile(File f, String defaultContents) { try { checkFileNotTooBigToRead(f); if (f == null || !f.exists()) return defaultContents; FileInputStream fileInputStream = new FileInputStream(f); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); return loadTextFile(inputStreamReader); } catch (Exception __e) { throw rethrow(__e); } } public static String loadTextFile(File fileName) { return loadTextFile(fileName, null); } static String loadTextFile(String fileName, String defaultContents) { return fileName == null ? defaultContents : loadTextFile(newFile(fileName), defaultContents); } static String loadTextFile(Reader reader) throws IOException { StringBuilder builder = new StringBuilder(); try { char[] buffer = new char[1024]; int n; while (-1 != (n = reader.read(buffer))) builder.append(buffer, 0, n); } finally { reader.close(); } return str(builder); } static boolean preferCached = false; static boolean loadSnippet_debug = false; static ThreadLocal loadSnippet_silent = new ThreadLocal(); static ThreadLocal loadSnippet_publicOnly = new ThreadLocal(); static int loadSnippet_timeout = 30000; static String loadSnippet(String snippetID) { try { if (snippetID == null) return null; return loadSnippet(parseSnippetID(snippetID), preferCached); } catch (Exception __e) { throw rethrow(__e); } } static String loadSnippet(String snippetID, boolean preferCached) throws IOException { return loadSnippet(parseSnippetID(snippetID), preferCached); } public static String loadSnippet(long snippetID) { try { return loadSnippet(snippetID, preferCached); } catch (Exception __e) { throw rethrow(__e); } } public static String loadSnippet(long snippetID, boolean preferCached) throws IOException { String text; if (isLocalSnippetID(snippetID)) return loadLocalSnippet(snippetID); IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.loadSnippet(fsI(snippetID)); // boss bot disabled for now for shorter transpilations /*text = getSnippetFromBossBot(snippetID); if (text != null) return text;*/ initSnippetCache(); text = DiskSnippetCache_get(snippetID); if (preferCached && text != null) return text; try { if (loadSnippet_debug && text != null) System.err.println("md5: " + md5(text)); String url = tb_mainServer() + "/getraw.php?id=" + snippetID + "&utf8=1"; if (nempty(text)) url += "&md5=" + md5(text); if (!isTrue(loadSnippet_publicOnly.get())) url += standardCredentials(); String text2 = loadSnippet_loadFromServer(url); boolean same = eq(text2, "==*#*=="); if (loadSnippet_debug) print("loadSnippet: same=" + same); if (!same) text = text2; } catch (RuntimeException e) { e.printStackTrace(); throw new IOException("Snippet #" + snippetID + " not found or not public"); } try { initSnippetCache(); DiskSnippetCache_put(snippetID, text); } catch (IOException e) { System.err.println("Minor warning: Couldn't save snippet to cache (" + DiskSnippetCache_getDir() + ")"); } return text; } static File DiskSnippetCache_dir; public static void initDiskSnippetCache(File dir) { DiskSnippetCache_dir = dir; dir.mkdirs(); } public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException { return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null); } private static File DiskSnippetCache_getFile(long snippetID) { return new File(DiskSnippetCache_dir, "" + snippetID); } public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException { saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet); } public static File DiskSnippetCache_getDir() { return DiskSnippetCache_dir; } public static void initSnippetCache() { if (DiskSnippetCache_dir == null) initDiskSnippetCache(getGlobalCache()); } static String loadSnippet_loadFromServer(String url) { Integer oldTimeout = setThreadLocal(loadPage_forcedTimeout_byThread, loadSnippet_timeout); try { return isTrue(loadSnippet_silent.get()) ? loadPageSilently(url) : loadPage(url); } finally { loadPage_forcedTimeout_byThread.set(oldTimeout); } } static volatile StringBuffer local_log = new StringBuffer(); // not redirected static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot // in bytes - will cut to half that static volatile int print_log_max = 1024*1024; static volatile int local_log_max = 100*1024; static boolean print_silent = false; // total mute if set static Object print_byThread_lock = new Object(); static volatile ThreadLocal print_byThread; // special handling by thread - prefers F1 static volatile Object print_allThreads; static volatile Object print_preprocess; static void print() { print(""); } static A print(String s, A o) { print((endsWithLetterOrDigit(s) ? s + ": " : s) + o); return o; } // slightly overblown signature to return original object... static A print(A o) { ping_okInCleanUp(); if (print_silent) return o; String s = String.valueOf(o) + "\n"; print_noNewLine(s); return o; } static void print_noNewLine(String s) { Object f = getThreadLocal(print_byThread_dontCreate()); if (f == null) f = print_allThreads; if (f != null) // We do need the general callF machinery here as print_byThread is sometimes shared between modules if (isFalse( callF(f, s))) return; print_raw(s); } static void print_raw(String s) { if (print_preprocess != null) s = (String) callF(print_preprocess, s); s = fixNewLines(s); Appendable loc = local_log; Appendable buf = print_log; int loc_max = print_log_max; if (buf != loc && buf != null) { print_append(buf, s, print_log_max); loc_max = local_log_max; } if (loc != null) print_append(loc, s, loc_max); System.out.print(s); } static void print_autoRotate() { } static RuntimeException fail() { throw new RuntimeException("fail"); } static RuntimeException fail(Throwable e) { throw asRuntimeException(e); } static RuntimeException fail(Object msg) { throw new RuntimeException(String.valueOf(msg)); } static RuntimeException fail(String msg) { throw new RuntimeException(msg == null ? "" : msg); } static RuntimeException fail(String msg, Throwable innerException) { throw new RuntimeException(msg, innerException); } static boolean structure_showTiming, structure_checkTokenCount; static String structure(Object o) { return structure(o, new structure_Data()); } static String structure(Object o, structure_Data d) { StringWriter sw = new StringWriter(); d.out = new PrintWriter(sw); structure_go(o, d); String s = str(sw); if (structure_checkTokenCount) { print("token count=" + d.n); assertEquals("token count", l(javaTokC(s)), d.n); } return s; } static void structure_go(Object o, structure_Data d) { structure_1(o, d); while (nempty(d.stack)) popLast(d.stack).run(); } static void structureToPrintWriter(Object o, PrintWriter out) { structure_Data d = new structure_Data(); d.out = out; structure_go(o, d); } // leave to false, unless unstructure() breaks static boolean structure_allowShortening = false; // info on how to serialize objects of a certain class static class structure_ClassInfo { List fields; Method customSerializer; boolean special, nullInstances; } static class structure_Data { PrintWriter out; int stringSizeLimit; int shareStringsLongerThan = 20; boolean noStringSharing = false; IdentityHashMap seen = new IdentityHashMap(); //new BitSet refd; HashMap strings = new HashMap(); HashSet concepts = new HashSet(); HashMap infoByClass = new HashMap(); HashMap persistenceInfo = new HashMap(); int n; // token count List stack = new ArrayList(); // append single token structure_Data append(String token) { out.print(token); ++n; return this; } structure_Data append(int i) { out.print(i); ++n; return this; } // append multiple tokens structure_Data append(String token, int tokCount) { out.print(token); n += tokCount; return this; } // extend last token structure_Data app(String token) { out.print(token); return this; } structure_Data app(int i) { out.print(i); return this; } } static void structure_1(final Object o, final structure_Data d) { try { if (o == null) { d.append("null"); return; } Class c = o.getClass(); boolean concept = false; structure_ClassInfo info = d.infoByClass.get(c); if (info == null) { d.infoByClass.put(c, info = new structure_ClassInfo()); if ((info.customSerializer = findMethodNamed(c, "_serialize")) != null) info.special = true; } List lFields = info.fields; if (lFields == null) { // these are never back-referenced (for readability) if (o instanceof Number) { PrintWriter out = d.out; if (o instanceof Integer) { int i = ((Integer) o).intValue(); out.print(i); d.n += i < 0 ? 2 : 1; return; } if (o instanceof Long) { long l = ((Long) o).longValue(); out.print(l); out.print("L"); d.n += l < 0 ? 2 : 1; return; } if (o instanceof Short) { short s = ((Short) o).shortValue(); d.append("sh "); out.print(s); d.n += s < 0 ? 2 : 1; return; } if (o instanceof Float) { d.append("fl ", 2); quoteToPrintWriter(str(o), out); return; } if (o instanceof Double) { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); return; } if (o instanceof BigInteger) { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; return; } } if (o instanceof Boolean) { d.append(((Boolean) o).booleanValue() ? "t" : "f"); return; } if (o instanceof Character) { d.append(quoteCharacter((Character) o)); return; } if (o instanceof File) { d.append("File ").append(quote(((File) o).getPath())); return; } // referencable objects follow Integer ref = d.seen.get(o); if (o instanceof String && ref == null) ref = d.strings.get((String) o); if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; } if (!(o instanceof String)) d.seen.put(o, d.n); // record token number else { String s = d.stringSizeLimit != 0 ? shorten((String) o, d.stringSizeLimit) : (String) o; if (!d.noStringSharing) { if (d.shareStringsLongerThan == Integer.MAX_VALUE) d.seen.put(o, d.n); if (l(s) >= d.shareStringsLongerThan) d.strings.put(s, d.n); } quoteToPrintWriter(s, d.out); d.n++; return; } if (o instanceof Set) { /*O set2 = unwrapSynchronizedSet(o); if (set2 != o) { d.append("sync"); o = set2; } TODO */ if (((Set) o) instanceof TreeSet) { d.append(isCISet_gen(((Set) o)) ? "ciset" : "treeset"); structure_1(new ArrayList(((Set) o)), d); return; } // assume it's a HashSet or LinkedHashSet d.append(((Set) o) instanceof LinkedHashSet ? "lhs" : "hashset"); structure_1(new ArrayList(((Set) o)), d); return; } String name = c.getName(); if (o instanceof Collection && !startsWith(name, "main$") /* && neq(name, "main$Concept$RefL") */) { // it's a list if (name.equals("java.util.Collections$SynchronizedList") || name.equals("java.util.Collections$SynchronizedRandomAccessList")) { d.append("sync "); { structure_1(unwrapSynchronizedList(((List) o)), d); return; } } else if (name.equals("java.util.LinkedList")) d.append("ll"); d.append("["); final int l = d.n; final Iterator it = ((Collection) o).iterator(); d.stack.add(new Runnable() { public void run() { try { if (!it.hasNext()) d.append("]"); else { d.stack.add(this); if (d.n != l) d.append(", "); structure_1(it.next(), d); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext())\r\n d.append(\"]\");\r\n else {\r\n d.sta..."; }}); return; } if (o instanceof Map && !startsWith(name, "main$")) { if (o instanceof LinkedHashMap) d.append("lhm"); else if (o instanceof HashMap) d.append("hm"); else if (o instanceof TreeMap) d.append(isCIMap_gen(((TreeMap) o)) ? "cimap" : "tm"); else if (name.equals("java.util.Collections$SynchronizedMap") || name.equals("java.util.Collections$SynchronizedSortedMap") || name.equals("java.util.Collections$SynchronizedNavigableMap")) { d.append("sync "); { structure_1(unwrapSynchronizedMap(((Map) o)), d); return; } } d.append("{"); final int l = d.n; final Iterator it = ((Map) o).entrySet().iterator(); d.stack.add(new Runnable() { boolean v = false; Map.Entry e; public void run() { if (v) { d.append("="); v = false; d.stack.add(this); structure_1(e.getValue(), d); } else { if (!it.hasNext()) d.append("}"); else { e = (Map.Entry) it.next(); v = true; d.stack.add(this); if (d.n != l) d.append(", "); structure_1(e.getKey(), d); } } } }); return; } if (c.isArray()) { if (o instanceof byte[]) { d.append("ba ").append(quote(bytesToHex((byte[]) o))); return; } final int n = Array.getLength(o); if (o instanceof boolean[]) { String hex = boolArrayToHex((boolean[]) o); int i = l(hex); while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2; d.append("boolarray ").append(n).app(" ").append(quote(substring(hex, 0, i))); return; } String atype = "array", sep = ", "; if (o instanceof int[]) { //ret "intarray " + quote(intArrayToHex((int[]) o)); atype = "intarray"; sep = " "; } d.append(atype).append("{"); d.stack.add(new Runnable() { int i; public void run() { if (i >= n) d.append("}"); else { d.stack.add(this); if (i > 0) d.append(", "); structure_1(Array.get(o, i++), d); } } }); return; } if (o instanceof Class) { d.append("class(", 2).append(quote(((Class) o).getName())).append(")"); return; } if (o instanceof Throwable) { d.append("exception(", 2).append(quote(((Throwable) o).getMessage())).append(")"); return; } if (o instanceof BitSet) { BitSet bs = (BitSet) o; d.append("bitset{", 2); int l = d.n; for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { if (d.n != l) d.append(", "); d.append(i); } d.append("}"); return; } // Need more cases? This should cover all library classes... if (name.startsWith("java.") || name.startsWith("javax.")) { d.append("j ").append(quote(str(o))); return; // Hm. this is not unstructure-able } /*if (name.equals("main$Lisp")) { fail("lisp not supported right now"); }*/ if (info.special) { if (info.customSerializer != null) { // custom serialization (_serialize method) Object o2 = invokeMethod(info.customSerializer, o); d.append("cu "); String shortName = dropPrefix("main$", name); d.append(shortName); d.out.append(' '); structure_1(o2, d); return; } else if (info.nullInstances) { d.append("null"); return; } else throw fail("unknown special type"); } String dynName = shortDynamicClassName(o); if (concept && !d.concepts.contains(dynName)) { d.concepts.add(dynName); d.append("c "); } // serialize an object with fields. // first, collect all fields and values in fv. TreeSet fields = new TreeSet(new Comparator() { public int compare(Field a, Field b) { return stdcompare(a.getName(), b.getName()); } }); Class cc = c; while (cc != Object.class) { for (Field field : getDeclaredFields_cached(cc)) { String fieldName = field.getName(); if (fieldName.equals("_persistenceInfo")) d.persistenceInfo.put(c, field); if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0) continue; fields.add(field); // put special cases here...? } cc = cc.getSuperclass(); } // TODO: S fieldOrder = getOpt(c, "_fieldOrder"); lFields = asList(fields); // Render this$1 first because unstructure needs it for constructor call. int n = l(lFields); for (int i = 0; i < n; i++) { Field f = lFields.get(i); if (f.getName().equals("this$1")) { lFields.remove(i); lFields.add(0, f); break; } } info.fields = lFields; } // << if (lFields == null) else { // ref handling for lFields != null Integer ref = d.seen.get(o); if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); return; } d.seen.put(o, d.n); // record token number } Field persistenceInfoField = (Field) (d.persistenceInfo.get(c)); Map persistenceInfo = persistenceInfoField == null ? null : (Map) persistenceInfoField.get(o); LinkedHashMap fv = new LinkedHashMap(); for (Field f : lFields) { Object value; try { value = f.get(o); } catch (Exception e) { value = "?"; } if (value != null && (persistenceInfo == null || !Boolean.FALSE.equals(persistenceInfo.get(f.getName())))) fv.put(f.getName(), value); } String name = c.getName(); String shortName = dropPrefix("main$", name); if (startsWithDigit(shortName)) shortName = name; // for anonymous classes // Now we have fields & values. Process fieldValues if it's a DynamicObject. // omit field "className" if equal to class's name if (concept && eq(fv.get("className"), shortName)) fv.remove("className"); if (o instanceof DynamicObject) { putAll(fv, (Map) fv.get("fieldValues")); fv.remove("fieldValues"); shortName = shortDynamicClassName(o); fv.remove("className"); } String singleField = fv.size() == 1 ? first(fv.keySet()) : null; d.append(shortName); d.n += countDots(shortName)*2; // correct token count final int l = d.n; final Iterator it = fv.entrySet().iterator(); d.stack.add(new Runnable() { public void run() { try { if (!it.hasNext()) { if (d.n != l) d.append(")"); } else { Map.Entry e = (Map.Entry) it.next(); d.append(d.n == l ? "(" : ", "); d.append((String) e.getKey()).append("="); d.stack.add(this); structure_1(e.getValue(), d); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (!it.hasNext()) {\r\n if (d.n != l)\r\n d.append(\")\");\r\n } else..."; }}); } catch (Exception __e) { throw rethrow(__e); } } static String programID() { return getProgramID(); } static String programID(Object o) { return getProgramID(o); } public static String join(String glue, Iterable strings) { if (strings == null) return ""; if (strings instanceof Collection) { if (((Collection) strings).size() == 1) return str(first(((Collection) strings))); } StringBuilder buf = new StringBuilder(); Iterator i = strings.iterator(); if (i.hasNext()) { buf.append(i.next()); while (i.hasNext()) buf.append(glue).append(i.next()); } return buf.toString(); } public static String join(String glue, String... strings) { return join(glue, Arrays.asList(strings)); } static String join(Iterable strings) { return join("", strings); } static String join(Iterable strings, String glue) { return join(glue, strings); } public static String join(String[] strings) { return join("", strings); } static String join(String glue, Pair p) { return p == null ? "" : str(p.a) + glue + str(p.b); } // TODO: extended multi-line strings static int javaTok_n, javaTok_elements; static boolean javaTok_opt = false; static List javaTok(String s) { ++javaTok_n; ArrayList tok = new ArrayList(); int l = s == null ? 0 : s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; // scan for whitespace while (j < l) { c = s.charAt(j); d = j+1 >= l ? '\0' : s.charAt(j+1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/")); j = Math.min(j+2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i+1 >= l ? '\0' : s.charAt(i+1); // scan for non-whitespace // Special JavaX syntax: 'identifier if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) { j += 2; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))) ++j; } else if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { int c2 = s.charAt(j); if (c2 == opener || c2 == '\n' && opener == '\'') { // allow multi-line strings, but not for ' ++j; break; } else if (c2 == '\\' && j+1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || s.charAt(j) == '\'')); // for stuff like "don't" else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L } else if (c == '[' && d == '[') { do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]")); j = Math.min(j+2, l); } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') { do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]")); j = Math.min(j+3, l); } else ++j; tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } static List javaTok(List tok) { return javaTokWithExisting(join(tok), tok); } // for now, no caching... static String[] loadSnippetAndTitle(String id) throws IOException { long snippetID = parseSnippetID(id); String json; try { URL url = new URL(tb_mainServer() + "/tb-int/get.php?id=" + snippetID + "&full=1"); json = loadPage(url); } catch (RuntimeException e) { // TODO: test for nested IOException throw new IOException("Snippet #" + snippetID + " not found or not public"); } Map map = jsonDecodeMap(json); return new String[] {map.get("text"), map.get("title")}; } static String quote(Object o) { if (o == null) return "null"; return quote(str(o)); } static String quote(String s) { if (s == null) return "null"; StringBuilder out = new StringBuilder((int) (l(s)*1.5+2)); quote_impl(s, out); return out.toString(); } static void quote_impl(String s, StringBuilder out) { out.append('"'); int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') out.append('\\').append(c); else if (c == '\r') out.append("\\r"); else if (c == '\n') out.append("\\n"); else if (c == '\0') out.append("\\0"); else out.append(c); } out.append('"'); } static IterableIterator toLines(File f) { return linesFromFile(f); } static List toLines(String s) { List lines = new ArrayList(); if (s == null) return lines; int start = 0; while (true) { int i = toLines_nextLineBreak(s, start); if (i < 0) { if (s.length() > start) lines.add(s.substring(start)); break; } lines.add(s.substring(start, i)); if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n') i += 2; else ++i; start = i; } return lines; } static int toLines_nextLineBreak(String s, int start) { for (int i = start; i < s.length(); i++) { char c = s.charAt(i); if (c == '\r' || c == '\n') return i; } return -1; } static boolean swic(String a, String b) { return startsWithIgnoreCase(a, b); } static boolean eqic(String a, String b) { if ((a == null) != (b == null)) return false; if (a == null) return true; return a.equalsIgnoreCase(b); } static boolean eqic(char a, char b) { if (a == b) return true; char u1 = Character.toUpperCase(a); char u2 = Character.toUpperCase(b); if (u1 == u2) return true; return Character.toLowerCase(u1) == Character.toLowerCase(u2); } static String newLinesToBars(String s) { return escapeNewLines(s); } static int indent_default = 2; static String indent(int indent) { return repeat(' ', indent); } static String indent(int indent, String s) { return indent(repeat(' ', indent), s); } static String indent(String indent, String s) { return indent + s.replace("\n", "\n" + indent); } static String indent(String s) { return indent(indent_default, s); } static List indent(String indent, List lines) { List l = new ArrayList(); if (lines != null) for (String s : lines) l.add(indent + s); return l; } static void updateMyCommentOnSnippet(String snippetID, String text) { try { String url = tb_mainServer() + "/tb-int/update-comment.php"; String query = "id=" + parseSnippetID(snippetID) + "&progid=" + parseSnippetID(getProgramID()) + "&text=" + urlencode(text); String page = doPost(query, url); print(page); print(snippetID + " >> " + text); } catch (Exception __e) { throw rethrow(__e); } } static int leven(String s, String t) { // degenerate cases if (s.equals(t)) return 0; int ls = s.length(), lt = t.length(); if (ls == 0) return lt; if (lt == 0) return ls; // create two work vectors of integer distances int[] v0 = new int[lt + 1]; int[] v1 = new int[lt + 1]; // initialize v0 (the previous row of distances) // this row is A[0][i]: edit distance for an empty s // the distance is just the number of characters to delete from t for (int i = 0; i < v0.length; i++) v0[i] = i; for (int i = 0; i < ls; i++) { // calculate v1 (current row distances) from the previous row v0 // first element of v1 is A[i+1][0] // edit distance is delete (i+1) chars from s to match empty t v1[0] = i + 1; // use formula to fill in the rest of the row for (int j = 0; j < lt; j++) { int cost = s.charAt(i) == t.charAt(j) ? 0 : 1; v1[j + 1] = Math.min(Math.min(v1[j] + 1, v0[j + 1] + 1), v0[j] + cost); } // swap arrays int[] v = v1; v1 = v0; v0 = v; } return v0[lt]; } static class Comment { int user_id; String user_name; String text; } static List getSnippetComments(String snippetID) { try { Object json = jsonDecode(loadPage(tb_mainServer() + "/tb-int/get-comments.php?id=" + parseSnippetID(snippetID))); //System.out.println("json: " + json); List list = new ArrayList(); for (Object _row : (List) json) { Map row = (Map) _row; Comment c = new Comment(); c.user_id = Integer.parseInt((String) row.get("author")); c.user_name = (String) row.get("user_name"); c.text = (String) row.get("text"); list.add(c); } return list; } catch (Exception __e) { throw rethrow(__e); } } static RuntimeException rethrow(Throwable t) { if (t instanceof Error) _handleError((Error) t); throw t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } static RuntimeException rethrow(String msg, Throwable t) { throw new RuntimeException(msg, t); } static boolean isJavaxSafe(String snippetID) { try { URL url = new URL(tb_mainServer() + "/tb-int/is-javax-safe.php?id=" + parseSnippetID(snippetID)); String text = loadPage(url); return text.startsWith("{\"safe\":\"1\"}"); } catch (Exception __e) { throw rethrow(__e); } } static List> myNonAbstractClassesImplementing(Class base) { List> l = new ArrayList(); for (String name : myInnerClasses()) { Class c = _getClass("main$" + name); if (c != null && !isAbstract(c) && isSubtypeOf(c, base)) l.add(c); } return l; } static List myInnerClasses_list=litlist( "VF1", "LSplitInput", "LId", "LInputPattern", "ReversibleFunctionImpl", "LChange", "LGetListElement", "LFixWhitespace", "Matrix", "FunctionImpl", "LSwitch", "LFixedFunction", "LOutPattern", "LEach", "RunnersUp", "RFToLines", "LMath2", "LMath", "LRemoveInputSuffix", "LFunctionPlusPrevious", "FStringsOnly", "F0", "FLength", "LFixedPositions", "FToUpper", "IterableIterator", "LCombineTwoFunctions", "Code", "FCommonPrefix", "LTrivial", "CloseableIterableIterator", "LFixed", "FCommonSuffix", "getOpt_Map", "LOutSuffix", "LUse", "LCertainElement", "structure_Data", "IVF1", "FNonMarkedAsList", "_MethodCache", "structure_ClassInfo", "RFJavaTok", "LFindOutInIn", "LBla", "LOneWordChanged", "Case", "Function", "FIsString", "LearnerImpl", "LMap", "CriticalAction", "FBeforeColon", "LCharShift", "IResourceLoader", "EscapeCase", "LPost", "Pair", "LFixedSubstring", "Comment", "LCombineTwoFunctions2", "FDropFirstLines", "LPrefixSuffix", "LEmptyBox", "LMulti", "FJoin", "Learner", "Base", "LWrap", "FRegExp", "FNumbersOnly", "MRUCache", "FAfterColon", "DynamicObject", "LIntersperse", "FUnquote", "ReversibleFunction", "LDistinguishList", "LFirst", "LHotwire", "PersistableThrowable", "LBox", "LBox2", "FStringAsChars"); static List myInnerClasses() { return myInnerClasses_list; } static boolean isAbstract(Class c) { return (c.getModifiers() & Modifier.ABSTRACT) != 0; } static boolean isInteger(String s) { int n = l(s); if (n == 0) return false; int i = 0; if (s.charAt(0) == '-') if (++i >= n) return false; while (i < n) { char c = s.charAt(i); if (c < '0' || c > '9') return false; ++i; } return true; } static Field makeAccessible(Field f) { try { f.setAccessible(true); } catch (Throwable e) { // Note: The error reporting only works with Java VM option --illegal-access=deny vmBus_send("makeAccessible_error",e, f); } return f; } static Method makeAccessible(Method m) { try { m.setAccessible(true); } catch (Throwable e) { vmBus_send("makeAccessible_error",e, m); } return m; } static Constructor makeAccessible(Constructor c) { try { c.setAccessible(true); } catch (Throwable e) { vmBus_send("makeAccessible_error",e, c); } return c; } static A printIndent(A o) { print(indentx(str(o))); return o; } static A printIndent(String indent, A o) { print(indentx(indent, str(o))); return o; } static void printIndent(int indent, Object o) { print(indentx(indent, str(o))); } // Use like this: printVars(+x, +y); // Or: printVars("bla", +x); static void printVars(Object... params) { String s = ""; if (odd(l(params))) { s = str(first(params)); if (endsWithLetter(s)) s += ": "; params = dropFirst(params); } print(s + renderVars(params)); } static BigInteger[] makeBigIntArray(List in) { BigInteger[] big = new BigInteger[in.size()]; for (int i = 0; i < in.size(); i++) big[i] = new BigInteger(in.get(i)); return big; } static BigInteger bigint(String s) { return new BigInteger(s); } static BigInteger bigint(long l) { return BigInteger.valueOf(l); } static boolean arraysEqual(Object[] a, Object[] b) { if (a.length != b.length) return false; for (int i = 0; i < a.length; i++) if (neq(a[i], b[i])) return false; return true; } static String struct(Object o) { return structure(o); } static String struct(Object o, structure_Data data) { return structure(o, data); } static int l(Object[] a) { return a == null ? 0 : a.length; } static int l(boolean[] a) { return a == null ? 0 : a.length; } static int l(byte[] a) { return a == null ? 0 : a.length; } static int l(short[] a) { return a == null ? 0 : a.length; } static int l(long[] a) { return a == null ? 0 : a.length; } static int l(int[] a) { return a == null ? 0 : a.length; } static int l(float[] a) { return a == null ? 0 : a.length; } static int l(double[] a) { return a == null ? 0 : a.length; } static int l(char[] a) { return a == null ? 0 : a.length; } static int l(Collection c) { return c == null ? 0 : c.size(); } static int l(Iterator i) { return iteratorCount_int_close(i); } // consumes the iterator && closes it if possible static int l(Map m) { return m == null ? 0 : m.size(); } static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); } static int l(Object o) { return o == null ? 0 : o instanceof String ? l((String) o) : o instanceof Map ? l((Map) o) : o instanceof Collection ? l((Collection) o) : o instanceof Object[] ? l((Object[]) o) : o instanceof boolean[] ? l((boolean[]) o) : o instanceof byte[] ? l((byte[]) o) : o instanceof char[] ? l((char[]) o) : o instanceof short[] ? l((short[]) o) : o instanceof int[] ? l((int[]) o) : o instanceof float[] ? l((float[]) o) : o instanceof double[] ? l((double[]) o) : o instanceof long[] ? l((long[]) o) : (Integer) call(o, "size"); } static RuntimeException tilt() { throw fail("tilt"); } static boolean eq(Object a, Object b) { return a == b || (a == null ? b == null : b != null && a.equals(b)); } static boolean neq(Object a, Object b) { return !eq(a, b); } static String str(Object o) { return o == null ? "null" : o.toString(); } static String str(char[] c) { return new String(c); } // compile JavaX source and load main class static Class compileAndLoadMainClass(String src) { try { Object javax = getJavaX(); File srcDir = (File) (call(javax, "TempDirMaker_make")); File classesDir = (File) (call(javax, "TempDirMaker_make")); saveTextFile(new File(srcDir, "main.java"), src); List libraries = new ArrayList(); set(javax, "safeTranslate", true); File transpiledDir = (File) (call(javax, "topLevelTranslate", srcDir, libraries)); String javacOutput = (String) (call(javax, "compileJava", transpiledDir, libraries, classesDir)); System.out.println(javacOutput); URL[] urls = {classesDir.toURI().toURL()}; // make class loader URLClassLoader classLoader = new URLClassLoader(urls); // load main class Class mainClass = classLoader.loadClass("main"); return mainClass; } catch (Exception __e) { throw rethrow(__e); } } static String commonPrefix(String a, String b) { int i = 0, nb = b.length(), na = a.length(), n = min(na, nb); while (i < n && a.charAt(i) == b.charAt(i)) ++i; return i == nb ? b : substring(a, 0, i); } static String commonPrefix(List l) { if (empty(l)) return ""; String s = first(l); for (int i = 1; i < l(l); i++) { if (empty(s)) return s; s = commonPrefix(s, l.get(i)); } return s; } static String commonSuffix(String a, String b) { int i = 0, nb = b.length(), na = a.length(), n = min(na, nb); while (i < n && a.charAt(na-1-i) == b.charAt(nb-1-i)) ++i; return i == nb ? b : substring(a, na-i); } public static long parseSnippetID(String snippetID) { long id = Long.parseLong(shortenSnippetID(snippetID)); if (id == 0) throw fail("0 is not a snippet ID"); return id; } static String repeat(char c, int n) { n = Math.max(n, 0); char[] chars = new char[n]; for (int i = 0; i < n; i++) chars[i] = c; return new String(chars); } static List repeat(A a, int n) { n = Math.max(n, 0); List l = new ArrayList(n); for (int i = 0; i < n; i++) l.add(a); return l; } static List repeat(int n, A a) { return repeat(a, n); } static int count(Collection l, Object o) { int count = 0; for (Object x : l) if (eq(x, o)) ++count; return count; } static String javaQuote(String s) { return quote(s); } static String patternQuote(String s) { return s.length() == 0 ? "" : Pattern.quote(s); } static Thread startDaemonThread(Object runnable) { return startDaemonThread(defaultThreadName(), runnable); } static Thread startDaemonThread(String name, Object runnable) { runnable = wrapAsActivity(runnable); return startThread(newDaemonThread(toRunnable(runnable), name)); } static String _userHome; static String userHome() { if (_userHome == null) return actualUserHome(); return _userHome; } static File userHome(String path) { return new File(userDir(), path); } public static String readTextFile(String fileName, String defaultContents) throws IOException { return loadTextFile(fileName, defaultContents); } public static String readTextFile(File file) { try { return readTextFile(file, null); } catch (IOException e) { throw new RuntimeException(e); } } public static String readTextFile(File file, String defaultContents) throws IOException { return loadTextFile(file.getPath(), defaultContents); } static String tb_mainServer_default = "http://code.botcompany.de:8081"; static Object tb_mainServer_override; // func -> S static String tb_mainServer() { if (tb_mainServer_override != null) return (String) callF(tb_mainServer_override); return trim(loadTextFile(tb_mainServer_file(), tb_mainServer_default)); } static File tb_mainServer_file() { return getProgramFile("#1001638", "mainserver.txt"); } static boolean tb_mainServer_isDefault() { return eq(tb_mainServer(), tb_mainServer_default); } static String urlencode(String x) { try { return URLEncoder.encode(unnull(x), "UTF-8"); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } static ThreadLocal doPost_silently = new ThreadLocal(); static ThreadLocal doPost_timeout = new ThreadLocal(); static ThreadLocal> doPost_extraHeaders = new ThreadLocal(); static String doPost(Map urlParameters, String url) { return doPost(makePostData(urlParameters), url); } static String doPost(String urlParameters, String url) { try { URL _url = new URL(url); ping(); return doPost(urlParameters, _url.openConnection(), _url); } catch (Exception __e) { throw rethrow(__e); } } static String doPost(String urlParameters, URLConnection conn, URL url) { try { boolean silently = isTrue(optParam(doPost_silently)); Long timeout = optParam(doPost_timeout); Map extraHeaders = optPar(doPost_extraHeaders); setHeaders(conn); for (String key : keys(extraHeaders)) { conn.setRequestProperty(key, extraHeaders.get(key)); } int l = lUtf8(urlParameters); if (!silently) print("Sending POST request: " + hideCredentials(url) + " (" + l + " bytes)"); // connect and do POST if (timeout != null) setURLConnectionTimeouts(conn, timeout); ((HttpURLConnection) conn).setRequestMethod("POST"); conn.setDoOutput(true); conn.setRequestProperty("Content-Length", str(l)); OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); writer.write(urlParameters); writer.flush(); String contents = loadPage_utf8(conn, url, false); writer.close(); return contents; } catch (Exception __e) { throw rethrow(__e); } } static Class hotwire(String src) { assertFalse(_inCore()); Class j = getJavaX(); if (isAndroid()) { synchronized(j) { // hopefully this goes well... List libraries = new ArrayList(); File srcDir = (File) call(j, "transpileMain", src, libraries); if (srcDir == null) throw fail("transpileMain returned null (src=" + quote(src) + ")"); Object androidContext = get(j, "androidContext"); return (Class) call(j, "loadx2android", srcDir, src); } } else { Class c = (Class) (call(j, "hotwire", src)); hotwire_copyOver(c); return c; } } static A set(A o, String field, Object value) { if (o == null) return null; if (o instanceof Class) set((Class) o, field, value); else try { Field f = set_findField(o.getClass(), field); makeAccessible(f); smartSet(f, o, value); } catch (Exception e) { throw new RuntimeException(e); } return o; } static void set(Class c, String field, Object value) { if (c == null) return; try { Field f = set_findStaticField(c, field); makeAccessible(f); smartSet(f, null, value); } catch (Exception e) { throw new RuntimeException(e); } } static Field set_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Static field '" + field + "' not found in " + c.getName()); } static Field set_findField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Field '" + field + "' not found in " + c.getName()); } static Object call(Object o) { return callF(o); } // varargs assignment fixer for a single string array argument static Object call(Object o, String method, String[] arg) { return call(o, method, new Object[] {arg}); } static Object call(Object o, String method, Object... args) { //ret call_cached(o, method, args); return call_withVarargs(o, method, args); } // get purpose 1: access a list/array/map (safer version of x.get(y)) static A get(List l, int idx) { return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null; } // seems to conflict with other signatures /*static B get(Map map, A key) { ret map != null ? map.get(key) : null; }*/ static A get(A[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : null; } // default to false static boolean get(boolean[] l, int idx) { return idx >= 0 && idx < l(l) ? l[idx] : false; } // get purpose 2: access a field by reflection or a map static Object get(Object o, String field) { try { if (o == null) return null; if (o instanceof Class) return get((Class) o, field); if (o instanceof Map) return ((Map) o).get(field); Field f = getOpt_findField(o.getClass(), field); if (f != null) { makeAccessible(f); return f.get(o); } if (o instanceof DynamicObject) return ((DynamicObject) o).fieldValues.get(field); } catch (Exception e) { throw asRuntimeException(e); } throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName()); } static Object get_raw(String field, Object o) { return get_raw(o, field); } static Object get_raw(Object o, String field) { try { if (o == null) return null; Field f = get_findField(o.getClass(), field); makeAccessible(f); return f.get(o); } catch (Exception __e) { throw rethrow(__e); } } static Object get(Class c, String field) { try { Field f = get_findStaticField(c, field); makeAccessible(f); return f.get(null); } catch (Exception e) { throw new RuntimeException(e); } } static Field get_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Static field '" + field + "' not found in " + c.getName()); } static Field get_findField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); throw new RuntimeException("Field '" + field + "' not found in " + c.getName()); } static Object get(String field, Object o) { return get(o, field); } static String upper(String s) { return s == null ? null : s.toUpperCase(); } static char upper(char c) { return Character.toUpperCase(c); } //static final Map> getOpt_cache = newDangerousWeakHashMap(f getOpt_special_init); static class getOpt_Map extends WeakHashMap { getOpt_Map() { if (getOpt_special == null) getOpt_special = new HashMap(); clear(); } public void clear() { super.clear(); //print("getOpt clear"); put(Class.class, getOpt_special); put(String.class, getOpt_special); } } static final Map> getOpt_cache = _registerDangerousWeakMap(synchroMap(new getOpt_Map())); //static final Map> getOpt_cache = _registerWeakMap(synchroMap(new getOpt_Map)); static HashMap getOpt_special; // just a marker /*static void getOpt_special_init(Map map) { map.put(Class.class, getOpt_special); map.put(S.class, getOpt_special); }*/ static Object getOpt_cached(Object o, String field) { try { if (o == null) return null; Class c = o.getClass(); HashMap map; synchronized(getOpt_cache) { map = getOpt_cache.get(c); if (map == null) map = getOpt_makeCache(c); } if (map == getOpt_special) { if (o instanceof Class) return getOpt((Class) o, field); /*if (o instanceof S) ret getOpt(getBot((S) o), field);*/ if (o instanceof Map) return ((Map) o).get(field); } Field f = map.get(field); if (f != null) return f.get(o); if (o instanceof DynamicObject) return mapGet2(((DynamicObject) o).fieldValues, field); return null; } catch (Exception __e) { throw rethrow(__e); } } // used internally - we are in synchronized block static HashMap getOpt_makeCache(Class c) { HashMap map; if (isSubtypeOf(c, Map.class)) map = getOpt_special; else { map = new HashMap(); if (!reflection_classesNotToScan().contains(c.getName())) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) { makeAccessible(f); String name = f.getName(); if (!map.containsKey(name)) map.put(name, f); } _c = _c.getSuperclass(); } while (_c != null); } } if (getOpt_cache != null) getOpt_cache.put(c, map); return map; } static ArrayList litlist(A... a) { ArrayList l = new ArrayList(a.length); for (A x : a) l.add(x); return l; } static String classNameToVM(String name) { return name.replace(".", "$"); } static boolean empty(Collection c) { return c == null || c.isEmpty(); } static boolean empty(CharSequence s) { return s == null || s.length() == 0; } static boolean empty(Map map) { return map == null || map.isEmpty(); } static boolean empty(Object[] o) { return o == null || o.length == 0; } static boolean empty(Object o) { if (o instanceof Collection) return empty((Collection) o); if (o instanceof String) return empty((String) o); if (o instanceof Map) return empty((Map) o); if (o instanceof Object[]) return empty((Object[]) o); if (o instanceof byte[]) return empty((byte[]) o); if (o == null) return true; throw fail("unknown type for 'empty': " + getType(o)); } static boolean empty(Iterator i) { return i == null || !i.hasNext(); } static boolean empty(float[] a) { return a == null || a.length == 0; } static boolean empty(int[] a) { return a == null || a.length == 0; } static boolean empty(long[] a) { return a == null || a.length == 0; } static boolean empty(byte[] a) { return a == null || a.length == 0; } static boolean empty(short[] a) { return a == null || a.length == 0; } static boolean empty(File f) { return getFileSize(f) == 0; } static ArrayList asList(A[] a) { return a == null ? new ArrayList() : new ArrayList(Arrays.asList(a)); } static ArrayList asList(int[] a) { if (a == null) return null; ArrayList l = emptyList(a.length); for (int i : a) l.add(i); return l; } static ArrayList asList(float[] a) { if (a == null) return null; ArrayList l = emptyList(a.length); for (float i : a) l.add(i); return l; } static ArrayList asList(Iterable s) { if (s instanceof ArrayList) return (ArrayList) s; ArrayList l = new ArrayList(); if (s != null) for (A a : s) l.add(a); return l; } static ArrayList asList(Enumeration e) { ArrayList l = new ArrayList(); if (e != null) while (e.hasMoreElements()) l.add(e.nextElement()); return l; } static void setOpt_raw(Object o, String field, Object value) { try { if (o == null) return; if (o instanceof Class) setOpt_raw((Class) o, field, value); else { Field f = setOpt_raw_findField(o.getClass(), field); if (f != null) { makeAccessible(f); smartSet(f, o, value); } } } catch (Exception __e) { throw rethrow(__e); } } static void setOpt_raw(Class c, String field, Object value) { try { if (c == null) return; Field f = setOpt_raw_findStaticField(c, field); if (f != null) { makeAccessible(f); smartSet(f, null, value); } } catch (Exception __e) { throw rethrow(__e); } } static Field setOpt_raw_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } static Field setOpt_raw_findField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } static void smartSet(Field f, Object o, Object value) throws Exception { try { f.set(o, value); } catch (Exception e) { Class type = f.getType(); // take care of common case (long to int) if (type == int.class && value instanceof Long) value = ((Long) value).intValue(); if (type == LinkedHashMap.class && value instanceof Map) { f.set(o, asLinkedHashMap((Map) value)); return; } throw e; } } static A setDyn(A o, String key, Object value) { if (o == null) return o; setDynObjectValue(o, key, value); return o; } static ThreadLocal> checkFileNotTooBigToRead_tl = new ThreadLocal(); static void checkFileNotTooBigToRead(File f) { callF(checkFileNotTooBigToRead_tl.get(), f); } static File newFile(File base, String... names) { for (String name : names) base = new File(base, name); return base; } static File newFile(String name) { return name == null ? null : new File(name); } static File newFile(String base, String... names) { return newFile(newFile(base), names); } static boolean isLocalSnippetID(String snippetID) { return isSnippetID(snippetID) && isLocalSnippetID(psI(snippetID)); } static boolean isLocalSnippetID(long snippetID) { return snippetID >= 1000 && snippetID <= 9999; } static String loadLocalSnippet(String snippetID) { return loadLocalSnippet(psI(snippetID)); } static String loadLocalSnippet(long snippetID) { return loadTextFile(localSnippetFile(snippetID)); } static IResourceLoader vm_getResourceLoader() { return proxy(IResourceLoader.class, vm_generalMap_get("_officialResourceLoader")); } static String fsI(String id) { return formatSnippetID(id); } static String fsI(long id) { return formatSnippetID(id); } static String md5(String text) { try { if (text == null) return "-"; return bytesToHex(md5_impl(toUtf8(text))); // maybe different than the way PHP does it... } catch (Exception __e) { throw rethrow(__e); } } static String md5(byte[] data) { if (data == null) return "-"; return bytesToHex(md5_impl(data)); } static byte[] md5_impl(byte[] data) { try { return MessageDigest.getInstance("MD5").digest(data); } catch (Exception __e) { throw rethrow(__e); } } static String md5(File file) { return md5OfFile(file); } static boolean isTrue(Object o) { if (o instanceof Boolean) return ((Boolean) o).booleanValue(); if (o == null) return false; if (o instanceof ThreadLocal) // TODO: remove this return isTrue(((ThreadLocal) o).get()); throw fail(getClassName(o)); } static String standardCredentials() { String user = standardCredentialsUser(); String pass = standardCredentialsPass(); if (nempty(user) && nempty(pass)) return "&_user=" + urlencode(user) + "&_pass=" + urlencode(pass); return ""; } /** writes safely (to temp file, then rename) */ static File saveTextFile(String fileName, String contents) throws IOException { CriticalAction action = beginCriticalAction("Saving file " + fileName + " (" + l(contents) + " chars)"); try { File file = new File(fileName); mkdirsForFile(file); String tempFileName = fileName + "_temp"; File tempFile = new File(tempFileName); if (contents != null) { if (tempFile.exists()) try { String saveName = tempFileName + ".saved." + now(); copyFile(tempFile, new File(saveName)); } catch (Throwable e) { printStackTrace(e); } FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath()); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.print(contents); printWriter.close(); } if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (contents != null) if (!tempFile.renameTo(file)) throw new IOException("Can't rename " + tempFile + " to " + file); vmBus_send("wroteFile", file); return file; } finally { action.done(); } } static File saveTextFile(File fileName, String contents) { try { saveTextFile(fileName.getPath(), contents); return fileName; } catch (Exception __e) { throw rethrow(__e); } } static File getGlobalCache() { File file = new File(javaxCachesDir(), "Binary Snippets"); file.mkdirs(); return file; } static A setThreadLocal(ThreadLocal tl, A value) { if (tl == null) return null; A old = tl.get(); tl.set(value); return old; } static int loadPage_defaultTimeout = 60000; static ThreadLocal loadPage_charset = new ThreadLocal(); static boolean loadPage_allowGzip = true, loadPage_debug; static boolean loadPage_anonymous = false; // don't send computer ID static int loadPage_verboseness = 100000; static int loadPage_retries = 1; //60; // seconds static ThreadLocal loadPage_silent = new ThreadLocal(); static volatile int loadPage_forcedTimeout; // ms static ThreadLocal loadPage_forcedTimeout_byThread = new ThreadLocal(); // ms static ThreadLocal>> loadPage_responseHeaders = new ThreadLocal(); static ThreadLocal> loadPage_extraHeaders = new ThreadLocal(); static ThreadLocal loadPage_sizeLimit = new ThreadLocal(); public static String loadPageSilently(String url) { try { return loadPageSilently(new URL(loadPage_preprocess(url))); } catch (Exception __e) { throw rethrow(__e); } } public static String loadPageSilently(URL url) { try { if (url.getProtocol().equals("https")) disableCertificateValidation(); if (!networkAllowanceTest(str(url))) throw fail("Not allowed: " + url); IOException e = null; for (int tries = 0; tries < loadPage_retries; tries++) try { URLConnection con = loadPage_openConnection(url); return loadPage(con, url); } catch (IOException _e) { e = _e; if (loadPageThroughProxy_enabled) { print("Trying proxy because of: " + e); try { return loadPageThroughProxy(str(url)); } catch (Throwable e2) { print(" " + exceptionToStringShort(e2)); } } else if (loadPage_debug) print(exceptionToStringShort(e)); if (tries < loadPage_retries-1) sleepSeconds(1); } throw e; } catch (Exception __e) { throw rethrow(__e); } } static String loadPage_preprocess(String url) { if (url.startsWith("tb/")) // don't think we use this anymore url = tb_mainServer() + "/" + url; if (url.indexOf("://") < 0) url = "http://" + url; return url; } static String loadPage(String url) { try { url = loadPage_preprocess(url); if (!isTrue(loadPage_silent.get())) printWithTime("Loading: " + hideCredentials(url)); return loadPageSilently(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } static String loadPage(URL url) { return loadPage(url.toExternalForm()); } static String loadPage(URLConnection con, URL url) throws IOException { return loadPage(con, url, true); } static String loadPage(URLConnection con, URL url, boolean addHeaders) throws IOException { Map extraHeaders = getAndClearThreadLocal(loadPage_extraHeaders); Long limit = optPar(loadPage_sizeLimit); if (addHeaders) try { if (!loadPage_anonymous) setHeaders(con); if (loadPage_allowGzip) con.setRequestProperty("Accept-Encoding", "gzip"); con.setRequestProperty("X-No-Cookies", "1"); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); } catch (Throwable e) {} // fails if within doPost vm_generalSubMap("URLConnection per thread").put(currentThread(), con); loadPage_responseHeaders.set(con.getHeaderFields()); InputStream in = null; try { in = urlConnection_getInputStream(con); //vm_generalSubMap("InputStream per thread").put(currentThread(), in); if (loadPage_debug) print("Put stream in map: " + currentThread()); String contentType = con.getContentType(); if (contentType == null) { //printStruct("Headers: ", con.getHeaderFields()); throw new IOException("Page could not be read: " + hideCredentials(url)); } //print("Content-Type: " + contentType); String charset = loadPage_charset == null ? null : loadPage_charset.get(); if (charset == null) charset = loadPage_guessCharset(contentType); if ("gzip".equals(con.getContentEncoding())) { if (loadPage_debug) print("loadPage: Using gzip."); in = newGZIPInputStream(in); } Reader r; try { r = new InputStreamReader(in, unquote(charset)); } catch (UnsupportedEncodingException e) { print(toHex(utf8(charset))); throw e; } boolean silent = isTrue(loadPage_silent.get()); StringBuilder buf = new StringBuilder(); int n = 0; while (limit == null || n < limit) { ping(); int ch = r.read(); if (ch < 0) break; buf.append((char) ch); ++n; if (!silent && (n % loadPage_verboseness) == 0) print(" " + n + " chars read"); } return buf.toString(); } finally { if (loadPage_debug) print("loadPage done"); //vm_generalSubMap("InputStream per thread").remove(currentThread()); vm_generalSubMap("URLConnection per thread").remove(currentThread()); if (in != null) in.close(); } } static String loadPage_guessCharset(String contentType) { Matcher m = regexpMatcher("text/[a-z]+;\\s*charset=([^\\s]+)\\s*", contentType); String match = m.matches() ? m.group(1) : null; if (loadPage_debug) print("loadPage: contentType=" + contentType + ", match: " + match); /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */ //return or(match, "ISO-8859-1"); return or(match, "UTF-8"); } static URLConnection loadPage_openConnection(URL url) { URLConnection con = openConnection(url); int timeout = toInt(loadPage_forcedTimeout_byThread.get()); if (timeout == 0) timeout = loadPage_forcedTimeout; if (timeout != 0) setURLConnectionTimeouts(con, loadPage_forcedTimeout); else setURLConnectionDefaultTimeouts(con, loadPage_defaultTimeout); return con; } static boolean endsWithLetterOrDigit(String s) { return s != null && s.length() > 0 && Character.isLetterOrDigit(s.charAt(s.length()-1)); } static void ping_okInCleanUp() { if (ping_pauseAll || ping_anyActions ) ping_impl(true); } // this syntax should be removed... static Object getThreadLocal(Object o, String name) { ThreadLocal t = (ThreadLocal) (getOpt(o, name)); return t != null ? t.get() : null; } static A getThreadLocal(ThreadLocal tl) { return tl == null ? null : tl.get(); } static A getThreadLocal(ThreadLocal tl, A defaultValue) { return or(getThreadLocal(tl), defaultValue); } static ThreadLocal print_byThread_dontCreate() { return print_byThread; } static boolean isFalse(Object o) { return eq(false, o); } static Map> callF_cache = newDangerousWeakHashMap(); static A callF(F0 f) { return f == null ? null : f.get(); } static void callF(VF1 f, A a) { if (f != null) f.get(a); } static Object callF(Object f, Object... args) { try { if (f instanceof String) return callMCWithVarArgs((String) f, args); // possible SLOWDOWN over callMC if (f instanceof Runnable) { ((Runnable) f).run(); return null; } if (f == null) return null; Class c = f.getClass(); ArrayList methods; synchronized(callF_cache) { methods = callF_cache.get(c); if (methods == null) methods = callF_makeCache(c); } int n = l(methods); if (n == 0) { throw fail("No get method in " + getClassName(c)); } if (n == 1) return invokeMethod(methods.get(0), f, args); for (int i = 0; i < n; i++) { Method m = methods.get(i); if (call_checkArgs(m, args, false)) return invokeMethod(m, f, args); } throw fail("No matching get method in " + getClassName(c)); } catch (Exception __e) { throw rethrow(__e); } } // used internally static ArrayList callF_makeCache(Class c) { ArrayList l = new ArrayList(); Class _c = c; do { for (Method m : _c.getDeclaredMethods()) if (m.getName().equals("get")) { makeAccessible(m); l.add(m); } if (!l.isEmpty()) break; _c = _c.getSuperclass(); } while (_c != null); callF_cache.put(c, l); return l; } static String fixNewLines(String s) { int i = indexOf(s, '\r'); if (i < 0) return s; int l = s.length(); StringBuilder out = new StringBuilder(l); out.append(s, 0, i); for (; i < l; i++) { char c = s.charAt(i); if (c != '\r') out.append(c); else { out.append('\n'); if (i+1 < l && s.charAt(i+1) == '\n') ++i; } } return out.toString(); } static void print_append(Appendable buf, String s, int max) { try { synchronized(buf) { buf.append(s); if (buf instanceof StringBuffer) rotateStringBuffer(((StringBuffer) buf), max); else if (buf instanceof StringBuilder) rotateStringBuilder(((StringBuilder) buf), max); } } catch (Exception __e) { throw rethrow(__e); } } static RuntimeException asRuntimeException(Throwable t) { if (t instanceof Error) _handleError((Error) t); return t instanceof RuntimeException ? (RuntimeException) t : new RuntimeException(t); } static A assertEquals(Object x, A y) { return assertEquals(null, x, y); } static A assertEquals(String msg, Object x, A y) { if (assertVerbose()) return assertEqualsVerbose(msg, x, y); if (!(x == null ? y == null : x.equals(y))) throw fail((msg != null ? msg + ": " : "") + y + " != " + x); return y; } static List javaTokC(String s) { if (s == null) return null; int l = s.length(); ArrayList tok = new ArrayList(); int i = 0; while (i < l) { int j = i; char c, d; // scan for whitespace while (j < l) { c = s.charAt(j); d = j+1 >= l ? '\0' : s.charAt(j+1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/")); j = Math.min(j+2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } i = j; if (i >= l) break; c = s.charAt(i); d = i+1 >= l ? '\0' : s.charAt(i+1); // scan for non-whitespace if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener || s.charAt(j) == '\n') { // end at \n to not propagate unclosed string literal errors ++j; break; } else if (s.charAt(j) == '\\' && j+1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't" else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L } else if (c == '[' && d == '[') { do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]")); j = Math.min(j+2, l); } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') { do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]")); j = Math.min(j+3, l); } else ++j; tok.add(javaTok_substringC(s, i, j)); i = j; } return tok; } static A popLast(List l) { return liftLast(l); } static List popLast(int n, List l) { return liftLast(n, l); } // This is a bit rough... finds static and non-static methods. static Method findMethodNamed(Object obj, String method) { if (obj == null) return null; if (obj instanceof Class) return findMethodNamed((Class) obj, method); return findMethodNamed(obj.getClass(), method); } static Method findMethodNamed(Class c, String method) { while (c != null) { for (Method m : c.getDeclaredMethods()) if (m.getName().equals(method)) { makeAccessible(m); return m; } c = c.getSuperclass(); } return null; } static void quoteToPrintWriter(String s, PrintWriter out) { if (s == null) { out.print("null"); return; } out.print('"'); int l = s.length(); for (int i = 0; i < l; i++) { char c = s.charAt(i); if (c == '\\' || c == '"') { out.print('\\'); out.print(c); } else if (c == '\r') out.print("\\r"); else if (c == '\n') out.print("\\n"); else if (c == '\0') out.print("\\0"); else out.print(c); } out.print('"'); } static String quoteCharacter(char c) { if (c == '\'') return "'\\''"; if (c == '\\') return "'\\\\'"; if (c == '\r') return "'\\r'"; if (c == '\n') return "'\\n'"; if (c == '\t') return "'\\t'"; return "'" + c + "'"; } static boolean isCISet_gen(Iterable l) { return l instanceof TreeSet && className(((TreeSet) l).comparator()).contains("CIComp"); } static boolean startsWith(String a, String b) { return a != null && a.startsWith(b); } static boolean startsWith(String a, char c) { return nemptyString(a) && a.charAt(0) == c; } static boolean startsWith(List a, List b) { if (a == null || listL(b) > listL(a)) return false; for (int i = 0; i < listL(b); i++) if (neq(a.get(i), b.get(i))) return false; return true; } static List unwrapSynchronizedList(List l) { if (eqOneOf(className(l), "java.util.Collections$SynchronizedList", "java.util.Collections$SynchronizedRandomAccessList")) return (List) get_raw(l, "list"); return l; } static boolean isCIMap_gen(Map map) { return map instanceof TreeMap && className(((TreeMap) map).comparator()).contains("CIComp"); } static Map unwrapSynchronizedMap(Map map) { if (eqOneOf(className(map), "java.util.Collections$SynchronizedMap", "java.util.Collections$SynchronizedSortedMap", "java.util.Collections$SynchronizedNavigableMap")) return (Map) get_raw(map, "m"); return map; } public static String bytesToHex(byte[] bytes) { return bytesToHex(bytes, 0, bytes.length); } public static String bytesToHex(byte[] bytes, int ofs, int len) { StringBuilder stringBuilder = new StringBuilder(len*2); for (int i = 0; i < len; i++) { String s = "0" + Integer.toHexString(bytes[ofs+i]); stringBuilder.append(s.substring(s.length()-2, s.length())); } return stringBuilder.toString(); } static String boolArrayToHex(boolean[] a) { return bytesToHex(boolArrayToBytes(a)); } static String substring(String s, int x) { return substring(s, x, strL(s)); } static String substring(String s, int x, int y) { if (s == null) return null; if (x < 0) x = 0; if (x >= s.length()) return ""; if (y < x) y = x; if (y > s.length()) y = s.length(); return s.substring(x, y); } static Iterator emptyIterator() { return Collections.emptyIterator(); } static Object invokeMethod(Method m, Object o, Object... args) { try { try { return m.invoke(o, args); } catch (InvocationTargetException e) { throw rethrow(getExceptionCause(e)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e.getMessage() + " - was calling: " + m + ", args: " + joinWithSpace(classNames(args))); } } catch (Exception __e) { throw rethrow(__e); } } static String dropPrefix(String prefix, String s) { return s == null ? null : s.startsWith(prefix) ? s.substring(l(prefix)) : s; } static String shortDynamicClassName(Object o) { if (o instanceof DynamicObject && ((DynamicObject) o).className != null) return ((DynamicObject) o).className; return shortClassName(o); } static int stdcompare(Number a, Number b) { return cmp(a, b); } static int stdcompare(String a, String b) { return cmp(a, b); } static int stdcompare(long a, long b) { return a < b ? -1 : a > b ? 1 : 0; } static int stdcompare(Object a, Object b) { return cmp(a, b); } static Map getDeclaredFields_cache = newDangerousWeakHashMap(); static Field[] getDeclaredFields_cached(Class c) { Field[] fields; synchronized(getDeclaredFields_cache) { fields = getDeclaredFields_cache.get(c); if (fields == null) { getDeclaredFields_cache.put(c, fields = c.getDeclaredFields()); for (Field f : fields) makeAccessible(f); } } return fields; } static boolean startsWithDigit(String s) { return nempty(s) && isDigit(s.charAt(0)); } static Map putAll(Map a, Map b) { if (a != null && b != null) a.putAll(b); return a; } static Object first(Object list) { return first((Iterable) list); } static A first(List list) { return empty(list) ? null : list.get(0); } static A first(A[] bla) { return bla == null || bla.length == 0 ? null : bla[0]; } static A first(IterableIterator i) { return first((Iterator) i); } static A first(Iterator i) { return i == null || !i.hasNext() ? null : i.next(); } static A first(Iterable i) { if (i == null) return null; Iterator it = i.iterator(); return it.hasNext() ? it.next() : null; } static Character first(String s) { return empty(s) ? null : s.charAt(0); } static A first(Pair p) { return p == null ? null : p.a; } static int countDots(String s) { int n = l(s), count = 0; for (int i = 0; i < n; i++) if (s.charAt(i) == '.') ++count; return count; } static String programID; static String getProgramID() { return nempty(programID) ? formatSnippetIDOpt(programID) : "?"; } // TODO: ask JavaX instead static String getProgramID(Class c) { String id = (String) getOpt(c, "programID"); if (nempty(id)) return formatSnippetID(id); return "?"; } static String getProgramID(Object o) { return getProgramID(getMainClass(o)); } static String javaTok_substringN(String s, int i, int j) { if (i == j) return ""; if (j == i+1 && s.charAt(i) == ' ') return " "; return s.substring(i, j); } static String javaTok_substringC(String s, int i, int j) { return s.substring(i, j); } static List javaTokWithExisting(String s, List existing) { ++javaTok_n; int nExisting = javaTok_opt && existing != null ? existing.size() : 0; ArrayList tok = existing != null ? new ArrayList(nExisting) : new ArrayList(); int l = s.length(); int i = 0, n = 0; while (i < l) { int j = i; char c, d; // scan for whitespace while (j < l) { c = s.charAt(j); d = j+1 >= l ? '\0' : s.charAt(j+1); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (c == '/' && d == '*') { do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/")); j = Math.min(j+2, l); } else if (c == '/' && d == '/') { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringN(s, i, j)); ++n; i = j; if (i >= l) break; c = s.charAt(i); d = i+1 >= l ? '\0' : s.charAt(i+1); // scan for non-whitespace // Special JavaX syntax: 'identifier if (c == '\'' && Character.isJavaIdentifierStart(d) && i+2 < l && "'\\".indexOf(s.charAt(i+2)) < 0) { j += 2; while (j < l && Character.isJavaIdentifierPart(s.charAt(j))) ++j; } else if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener /*|| s.charAt(j) == '\n'*/) { // allow multi-line strings ++j; break; } else if (s.charAt(j) == '\\' && j+1 < l) j += 2; else ++j; } } else if (Character.isJavaIdentifierStart(c)) do ++j; while (j < l && (Character.isJavaIdentifierPart(s.charAt(j)) || "'".indexOf(s.charAt(j)) >= 0)); // for stuff like "don't" else if (Character.isDigit(c)) { do ++j; while (j < l && Character.isDigit(s.charAt(j))); if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L } else if (c == '[' && d == '[') { do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]")); j = Math.min(j+2, l); } else if (c == '[' && d == '=' && i+2 < l && s.charAt(i+2) == '[') { do ++j; while (j+2 < l && !s.substring(j, j+3).equals("]=]")); j = Math.min(j+3, l); } else ++j; if (n < nExisting && javaTokWithExisting_isCopyable(existing.get(n), s, i, j)) tok.add(existing.get(n)); else tok.add(javaTok_substringC(s, i, j)); ++n; i = j; } if ((tok.size() % 2) == 0) tok.add(""); javaTok_elements += tok.size(); return tok; } static boolean javaTokWithExisting_isCopyable(String t, String s, int i, int j) { return t.length() == j-i && s.regionMatches(i, t, 0, j-i); // << could be left out, but that's brave } static Map jsonDecodeMap(String s) { Object o = jsonDecode(s); if (o instanceof List && empty((List) o)) return new HashMap(); if (o instanceof Map) return (Map) o; else throw fail("Not a JSON map: " + s); } static CloseableIterableIterator linesFromFile(File f) { try { if (!f.exists()) return emptyCloseableIterableIterator(); if (ewic(f.getName(), ".gz")) return linesFromReader(utf8bufferedReader(newGZIPInputStream(f))); return linesFromReader(utf8bufferedReader(f)); } catch (Exception __e) { throw rethrow(__e); } } static CloseableIterableIterator linesFromFile(String path) { return linesFromFile(newFile(path)); } static boolean startsWithIgnoreCase(String a, String b) { return regionMatchesIC(a, 0, b, 0, b.length()); } static String asString(Object o) { return o == null ? null : o.toString(); } static String escapeNewLines(String s) { return s == null ? null : fixNewLines(s).replace("\n", " | "); } static boolean jsonDecode_useOrderedMaps = true; static Object jsonDecode(final String text) { final List tok = jsonTok(text); if (l(tok) == 1) return null; class Y { int i = 1; Object parse() { String t = tok.get(i); if (t.startsWith("\"")) { String s = unquote(tok.get(i)); i += 2; return s; } if (t.equals("{")) return parseMap(); if (t.equals("[")) return this.parseList(); // avoid loading standard function "parseList" if (t.equals("null")) { i += 2; return null; } if (t.equals("false")) { i += 2; return false; } if (t.equals("true")) { i += 2; return true; } boolean minus = false; if (t.equals("-")) { minus = true; i += 2; t = get(tok, i); } if (isInteger(t)) { i += 2; if (eq(get(tok, i), ".")) { String x = t + "." + get(tok, i+2); i += 4; double d = parseDouble(x); if (minus) d = -d; return d; } else { long l = parseLong(t); if (minus) l = -l; return l != (int) l ? new Long(l) : new Integer((int) l); } } throw new RuntimeException("Unknown token " + (i+1) + ": " + t + ": " + text); } Object parseList() { consume("["); List list = new ArrayList(); while (!tok.get(i).equals("]")) { list.add(parse()); if (tok.get(i).equals(",")) i += 2; } consume("]"); return list; } Object parseMap() { consume("{"); Map map = jsonDecode_useOrderedMaps ? new LinkedHashMap() : new TreeMap(); while (!tok.get(i).equals("}")) { String key = unquote(tok.get(i)); i += 2; consume(":"); Object value = parse(); map.put(key, value); if (tok.get(i).equals(",")) i += 2; } consume("}"); return map; } void consume(String s) { if (!tok.get(i).equals(s)) { String prevToken = i-2 >= 0 ? tok.get(i-2) : ""; String nextTokens = join(tok.subList(i, Math.min(i+4, tok.size()))); throw fail(quote(s) + " expected: " + prevToken + " " + nextTokens + " (" + i + "/" + tok.size() + ")"); } i += 2; } } return new Y().parse(); } static void _handleError(Error e) { call(javax(), "_handleError", e); } static Class _getClass(String name) { try { return Class.forName(name); } catch (ClassNotFoundException e) { return null; // could optimize this } } static Class _getClass(Object o) { return o == null ? null : o instanceof Class ? (Class) o : o.getClass(); } static Class _getClass(Object realm, String name) { try { return getClass(realm).getClassLoader().loadClass(classNameToVM(name)); } catch (ClassNotFoundException e) { return null; // could optimize this } } static boolean isSubtypeOf(Class a, Class b) { return b.isAssignableFrom(a); // << always hated that method, let's replace it! } static void vmBus_send(String msg, Object... args) { Object arg = vmBus_wrapArgs(args); pcallFAll(vm_busListeners_live(), msg, arg); pcallFAll(vm_busListenersByMessage_live().get(msg), msg, arg); } static void vmBus_send(String msg) { vmBus_send(msg, (Object) null); } static String indentx(String s) { return indentx(indent_default, s); } static String indentx(int n, String s) { return dropSuffix(repeat(' ', n), indent(n, s)); } static String indentx(String indent, String s) { return dropSuffix(indent, indent(indent, s)); } static boolean odd(int i) { return (i & 1) != 0; } static boolean odd(long i) { return (i & 1) != 0; } static boolean odd(BigInteger i) { return odd(toInt(i)); } static boolean endsWithLetter(String s) { return nempty(s) && isLetter(last(s)); } static String[] dropFirst(int n, String[] a) { return drop(n, a); } static String[] dropFirst(String[] a) { return drop(1, a); } static Object[] dropFirst(Object[] a) { return drop(1, a); } static List dropFirst(List l) { return dropFirst(1, l); } static List dropFirst(int n, Iterable i) { return dropFirst(n, toList(i)); } static List dropFirst(Iterable i) { return dropFirst(toList(i)); } static List dropFirst(int n, List l) { return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size())); } static List dropFirst(List l, int n) { return dropFirst(n, l); } static String dropFirst(int n, String s) { return substring(s, n); } static String dropFirst(String s, int n) { return substring(s, n); } static String dropFirst(String s) { return substring(s, 1); } // Use like this: renderVars(+x, +y) static String renderVars(Object... params) { List l = new ArrayList(); for (int i = 0; i+1 < l(params); i += 2) l.add(params[i] + "=" + struct(params[i+1]) + ". "); return trim(join(l)); } static int iteratorCount_int_close(Iterator i) { try { int n = 0; if (i != null) while (i.hasNext()) { i.next(); ++n; } if (i instanceof AutoCloseable) ((AutoCloseable) i).close(); return n; } catch (Exception __e) { throw rethrow(__e); } } static int min(int a, int b) { return Math.min(a, b); } static long min(long a, long b) { return Math.min(a, b); } static float min(float a, float b) { return Math.min(a, b); } static float min(float a, float b, float c) { return min(min(a, b), c); } static double min(double a, double b) { return Math.min(a, b); } static double min(double[] c) { double x = Double.MAX_VALUE; for (double d : c) x = Math.min(x, d); return x; } static float min(float[] c) { float x = Float.MAX_VALUE; for (float d : c) x = Math.min(x, d); return x; } static byte min(byte[] c) { byte x = 127; for (byte d : c) if (d < x) x = d; return x; } static short min(short[] c) { short x = 0x7FFF; for (short d : c) if (d < x) x = d; return x; } static int min(int[] c) { int x = Integer.MAX_VALUE; for (int d : c) if (d < x) x = d; return x; } static String shortenSnippetID(String snippetID) { if (snippetID.startsWith("#")) snippetID = snippetID.substring(1); String httpBlaBla = "http://tinybrain.de/"; if (snippetID.startsWith(httpBlaBla)) snippetID = snippetID.substring(httpBlaBla.length()); return "" + parseLong(snippetID); } static String defaultThreadName_name; static String defaultThreadName() { if (defaultThreadName_name == null) defaultThreadName_name = "A thread by " + programID(); return defaultThreadName_name; } static Runnable wrapAsActivity(Object r) { return toRunnable(r); } static Thread startThread(Object runnable) { return startThread(defaultThreadName(), runnable); } static Thread startThread(String name, Object runnable) { runnable = wrapAsActivity(runnable); return startThread(newThread(toRunnable(runnable), name)); } static Thread startThread(Thread t) { _registerThread(t); t.start(); return t; } static Thread newDaemonThread(Object r) { Thread t = newThread(r); t.setDaemon(true); return t; } static Thread newDaemonThread(Object r, String name) { Thread t = newThread(r, name); t.setDaemon(true); return t; } static Thread newDaemonThread(String name, Object r) { return newDaemonThread(r, name); } static Runnable toRunnable(final Object o) { if (o instanceof Runnable) return (Runnable) o; return new Runnable() { public void run() { try { callF(o) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callF(o)"; }}; } static String actualUserHome_value; static String actualUserHome() { if (actualUserHome_value == null) { if (isAndroid()) actualUserHome_value = "/storage/emulated/0/"; else actualUserHome_value = System.getProperty("user.home"); } return actualUserHome_value; } static File actualUserHome(String sub) { return newFile(new File(actualUserHome()), sub); } static File userDir() { return new File(userHome()); } static File userDir(String path) { return new File(userHome(), path); } static String trim(String s) { return s == null ? null : s.trim(); } static String trim(StringBuilder buf) { return buf.toString().trim(); } static String trim(StringBuffer buf) { return buf.toString().trim(); } static File getProgramFile(String progID, String fileName) { if (new File(fileName).isAbsolute()) return new File(fileName); return new File(getProgramDir(progID), fileName); } static File getProgramFile(String fileName) { return getProgramFile(getProgramID(), fileName); } static String unnull(String s) { return s == null ? "" : s; } static Collection unnull(Collection l) { return l == null ? emptyList() : l; } static List unnull(List l) { return l == null ? emptyList() : l; } static Map unnull(Map l) { return l == null ? emptyMap() : l; } static Iterable unnull(Iterable i) { return i == null ? emptyList() : i; } static A[] unnull(A[] a) { return a == null ? (A[]) new Object[0] : a; } static BitSet unnull(BitSet b) { return b == null ? new BitSet() : b; } //ifclass Symbol static Pair unnull(Pair p) { return p != null ? p : new Pair(null, null); } static String makePostData(Map map) { StringBuilder buf = new StringBuilder(); for (Map.Entry e : map.entrySet()) { String key = (String) (e.getKey()); Object val = e.getValue(); if (val != null) { String value = str(val); if (nempty(buf)) buf.append("&"); buf.append(urlencode(key)).append("=").append(urlencode(/*escapeMultichars*/(value))); } } return str(buf); } static String makePostData(Object... params) { StringBuilder buf = new StringBuilder(); int n = l(params); for (int i = 0; i+1 < n; i += 2) { String key = (String) (params[i]); Object val = params[i+1]; if (val != null) { String value = str(val); if (nempty(buf)) buf.append("&"); buf.append(urlencode(key)).append("=").append(urlencode(/*escapeMultichars*/(value))); } } return str(buf); } //sbool ping_actions_shareable = true; static volatile boolean ping_pauseAll = false; static int ping_sleep = 100; // poll pauseAll flag every 100 static volatile boolean ping_anyActions = false; static Map ping_actions = newWeakHashMap(); static ThreadLocal ping_isCleanUpThread = new ThreadLocal(); // always returns true static boolean ping() { if (ping_pauseAll || ping_anyActions ) ping_impl(true /* XXX */); //ifndef LeanMode ping_impl(); endifndef return true; } // returns true when it slept static boolean ping_impl(boolean okInCleanUp) { try { if (ping_pauseAll && !isAWTThread()) { do Thread.sleep(ping_sleep); while (ping_pauseAll); return true; } if (ping_anyActions) { // don't allow sharing ping_actions if (!okInCleanUp && !isTrue(ping_isCleanUpThread.get())) failIfUnlicensed(); Object action = null; synchronized(ping_actions) { if (!ping_actions.isEmpty()) { action = ping_actions.get(currentThread()); if (action instanceof Runnable) ping_actions.remove(currentThread()); if (ping_actions.isEmpty()) ping_anyActions = false; } } if (action instanceof Runnable) ((Runnable) action).run(); else if (eq(action, "cancelled")) throw fail("Thread cancelled."); } return false; } catch (Exception __e) { throw rethrow(__e); } } static A optParam(ThreadLocal tl, A defaultValue) { return optPar(tl, defaultValue); } static A optParam(ThreadLocal tl) { return optPar(tl); } static Object optParam(String name, Map params) { return mapGet(params, name); } // now also takes a map as single array entry static A optParam(Object[] opt, String name, A defaultValue) { int n = l(opt); if (n == 1 && opt[0] instanceof Map) { Map map = (Map) (opt[0]); return map.containsKey(name) ? (A) map.get(name) : defaultValue; } if (!even(l(opt))) throw fail("Odd parameter length"); for (int i = 0; i < l(opt); i += 2) if (eq(opt[i], name)) return (A) opt[i+1]; return defaultValue; } static Object optParam(Object[] opt, String name) { return optParam(opt, name, null); } static Object optParam(String name, Object[] params) { return optParam(params, name); } static A optPar(ThreadLocal tl, A defaultValue) { A a = tl.get(); if (a != null) { tl.set(null); return a; } return defaultValue; } static A optPar(ThreadLocal tl) { return optPar(tl, null); } static Object optPar(Object[] params, String name) { return optParam(params, name); } static Object optPar(String name, Object[] params) { return optParam(params, name); } static Object optPar(String name, Map params) { return optParam(name, params); } static A optPar(Object[] params, String name, A defaultValue) { return optParam(params, name, defaultValue); } static A optPar(String name, Object[] params, A defaultValue) { return optParam(params, name, defaultValue); } static void setHeaders(URLConnection con) throws IOException { String computerID = getComputerID_quick(); if (computerID != null) try { con.setRequestProperty("X-ComputerID", computerID); con.setRequestProperty("X-OS", System.getProperty("os.name") + " " + System.getProperty("os.version")); } catch (Throwable e) { //printShortException(e); } } static Set keys(Map map) { return map == null ? new HashSet() : map.keySet(); } static Set keys(Object map) { return keys((Map) map); } static int lUtf8(String s) { return l(utf8(s)); } static String hideCredentials(URL url) { return url == null ? null : hideCredentials(str(url)); } static String hideCredentials(String url) { try { if (startsWithOneOf(url, "http://", "https://") && isAGIBlueDomain(hostNameFromURL(url))) return url; } catch (Throwable e) { print("HideCredentials", e); } return url.replaceAll("([&?])(_pass|key)=[^&\\s\"]*", "$1$2="); } static String hideCredentials(Object o) { return hideCredentials(str(o)); } static URLConnection setURLConnectionTimeouts(URLConnection con, long timeout) { con.setConnectTimeout(toInt(timeout)); con.setReadTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout || con.getReadTimeout() != timeout) print("Warning: Timeouts not set by JDK."); return con; } static String loadPage_utf8(URL url) { return loadPage_utf8(url.toString()); } static String loadPage_utf8(String url) { AutoCloseable __0 = tempSetTL(loadPage_charset, "UTF-8"); try { return loadPage(url); } finally { _close(__0); }} static String loadPage_utf8(URLConnection con, URL url, boolean addHeaders) throws IOException { AutoCloseable __1 = tempSetTL(loadPage_charset, "UTF-8"); try { return loadPage(con, url, addHeaders); } finally { _close(__1); }} static void assertFalse(Object o) { if (!(eq(o, false) /*|| isFalse(pcallF(o))*/)) throw fail(str(o)); } static boolean assertFalse(boolean b) { if (b) throw fail("oops"); return b; } static boolean assertFalse(String msg, boolean b) { if (b) throw fail(msg); return b; } static boolean _inCore() { return false; } static int isAndroid_flag; static boolean isAndroid() { if (isAndroid_flag == 0) isAndroid_flag = System.getProperty("java.vendor").toLowerCase().indexOf("android") >= 0 ? 1 : -1; return isAndroid_flag > 0; } static List hotwire_copyOver_after = synchroList(); static void hotwire_copyOver(Class c) { // TODO: make a mechanism for making such "inheritable" fields for (String field : ll("print_log", "print_silent", "androidContext", "_userHome")) setOptIfNotNull(c, field, getOpt(mc(), field)); setOptIfNotNull(c, "mainBot" , getMainBot()); setOpt(c, "creator_class" , new WeakReference(mc())); pcallFAll(hotwire_copyOver_after, c); } static Object call_withVarargs(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findStaticMethod(method, args); if (me != null) return invokeMethod(me, null, args); // try varargs List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } { if (!(isStaticMethod(m))) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, null, newArgs); } throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found"); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me != null) return invokeMethod(me, o, args); // try varargs List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, o, newArgs); } throw fail("Method " + c.getName() + "." + method + "(" + joinWithComma(classNames(args)) + ") not found"); } } catch (Exception __e) { throw rethrow(__e); } } static Field getOpt_findField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field)) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } static void clear(Collection c) { if (c != null) c.clear(); } static void put(Map map, A a, B b) { if (map != null) map.put(a, b); } static void put(List l, int i, A a) { if (l != null && i >= 0 && i < l(l)) l.set(i, a); } static List _registerDangerousWeakMap_preList; static A _registerDangerousWeakMap(A map) { return _registerDangerousWeakMap(map, null); } static A _registerDangerousWeakMap(A map, Object init) { callF(init, map); if (init instanceof String) { final String f = (String) init; init = new VF1() { public void get(Map map) { try { callMC(f, map) ; } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "callMC(f, map)"; }}; } if (javax() == null) { // We're in class init if (_registerDangerousWeakMap_preList == null) _registerDangerousWeakMap_preList = synchroList(); _registerDangerousWeakMap_preList.add(pair(map, init)); return map; } call(javax(), "_registerDangerousWeakMap", map, init); return map; } static void _onLoad_registerDangerousWeakMap() { assertNotNull(javax()); if (_registerDangerousWeakMap_preList == null) return; for (Pair p : _registerDangerousWeakMap_preList) _registerDangerousWeakMap(p.a, p.b); _registerDangerousWeakMap_preList = null; } static Map synchroMap() { return synchroHashMap(); } static Map synchroMap(Map map) { return Collections.synchronizedMap(map); } static Object getOpt(Object o, String field) { return getOpt_cached(o, field); } static Object getOpt(String field, Object o) { return getOpt_cached(o, field); } static Object getOpt_raw(Object o, String field) { try { Field f = getOpt_findField(o.getClass(), field); if (f == null) return null; makeAccessible(f); return f.get(o); } catch (Exception __e) { throw rethrow(__e); } } // access of static fields is not yet optimized static Object getOpt(Class c, String field) { try { if (c == null) return null; Field f = getOpt_findStaticField(c, field); if (f == null) return null; makeAccessible(f); return f.get(null); } catch (Exception __e) { throw rethrow(__e); } } static Field getOpt_findStaticField(Class c, String field) { Class _c = c; do { for (Field f : _c.getDeclaredFields()) if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) return f; _c = _c.getSuperclass(); } while (_c != null); return null; } static B mapGet2(Map map, A a) { return map == null ? null : map.get(a); } static B mapGet2(A a, Map map) { return map == null ? null : map.get(a); } static Set reflection_classesNotToScan_value = litset( "jdk.internal.loader.URLClassPath" ); static Set reflection_classesNotToScan() { return reflection_classesNotToScan_value; } static String getType(Object o) { return getClassName(o); } static long getFileSize(String path) { return path == null ? 0 : new File(path).length(); } static long getFileSize(File f) { return f == null ? 0 : f.length(); } static ArrayList emptyList() { return new ArrayList(); //ret Collections.emptyList(); } static ArrayList emptyList(int capacity) { return new ArrayList(max(0, capacity)); } // Try to match capacity static ArrayList emptyList(Iterable l) { return l instanceof Collection ? emptyList(((Collection) l).size()) : emptyList(); } static ArrayList emptyList(Object[] l) { return emptyList(l(l)); } // get correct type at once static ArrayList emptyList(Class c) { return new ArrayList(); } static LinkedHashMap asLinkedHashMap(Map map) { if (map instanceof LinkedHashMap) return (LinkedHashMap) map; LinkedHashMap m = new LinkedHashMap(); if (map != null) synchronized(collectionMutex(map)) { m.putAll(map); } return m; } static void setDynObjectValue(DynamicObject o, String field, Object value) { o.fieldValues = syncMapPut2_createLinkedHashMap(o.fieldValues, field, value); } static long psI(String snippetID) { return parseSnippetID(snippetID); } static File localSnippetFile(long snippetID) { return localSnippetsDir(snippetID + ".text"); } static File localSnippetFile(String snippetID) { return localSnippetFile(parseSnippetID(snippetID)); } static A proxy(Class intrface, final Object target) { if (target == null) return null; if (isInstance(intrface, target)) return (A) target; return (A) java.lang.reflect.Proxy.newProxyInstance(intrface.getClassLoader(), new Class[] { intrface }, new InvocationHandler() { public Object invoke(Object proxy, Method method, Object[] args) { return call(target, method.getName(), unnull(args)); } }); } static A proxy(Object target, Class intrface) { return proxy(intrface, target); } static Object vm_generalMap_get(Object key) { return vm_generalMap().get(key); } static String formatSnippetID(String id) { return "#" + parseSnippetID(id); } static String formatSnippetID(long id) { return "#" + id; } static byte[] toUtf8(String s) { try { return s.getBytes("UTF-8"); } catch (Exception __e) { throw rethrow(__e); } } static boolean md5OfFile_verbose = false; static String md5OfFile(String path) { return md5OfFile(newFile(path)); } static String md5OfFile(File f) { try { if (!f.exists()) return "-"; if (md5OfFile_verbose) print("Getting MD5 of " + f); MessageDigest md5 = MessageDigest.getInstance("MD5"); FileInputStream in = new FileInputStream(f); try { byte buf[] = new byte[65536]; int l; while (true) { l = in.read(buf); if (l <= 0) break; md5.update(buf, 0, l); } return bytesToHex(md5.digest()); } finally { _close(in); }} catch (Exception __e) { throw rethrow(__e); } } static String getClassName(Object o) { return o == null ? "null" : o instanceof Class ? ((Class) o).getName() : o.getClass().getName(); } static String standardCredentialsUser() { return trim(loadTextFile( oneOfTheFiles( javaxSecretDir("tinybrain-username"), userDir(".tinybrain/username")))); } static String standardCredentialsPass() { return trim(loadTextFile( oneOfTheFiles( javaxSecretDir("tinybrain-userpass"), userDir(".tinybrain/userpass")))); } static List beginCriticalAction_inFlight = synchroList(); static class CriticalAction { String description; CriticalAction() {} CriticalAction(String description) { this.description = description;} void done() { beginCriticalAction_inFlight.remove(this); } } static CriticalAction beginCriticalAction(String description) { ping(); CriticalAction c = new CriticalAction(description); beginCriticalAction_inFlight.add(c); return c; } static void cleanMeUp_beginCriticalAction() { int n = 0; while (nempty(beginCriticalAction_inFlight)) { int m = l(beginCriticalAction_inFlight); if (m != n) { n = m; try { print("Waiting for " + n2(n, "critical actions") + ": " + join(", ", collect(beginCriticalAction_inFlight, "description"))); } catch (Throwable __e) { _handleException(__e); } } sleepInCleanUp(10); } } public static File mkdirsForFile(File file) { File dir = file.getParentFile(); if (dir != null) { // is null if file is in current dir dir.mkdirs(); if (!dir.isDirectory()) if (dir.isFile()) throw fail("Please delete the file " + f2s(dir) + " - it is supposed to be a directory!"); else throw fail("Unknown IO exception during mkdirs of " + f2s(file)); } return file; } public static String mkdirsForFile(String path) { mkdirsForFile(new File(path)); return path; } static long now_virtualTime; static long now() { return now_virtualTime != 0 ? now_virtualTime : System.currentTimeMillis(); } static File copyFile(File src, File dest) { try { FileInputStream inputStream = new FileInputStream(src.getPath()); FileOutputStream outputStream = newFileOutputStream(dest.getPath()); try { copyStream(inputStream, outputStream); inputStream.close(); } finally { outputStream.close(); } return dest; } catch (Exception __e) { throw rethrow(__e); } } static A printStackTrace(A e) { // we go to system.out now - system.err is nonsense print(getStackTrace(e)); return e; } static void printStackTrace() { printStackTrace(new Throwable()); } static void printStackTrace(String msg) { printStackTrace(new Throwable(msg)); } static void printStackTrace(String msg, Throwable e) { printStackTrace(new Throwable(msg, e)); } static FileOutputStream newFileOutputStream(File path) throws IOException { return newFileOutputStream(path.getPath()); } static FileOutputStream newFileOutputStream(String path) throws IOException { return newFileOutputStream(path, false); } static FileOutputStream newFileOutputStream(File path, boolean append) throws IOException { return newFileOutputStream(path.getPath(), append); } static FileOutputStream newFileOutputStream(String path, boolean append) throws IOException { mkdirsForFile(path); FileOutputStream f = new FileOutputStream(path, append); _registerIO(f, path, true); return f; } static File javaxCachesDir_dir; // can be set to work on different base dir static File javaxCachesDir() { return javaxCachesDir_dir != null ? javaxCachesDir_dir : new File(userHome(), "JavaX-Caches"); } static File javaxCachesDir(String sub) { return newFile(javaxCachesDir(), sub); } static volatile boolean disableCertificateValidation_attempted = false; static void disableCertificateValidation() { try { if (disableCertificateValidation_attempted) return; disableCertificateValidation_attempted = true; print("Disabling certificate validation for whole VM"); try { // Create a trust manager that does not validate certificate chains TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } public void checkClientTrusted(X509Certificate[] certs, String authType) {} public void checkServerTrusted(X509Certificate[] certs, String authType) {} }}; // Ignore differences between given hostname and certificate hostname HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; // Install the all-trusting trust manager SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(hv); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } static boolean networkAllowanceTest(String url) { return isAllowed("networkAllowanceTest", url); } static final boolean loadPageThroughProxy_enabled = false; static String loadPageThroughProxy(String url) { return null; } static String exceptionToStringShort(Throwable e) { lastException(e); e = getInnerException(e); String msg = hideCredentials(unnull(e.getMessage())); if (msg.indexOf("Error") < 0 && msg.indexOf("Exception") < 0) return baseClassName(e) + prependIfNempty(": ", msg); else return msg; } static void sleepSeconds(double s) { if (s > 0) sleep(round(s*1000)); } static A printWithTime(A a) { return printWithTime("", a); } static A printWithTime(String s, A a) { print(hmsWithColons() + ": " + s, a); return a; } static A getAndClearThreadLocal(ThreadLocal tl) { A a = tl.get(); tl.set(null); return a; } static Map vm_generalSubMap(Object name) { synchronized(get(javax(), "generalMap")) { Map map = (Map) (vm_generalMap_get(name)); if (map == null) vm_generalMap_put(name, map = synchroMap()); return map; } } static Thread currentThread() { return Thread.currentThread(); } static InputStream urlConnection_getInputStream(URLConnection con) throws IOException { UnknownHostException lastException = null; for (int _repeat_0 = 0; _repeat_0 < 2; _repeat_0++) { try { if (con instanceof HttpURLConnection) if (((HttpURLConnection) con).getResponseCode() == 500) throw new IOException(joinNemptiesWithColonSpace("Server code 500", tryToReadErrorStreamFromURLConnection(((HttpURLConnection) con)))); return con.getInputStream(); } catch (UnknownHostException e) { lastException = e; print("Retrying because of: " + e); continue; } } throw lastException; } static GZIPInputStream newGZIPInputStream(File f) { return gzInputStream(f); } static GZIPInputStream newGZIPInputStream(InputStream in) { return gzInputStream(in); } static String toHex(byte[] bytes) { return bytesToHex(bytes); } static String toHex(byte[] bytes, int ofs, int len) { return bytesToHex(bytes, ofs, len); } static byte[] utf8(String s) { return toUtf8(s); } static Matcher regexpMatcher(String pat, String s) { return compileRegexp(pat).matcher(unnull(s)); } static A or(A a, A b) { return a != null ? a : b; } static URLConnection openConnection(String url) { try { return openConnection(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } static URLConnection openConnection(URL url) { try { ping(); callOpt(javax(), "recordOpenURLConnection", str(url)); return url.openConnection(); } catch (Exception __e) { throw rethrow(__e); } } static int toInt(Object o) { if (o == null) return 0; if (o instanceof Number) return ((Number) o).intValue(); if (o instanceof String) return parseInt(((String) o)); if (o instanceof Boolean) return boolToInt(((Boolean) o)); throw fail("woot not int: " + getClassName(o)); } static int toInt(long l) { if (l != (int) l) throw fail("Too large for int: " + l); return (int) l; } static URLConnection setURLConnectionDefaultTimeouts(URLConnection con, long timeout) { if (con.getConnectTimeout() == 0) { con.setConnectTimeout(toInt(timeout)); if (con.getConnectTimeout() != timeout) print("Warning: URL connect timeout not set by JDK."); } if (con.getReadTimeout() == 0) { con.setReadTimeout(toInt(timeout)); if (con.getReadTimeout() != timeout) print("Warning: URL read timeout not set by JDK."); } return con; } static Map newDangerousWeakHashMap() { return _registerDangerousWeakMap(synchroMap(new WeakHashMap())); } // initFunction: voidfunc(Map) - is called initially, and after clearing the map static Map newDangerousWeakHashMap(Object initFunction) { return _registerDangerousWeakMap(synchroMap(new WeakHashMap()), initFunction); } static Object callMCWithVarArgs(String method, Object... args) { return call_withVarargs(mc(), method, args); } static boolean call_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) print("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) { Object arg = args[i]; if (!(arg == null ? !types[i].isPrimitive() : isInstanceX(types[i], arg))) { if (debug) print("Bad parameter " + i + ": " + arg + " vs " + types[i]); return false; } } return true; } static int indexOf(List l, A a, int startIndex) { if (l == null) return -1; int n = l(l); for (int i = startIndex; i < n; i++) if (eq(l.get(i), a)) return i; return -1; } static int indexOf(List l, int startIndex, A a) { return indexOf(l, a, startIndex); } static int indexOf(List l, A a) { if (l == null) return -1; return l.indexOf(a); } static int indexOf(String a, String b) { return a == null || b == null ? -1 : a.indexOf(b); } static int indexOf(String a, String b, int i) { return a == null || b == null ? -1 : a.indexOf(b, i); } static int indexOf(String a, char b) { return a == null ? -1 : a.indexOf(b); } static int indexOf(String a, int i, char b) { return indexOf(a, b, i); } static int indexOf(String a, char b, int i) { return a == null ? -1 : a.indexOf(b, i); } static int indexOf(String a, int i, String b) { return a == null || b == null ? -1 : a.indexOf(b, i); } static int indexOf(A[] x, A a) { int n = l(x); for (int i = 0; i < n; i++) if (eq(x[i], a)) return i; return -1; } static void rotateStringBuffer(StringBuffer buf, int max) { try { if (buf == null) return; synchronized(buf) { if (buf.length() <= max) return; try { int newLength = max/2; int ofs = buf.length()-newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } buf.trimToSize(); } } catch (Exception __e) { throw rethrow(__e); } } static void rotateStringBuilder(StringBuilder buf, int max) { try { if (buf == null) return; synchronized(buf) { if (buf.length() <= max) return; try { int newLength = max/2; int ofs = buf.length()-newLength; String newString = buf.substring(ofs); buf.setLength(0); buf.append("[...] ").append(newString); } catch (Exception e) { buf.setLength(0); } buf.trimToSize(); } } catch (Exception __e) { throw rethrow(__e); } } static ThreadLocal assertVerbose_value = new ThreadLocal(); static void assertVerbose(boolean b) { assertVerbose_value.set(b); } static boolean assertVerbose() { return isTrue(assertVerbose_value.get()); } static A assertEqualsVerbose(Object x, A y) { assertEqualsVerbose((String) null, x, y); return y; } static A assertEqualsVerbose(String msg, Object x, A y) { if (!eq(x, y)) { throw fail((msg != null ? msg + ": " : "") + /*sfu*/(y) + " != " + /*sfu*/(x)); } else print("OK: " + /*sfu*/(x)); return y; } static A liftLast(List l) { if (empty(l)) return null; int i = l(l)-1; A a = l.get(i); l.remove(i); return a; } static List liftLast(int n, List l) { int i = l(l)-n; List part = cloneSubList(l, i); removeSubList(l, i); return part; } static String className(Object o) { return getClassName(o); } static boolean nemptyString(String s) { return s != null && s.length() > 0; } static int strL(String s) { return s == null ? 0 : s.length(); } static int listL(Collection l) { return l == null ? 0 : l.size(); } static boolean eqOneOf(Object o, Object... l) { for (Object x : l) if (eq(o, x)) return true; return false; } static byte[] boolArrayToBytes(boolean[] a) { byte[] b = new byte[(l(a)+7)/8]; for (int i = 0; i < l(a); i++) if (a[i]) b[i/8] |= 1 << (i & 7); return b; } static Throwable getExceptionCause(Throwable e) { Throwable c = e.getCause(); return c != null ? c : e; } static String joinWithSpace(Collection c) { return join(" ", c); } static String joinWithSpace(String... c) { return join(" ", c); } static List classNames(Collection l) { return getClassNames(l); } static List classNames(Object[] l) { return getClassNames(Arrays.asList(l)); } static String shortClassName(Object o) { if (o == null) return null; Class c = o instanceof Class ? (Class) o : o.getClass(); String name = c.getName(); return shortenClassName(name); } static int cmp(Number a, Number b) { return a == null ? b == null ? 0 : -1 : cmp(a.doubleValue(), b.doubleValue()); } static int cmp(double a, double b) { return a < b ? -1 : a == b ? 0 : 1; } static int cmp(Object a, Object b) { if (a == null) return b == null ? 0 : -1; if (b == null) return 1; return ((Comparable) a).compareTo(b); } static boolean isDigit(char c) { return Character.isDigit(c); } static String formatSnippetIDOpt(String s) { return isSnippetID(s) ? formatSnippetID(s) : s; } static Class getMainClass() { return mc(); } static Class getMainClass(Object o) { try { if (o == null) return null; if (o instanceof Class && eq(((Class) o).getName(), "x30")) return (Class) o; return (o instanceof Class ? (Class) o : o.getClass()).getClassLoader().loadClass("main"); } catch (Exception __e) { throw rethrow(__e); } } static CloseableIterableIterator emptyCloseableIterableIterator_instance = new CloseableIterableIterator() { public Object next() { throw fail(); } public boolean hasNext() { return false; } }; static CloseableIterableIterator emptyCloseableIterableIterator() { return emptyCloseableIterableIterator_instance; } static boolean ewic(String a, String b) { return endsWithIgnoreCase(a, b); } static CloseableIterableIterator linesFromReader(Reader r) { final BufferedReader br = bufferedReader(r); return iteratorFromFunction_f0_autoCloseable(new F0() { String get() { try { return readLineFromReaderWithClose(br); } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "ret readLineFromReaderWithClose(br);"; }}, _wrapIOCloseable(r)); } static BufferedReader utf8bufferedReader(InputStream in) { try { return bufferedReader(_registerIOWrap(new InputStreamReader(in, "UTF-8"), in)); } catch (Exception __e) { throw rethrow(__e); } } static BufferedReader utf8bufferedReader(File f) { try { return utf8bufferedReader(newFileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } static boolean regionMatchesIC(String a, int offsetA, String b, int offsetB, int len) { return a != null && a.regionMatches(true, offsetA, b, offsetB, len); } static List jsonTok(String s) { List tok = new ArrayList(); int l = l(s); int i = 0; while (i < l) { int j = i; char c; String cc; // scan for whitespace while (j < l) { c = s.charAt(j); cc = s.substring(j, Math.min(j+2, l)); if (c == ' ' || c == '\t' || c == '\r' || c == '\n') ++j; else if (cc.equals("/*")) { do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/")); j = Math.min(j+2, l); } else if (cc.equals("//")) { do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0); } else break; } tok.add(s.substring(i, j)); i = j; if (i >= l) break; c = s.charAt(i); // cc is not needed in rest of loop body // scan for non-whitespace (json strings, "null" identifier, numbers. everything else automatically becomes a one character token.) if (c == '\'' || c == '"') { char opener = c; ++j; while (j < l) { if (s.charAt(j) == opener) { ++j; break; } else if (s.charAt(j) == '\\' && j+1 < l) j += 2; else ++j; } } else if (Character.isLetter(c)) do ++j; while (j < l && Character.isLetter(s.charAt(j))); else if (Character.isDigit(c)) do ++j; while (j < l && Character.isDigit(s.charAt(j))); else ++j; tok.add(s.substring(i, j)); i = j; } if ((tok.size() % 2) == 0) tok.add(""); return tok; } static double parseDouble(String s) { return Double.parseDouble(s); } static long parseLong(String s) { if (empty(s)) return 0; return Long.parseLong(dropSuffix("L", s)); } static long parseLong(Object s) { return Long.parseLong((String) s); } static Class javax() { return getJavaX(); } static Object vmBus_wrapArgs(Object... args) { return empty(args) ? null : l(args) == 1 ? args[0] : args; } static void pcallFAll(Collection l, Object... args) { if (l != null) for (Object f : cloneList(l)) pcallF(f, args); } static void pcallFAll(Iterator it, Object... args) { while (it.hasNext()) pcallF(it.next(), args); } static Set vm_busListeners_live_cache; static Set vm_busListeners_live() { if (vm_busListeners_live_cache == null) vm_busListeners_live_cache = vm_busListeners_live_load(); return vm_busListeners_live_cache; } static Set vm_busListeners_live_load() { return vm_generalIdentityHashSet("busListeners"); } static Map vm_busListenersByMessage_live_cache; static Map vm_busListenersByMessage_live() { if (vm_busListenersByMessage_live_cache == null) vm_busListenersByMessage_live_cache = vm_busListenersByMessage_live_load(); return vm_busListenersByMessage_live_cache; } static Map vm_busListenersByMessage_live_load() { return vm_generalHashMap("busListenersByMessage"); } static String dropSuffix(String suffix, String s) { return s.endsWith(suffix) ? s.substring(0, l(s)-l(suffix)) : s; } static boolean isLetter(char c) { return Character.isLetter(c); } static A last(List l) { return empty(l) ? null : l.get(l.size()-1); } static char last(String s) { return empty(s) ? '#' : s.charAt(l(s)-1); } static int last(int[] a) { return l(a) != 0 ? a[l(a)-1] : 0; } static A last(A[] a) { return l(a) != 0 ? a[l(a)-1] : null; } static A last(Iterator it) { A a = null; while (it.hasNext()) { ping(); a = it.next(); } return a; } static A last(Collection l) { if (l == null) return null; Iterator it = iterator(l); A a = null; while (it.hasNext()) { ping(); a = it.next(); } return a; } static A last(SortedSet l) { return l == null ? null : l.last(); } static String[] drop(int n, String[] a) { n = Math.min(n, a.length); String[] b = new String[a.length-n]; System.arraycopy(a, n, b, 0, b.length); return b; } static Object[] drop(int n, Object[] a) { n = Math.min(n, a.length); Object[] b = new Object[a.length-n]; System.arraycopy(a, n, b, 0, b.length); return b; } static ArrayList toList(A[] a) { return asList(a); } static ArrayList toList(int[] a) { return asList(a); } static ArrayList toList(Set s) { return asList(s); } static ArrayList toList(Iterable s) { return asList(s); } // runnable = Runnable or String (method name) static Thread newThread(Object runnable) { return new Thread(_topLevelErrorHandling(toRunnable(runnable))); } static Thread newThread(Object runnable, String name) { if (name == null) name = defaultThreadName(); return new Thread(_topLevelErrorHandling(toRunnable(runnable)), name); } static Thread newThread(String name, Object runnable) { return newThread(runnable, name); } static Map _registerThread_threads; static Object _onRegisterThread; // voidfunc(Thread) static Thread _registerThread(Thread t) { if (_registerThread_threads == null) _registerThread_threads = newWeakHashMap(); _registerThread_threads.put(t, true); vm_generalWeakSubMap("thread2mc").put(t, weakRef(mc())); callF(_onRegisterThread, t); return t; } static void _registerThread() { _registerThread(Thread.currentThread()); } static File getProgramDir() { return programDir(); } static File getProgramDir(String snippetID) { return programDir(snippetID); } static Map emptyMap() { return new HashMap(); } static Map newWeakHashMap() { return _registerWeakMap(synchroMap(new WeakHashMap())); } // TODO: test if android complains about this static boolean isAWTThread() { if (isAndroid()) return false; if (isHeadless()) return false; return isAWTThread_awt(); } static boolean isAWTThread_awt() { return SwingUtilities.isEventDispatchThread(); } static void failIfUnlicensed() { assertTrue("license off", licensed()); } static B mapGet(Map map, A a) { return map == null || a == null ? null : map.get(a); } static B mapGet(A a, Map map) { return map == null || a == null ? null : map.get(a); } static boolean even(int i) { return (i & 1) == 0; } static boolean even(long i) { return (i & 1) == 0; } static String getComputerID_quick() { return computerID(); } static boolean startsWithOneOf(String s, String... l) { for (String x : l) if (startsWith(s, x)) return true; return false; } static boolean isAGIBlueDomain(String domain) { return domainIsUnder(domain, theAGIBlueDomain()); } static String hostNameFromURL(String url) { try { return new URL(url).getHost(); } catch (Exception __e) { throw rethrow(__e); } } static AutoCloseable tempSetTL(ThreadLocal tl, A a) { return tempSetThreadLocal(tl, a); } static void _close(AutoCloseable c) { if (c != null) try { c.close(); } catch (Throwable e) { // Some classes stupidly throw an exception on double-closing if (c instanceof javax.imageio.stream.ImageOutputStream) return; else throw rethrow(e); } } static List synchroList() { return Collections.synchronizedList(new ArrayList()); } static List synchroList(List l) { return Collections.synchronizedList(l); } static List ll(A... a) { ArrayList l = new ArrayList(a.length); if (a != null) for (A x : a) l.add(x); return l; } static void setOptIfNotNull(Object o, String field, Object value) { if (value != null) setOpt(o, field, value); } static Class mc() { return main.class; } static Object mainBot; static Object getMainBot() { return mainBot; } static final Map callOpt_cache = newDangerousWeakHashMap(); static Object callOpt_cached(Object o, String methodName, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); // TODO: (super-rare) case where method exists static and non-static // with different args Method me = cache.findMethod(methodName, args); if (me == null || (me.getModifiers() & Modifier.STATIC) == 0) return null; return invokeMethod(me, null, args); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(methodName, args); if (me == null) return null; return invokeMethod(me, o, args); } } catch (Exception __e) { throw rethrow(__e); } } static _MethodCache callOpt_getCache(Class c) { synchronized(callOpt_cache) { _MethodCache cache = callOpt_cache.get(c); if (cache == null) callOpt_cache.put(c, cache = new _MethodCache(c)); return cache; } } static boolean isStaticMethod(Method m) { return methodIsStatic(m); } static Object[] massageArgsForVarArgsCall(Method m, Object[] args) { Class[] types = m.getParameterTypes(); int n = types.length-1, nArgs = args.length; if (nArgs < n) return null; for (int i = 0; i < n; i++) if (!argumentCompatibleWithType(args[i], types[i])) return null; Class varArgType = types[n].getComponentType(); for (int i = n; i < nArgs; i++) if (!argumentCompatibleWithType(args[i], varArgType)) return null; Object[] newArgs = new Object[n+1]; arraycopy(args, 0, newArgs, 0, n); Object[] varArgs = arrayOfType(varArgType, nArgs-n); arraycopy(args, n, varArgs, 0, nArgs-n); newArgs[n] = varArgs; return newArgs; } static String joinWithComma(Collection c) { return join(", ", c); } static String joinWithComma(String... c) { return join(", ", c); } static String joinWithComma(Pair p) { return p == null ? "" : joinWithComma(str(p.a), str(p.b)); } static HashMap> callMC_cache = new HashMap(); static String callMC_key; static Method callMC_value; // varargs assignment fixer for a single string array argument static Object callMC(String method, String[] arg) { return callMC(method, new Object[] {arg}); } static Object callMC(String method, Object... args) { try { Method me; if (callMC_cache == null) callMC_cache = new HashMap(); // initializer time workaround synchronized(callMC_cache) { me = method == callMC_key ? callMC_value : null; } if (me != null) try { return invokeMethod(me, null, args); } catch (IllegalArgumentException e) { throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e); } List m; synchronized(callMC_cache) { m = callMC_cache.get(method); } if (m == null) { if (callMC_cache.isEmpty()) { callMC_makeCache(); m = callMC_cache.get(method); } if (m == null) throw fail("Method named " + method + " not found in main"); } int n = m.size(); if (n == 1) { me = m.get(0); synchronized(callMC_cache) { callMC_key = method; callMC_value = me; } try { return invokeMethod(me, null, args); } catch (IllegalArgumentException e) { throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e); } } for (int i = 0; i < n; i++) { me = m.get(i); if (call_checkArgs(me, args, false)) return invokeMethod(me, null, args); } throw fail("No method called " + method + " with arguments (" + joinWithComma(getClasses(args)) + ") found in main"); } catch (Exception __e) { throw rethrow(__e); } } static void callMC_makeCache() { synchronized(callMC_cache) { callMC_cache.clear(); Class _c = (Class) mc(), c = _c; while (c != null) { for (Method m : c.getDeclaredMethods()) if ((m.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) { makeAccessible(m); multiMapPut(callMC_cache, m.getName(), m); } c = c.getSuperclass(); } } } static Pair pair(A a, B b) { return new Pair(a, b); } static Pair pair(A a) { return new Pair(a, a); } static A assertNotNull(A a) { assertTrue(a != null); return a; } static A assertNotNull(String msg, A a) { assertTrue(msg, a != null); return a; } static Map synchroHashMap() { return Collections.synchronizedMap(new HashMap()); } static HashSet litset(A... items) { return lithashset(items); } static int max(int a, int b) { return Math.max(a, b); } static int max(int a, int b, int c) { return max(max(a, b), c); } static long max(int a, long b) { return Math.max((long) a, b); } static long max(long a, long b) { return Math.max(a, b); } static double max(int a, double b) { return Math.max((double) a, b); } static float max(float a, float b) { return Math.max(a, b); } static double max(double a, double b) { return Math.max(a, b); } static int max(Collection c) { int x = Integer.MIN_VALUE; for (int i : c) x = max(x, i); return x; } static double max(double[] c) { if (c.length == 0) return Double.MIN_VALUE; double x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } static float max(float[] c) { if (c.length == 0) return Float.MAX_VALUE; float x = c[0]; for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]); return x; } static byte max(byte[] c) { byte x = -128; for (byte d : c) if (d > x) x = d; return x; } static short max(short[] c) { short x = -0x8000; for (short d : c) if (d > x) x = d; return x; } static int max(int[] c) { int x = Integer.MIN_VALUE; for (int d : c) if (d > x) x = d; return x; } static Object collectionMutex(List l) { return l; } static Object collectionMutex(Object o) { if (o instanceof List) return o; String c = className(o); if (eq(c, "java.util.TreeMap$KeySet")) c = className(o = getOpt(o, "m")); else if (eq(c, "java.util.HashMap$KeySet")) c = className(o = get_raw(o, "this$0")); if (eqOneOf(c, "java.util.TreeMap$AscendingSubMap", "java.util.TreeMap$DescendingSubMap")) c = className(o = get_raw(o, "m")); return o; } static LinkedHashMap syncMapPut2_createLinkedHashMap(LinkedHashMap map, A key, B value) { if (key != null) if (value != null) { if (map == null) map = new LinkedHashMap(); synchronized(collectionMutex(map)) { map.put(key, value); } } else if (map != null) synchronized(collectionMutex(map)) { map.remove(key); } return map; } static File localSnippetsDir() { return javaxDataDir("Personal Programs"); } static File localSnippetsDir(String sub) { return newFile(localSnippetsDir(), sub); } static boolean isInstance(Class type, Object arg) { return type.isInstance(arg); } static Map vm_generalMap_map; static Map vm_generalMap() { if (vm_generalMap_map == null) vm_generalMap_map = (Map) get(javax(), "generalMap"); return vm_generalMap_map; } static File oneOfTheFiles(String... paths) { if (paths != null) for (String path : paths) if (fileExists(path)) return newFile(path); return null; } static File oneOfTheFiles(File... files) { if (files != null) for (File f : files) if (fileExists(f)) return f; return null; } static File javaxSecretDir_dir; // can be set to work on different base dir static File javaxSecretDir() { return javaxSecretDir_dir != null ? javaxSecretDir_dir : new File(userHome(), "JavaX-Secret"); } static File javaxSecretDir(String sub) { return newFile(javaxSecretDir(), sub); } static String n2(long l) { return formatWithThousands(l); } static String n2(Collection l) { return n2(l(l)); } static String n2(double l, String singular) { return n2(l, singular, singular + "s"); } static String n2(double l, String singular, String plural) { if (fraction(l) == 0) return n2((long) l, singular, plural); else return l + " " + plural; } static String n2(long l, String singular, String plural) { return n_fancy2(l, singular, plural); } static String n2(long l, String singular) { return n_fancy2(l, singular, singular + "s"); } static String n2(Collection l, String singular) { return n2(l(l), singular); } static String n2(Collection l, String singular, String plural) { return n_fancy2(l, singular, plural); } static String n2(Map m, String singular, String plural) { return n_fancy2(m, singular, plural); } static String n2(Map m, String singular) { return n2(l(m), singular); } static String n2(Object[] a, String singular) { return n2(l(a), singular); } static String n2(Object[] a, String singular, String plural) { return n_fancy2(a, singular, plural); } static List collect(Iterable c, String field) { return collectField(c, field); } static List collect(String field, Iterable c) { return collectField(c, field); } /*ifclass Concept static L collect(Class c, S field) { ret collect(list(c), field); } endif TODO: make translator ignore stuff in ifclass until resolved */ static volatile PersistableThrowable _handleException_lastException; static List _handleException_onException = synchroList(ll("printStackTrace2")); static void _handleException(Throwable e) { _handleException_lastException = persistableThrowable(e); Throwable e2 = innerException(e); if (e2.getClass() == RuntimeException.class && eq(e2.getMessage(), "Thread cancelled.") || e2 instanceof InterruptedException) return; for (Object f : cloneList(_handleException_onException)) try { callF(f, e); } catch (Throwable e3) { printStackTrace2(e3); // not using pcall here - it could lead to endless loops } } static void sleepInCleanUp(long ms) { try { if (ms < 0) return; Thread.sleep(ms); } catch (Exception __e) { throw rethrow(__e); } } static String f2s(File f) { return f == null ? null : f.getAbsolutePath(); } static String f2s(String s) { return f2s(newFile(s)); } static String f2s(java.nio.file.Path p) { return p == null ? null : f2s(p.toFile()); } static void copyStream(InputStream in, OutputStream out) { try { byte[] buf = new byte[65536]; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); } } catch (Exception __e) { throw rethrow(__e); } } static String getStackTrace(Throwable throwable) { lastException(throwable); return getStackTrace_noRecord(throwable); } static String getStackTrace_noRecord(Throwable throwable) { StringWriter writer = new StringWriter(); throwable.printStackTrace(new PrintWriter(writer)); return hideCredentials(writer.toString()); } static String getStackTrace() { return getStackTrace_noRecord(new Throwable()); } static void _registerIO(Object object, String path, boolean opened) { } static volatile Object isAllowed_function; // func(S, O[]) -> bool static volatile boolean isAllowed_all = true; static boolean isAllowed(String askingMethod, Object... args) { // check on VM level Object f = vm_generalMap_get("isAllowed_function"); if (f != null && !isTrue(callF(f, askingMethod, args))) return false; // check locally return isAllowed_all || isTrue(callF(isAllowed_function, askingMethod, args)); } // PersistableThrowable doesn't hold GC-disturbing class references in backtrace static volatile PersistableThrowable lastException_lastException; static PersistableThrowable lastException() { return lastException_lastException; } static void lastException(Throwable e) { lastException_lastException = persistableThrowable(e); } static Throwable getInnerException(Throwable e) { if (e == null) return null; while (e.getCause() != null) e = e.getCause(); return e; } static Throwable getInnerException(Runnable r) { return getInnerException(getException(r)); } static String baseClassName(String className) { return substring(className, className.lastIndexOf('.')+1); } static String baseClassName(Object o) { return baseClassName(getClassName(o)); } static String prependIfNempty(String prefix, String s) { return empty(s) ? s : prefix + s; } static volatile boolean sleep_noSleep = false; static void sleep(long ms) { ping(); if (ms < 0) return; // allow spin locks if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread"); try { Thread.sleep(ms); } catch (Exception e) { throw new RuntimeException(e); } } static void sleep() { try { if (sleep_noSleep) throw fail("nosleep"); print("Sleeping."); sleepQuietly(); } catch (Exception __e) { throw rethrow(__e); } } static long round(double d) { return Math.round(d); } static String hmsWithColons() { return hmsWithColons(now()); } static String hmsWithColons(long time) { return new SimpleDateFormat("HH:mm:ss").format(time); } static Object vm_generalMap_put(Object key, Object value) { return mapPutOrRemove(vm_generalMap(), key, value); } static String joinNemptiesWithColonSpace(String... strings) { return joinNempties(": ", strings); } static String joinNemptiesWithColonSpace(Collection strings) { return joinNempties(": ", strings); } static String tryToReadErrorStreamFromURLConnection(URLConnection conn) { try { if (conn instanceof HttpURLConnection) return stream2string(((HttpURLConnection) conn).getErrorStream()); // TODO: ensure some max length return null; } catch (Throwable __e) { return null; } } static int gzInputStream_defaultBufferSize = 65536; static GZIPInputStream gzInputStream(File f) { try { return gzInputStream(new FileInputStream(f)); } catch (Exception __e) { throw rethrow(__e); } } static GZIPInputStream gzInputStream(File f, int bufferSize) { try { return gzInputStream(new FileInputStream(f), bufferSize); } catch (Exception __e) { throw rethrow(__e); } } static GZIPInputStream gzInputStream(InputStream in) { return gzInputStream(in, gzInputStream_defaultBufferSize); } static GZIPInputStream gzInputStream(InputStream in, int bufferSize) { try { return _registerIOWrap(new GZIPInputStream(in, gzInputStream_defaultBufferSize), in); } catch (Exception __e) { throw rethrow(__e); } } static Map compileRegexp_cache = syncMRUCache(10); static java.util.regex.Pattern compileRegexp(String pat) { java.util.regex.Pattern p = compileRegexp_cache.get(pat); if (p == null) { compileRegexp_cache.put(pat, p = java.util.regex.Pattern.compile(pat)); } return p; } static Object callOpt(Object o) { return callF(o); } static A callOpt(Object o, String method, Object... args) { return (A) callOpt_withVarargs(o, method, args); } static int parseInt(String s) { return emptyString(s) ? 0 : Integer.parseInt(s); } static int parseInt(char c) { return Integer.parseInt(str(c)); } static int boolToInt(boolean b) { return b ? 1 : 0; } static boolean isInstanceX(Class type, Object arg) { if (type == boolean.class) return arg instanceof Boolean; if (type == int.class) return arg instanceof Integer; if (type == long.class) return arg instanceof Long; if (type == float.class) return arg instanceof Float; if (type == short.class) return arg instanceof Short; if (type == char.class) return arg instanceof Character; if (type == byte.class) return arg instanceof Byte; if (type == double.class) return arg instanceof Double; return type.isInstance(arg); } static List cloneSubList(List l, int startIndex, int endIndex) { return newSubList(l, startIndex, endIndex); } static List cloneSubList(List l, int startIndex) { return newSubList(l, startIndex); } static void removeSubList(List l, int from, int to) { if (l != null) subList(l, from, to).clear(); } static void removeSubList(List l, int from) { if (l != null) subList(l, from).clear(); } static List getClassNames(Collection l) { List out = new ArrayList(); if (l != null) for (Object o : l) out.add(o == null ? null : getClassName(o)); return out; } static String shortenClassName(String name) { if (name == null) return null; int i = lastIndexOf(name, "$"); if (i < 0) i = lastIndexOf(name, "."); return i < 0 ? name : substring(name, i+1); } static boolean endsWithIgnoreCase(String a, String b) { int la = l(a), lb = l(b); return la >= lb && regionMatchesIC(a, la-lb, b, 0, lb); } static BufferedReader bufferedReader(Reader r) { return r instanceof BufferedReader ? (BufferedReader) r : _registerIOWrap(new BufferedReader(r), r); } static CloseableIterableIterator iteratorFromFunction_f0_autoCloseable(final F0 f, final AutoCloseable closeable) { class IFF2 extends CloseableIterableIterator { A a; boolean done = false; public boolean hasNext() { getNext(); return !done; } public A next() { getNext(); if (done) throw fail(); A _a = a; a = null; return _a; } void getNext() { if (done || a != null) return; a = f.get(); done = a == null; } public void close() throws Exception { if (closeable != null) closeable.close(); } }; return new IFF2(); } static String readLineFromReaderWithClose(BufferedReader r) { try { String s = r.readLine(); if (s == null) r.close(); return s; } catch (Exception __e) { throw rethrow(__e); } } static AutoCloseable _wrapIOCloseable(final AutoCloseable c) { return c == null ? null : new AutoCloseable() { public String toString() { return "c.close();\r\n _registerIO(c, null, false);"; } public void close() throws Exception { c.close(); _registerIO(c, null, false); }}; } static A _registerIOWrap(A wrapper, Object wrapped) { return wrapper; } static FileInputStream newFileInputStream(File path) throws IOException { return newFileInputStream(path.getPath()); } static FileInputStream newFileInputStream(String path) throws IOException { FileInputStream f = new FileInputStream(path); _registerIO(f, path, true); return f; } static ArrayList cloneList(Iterable l) { return l instanceof Collection ? cloneList((Collection) l) : asList(l); } static ArrayList cloneList(Collection l) { if (l == null) return new ArrayList(); synchronized(collectionMutex(l)) { return new ArrayList(l); } } static Object pcallF(Object f, Object... args) { return pcallFunction(f, args); } static A pcallF(F0 f) { try { return f == null ? null : f.get(); } catch (Throwable __e) { return null; } } static void pcallF(VF1 f, A a) { try { if (f != null) f.get(a); } catch (Throwable __e) { _handleException(__e); } } static Set vm_generalIdentityHashSet(Object name) { synchronized(get(javax(), "generalMap")) { Set set = (Set) (vm_generalMap_get(name)); if (set == null) vm_generalMap_put(name, set = syncIdentityHashSet()); return set; } } static Map vm_generalHashMap(Object name) { synchronized(get(javax(), "generalMap")) { Map m = (Map) (vm_generalMap_get(name)); if (m == null) vm_generalMap_put(name, m = syncHashMap()); return m; } } static Iterator iterator(Iterable c) { return c == null ? emptyIterator() : c.iterator(); } static Runnable _topLevelErrorHandling(final Runnable runnable) { final Object info = _threadInfo(); return new Runnable() { public void run() { try { try { _threadInheritInfo(info); runnable.run(); } catch (Throwable __e) { _handleException(__e); } } catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "pcall {\r\n _threadInheritInfo(info);\r\n runnable.run();\r\n }"; }}; } static Map vm_generalWeakSubMap(Object name) { synchronized(get(javax(), "generalMap")) { Map map = (Map) (vm_generalMap_get(name)); if (map == null) vm_generalMap_put(name, map = newWeakMap()); return map; } } static WeakReference weakRef(A a) { return newWeakReference(a); } static File programDir_mine; // set this to relocate program's data static File programDir() { return programDir(getProgramID()); } static File programDir(String snippetID) { boolean me = sameSnippetID(snippetID, programID()); if (programDir_mine != null && me) return programDir_mine; File dir = new File(javaxDataDir(), formatSnippetIDOpt(snippetID)); if (me) { String c = caseID(); if (nempty(c)) dir = newFile(dir, c); } return dir; } static File programDir(String snippetID, String subPath) { return new File(programDir(snippetID), subPath); } static List _registerWeakMap_preList; static A _registerWeakMap(A map) { if (javax() == null) { // We're in class init if (_registerWeakMap_preList == null) _registerWeakMap_preList = synchroList(); _registerWeakMap_preList.add(map); return map; } try { call(javax(), "_registerWeakMap", map); } catch (Throwable e) { printException(e); print("Upgrade JavaX!!"); } return map; } static void _onLoad_registerWeakMap() { assertNotNull(javax()); if (_registerWeakMap_preList == null) return; for (Object o : _registerWeakMap_preList) _registerWeakMap(o); _registerWeakMap_preList = null; } static Boolean isHeadless_cache; static boolean isHeadless() { if (isHeadless_cache != null) return isHeadless_cache; if (isAndroid()) return isHeadless_cache = true; if (GraphicsEnvironment.isHeadless()) return isHeadless_cache = true; // Also check if AWT actually works. // If DISPLAY variable is set but no X server up, this will notice. try { SwingUtilities.isEventDispatchThread(); return isHeadless_cache = false; } catch (Throwable e) { return isHeadless_cache = true; } } static void assertTrue(Object o) { if (!(eq(o, true) /*|| isTrue(pcallF(o))*/)) throw fail(str(o)); } static boolean assertTrue(String msg, boolean b) { if (!b) throw fail(msg); return b; } static boolean assertTrue(boolean b) { if (!b) throw fail("oops"); return b; } static volatile boolean licensed_yes = true; static boolean licensed() { if (!licensed_yes) return false; ping_okInCleanUp(); return true; } static void licensed_off() { licensed_yes = false; } static String _computerID; static Lock computerID_lock = lock(); public static String computerID() { if (_computerID == null) { Lock __0 = computerID_lock; lock(__0); try { if (_computerID != null) return _computerID; File file = computerIDFile(); _computerID = loadTextFile(file.getPath()); if (_computerID == null) { // legacy load _computerID = loadTextFile(userDir(".tinybrain/computer-id")); if (_computerID == null) _computerID = makeRandomID(12, new SecureRandom()); saveTextFile(file, _computerID); } } finally { unlock(__0); } } return _computerID; } static boolean domainIsUnder(String domain, String mainDomain) { return eqic(domain, mainDomain) || ewic(domain, "." + mainDomain); } static String theAGIBlueDomain() { return "agi.blue"; } static AutoCloseable tempSetThreadLocal(final ThreadLocal tl, A a) { if (tl == null) return null; final A prev = setThreadLocal(tl, a); return new AutoCloseable() { public String toString() { return "tl.set(prev);"; } public void close() throws Exception { tl.set(prev); }}; } static boolean methodIsStatic(Method m) { return (m.getModifiers() & Modifier.STATIC) != 0; } static boolean argumentCompatibleWithType(Object arg, Class type) { return arg == null ? !type.isPrimitive() : isInstanceX(type, arg); } static void arraycopy(Object[] a, Object[] b) { if (a != null && b != null) arraycopy(a, 0, b, 0, min(a.length, b.length)); } static void arraycopy(Object src, int srcPos, Object dest, int destPos, int n) { if (n != 0) System.arraycopy(src, srcPos, dest, destPos, n); } static A[] arrayOfType(Class type, int n) { return makeArray(type, n); } static A[] arrayOfType(int n, Class type) { return arrayOfType(type, n); } static Throwable printStackTrace2(Throwable e) { // we go to system.out now - system.err is nonsense print(getStackTrace2(e)); return e; } static void printStackTrace2() { printStackTrace2(new Throwable()); } static void printStackTrace2(String msg) { printStackTrace2(new Throwable(msg)); } static List getClasses(Object[] array) { List l = emptyList(l(array)); for (Object o : array) l.add(_getClass(o)); return l; } static void multiMapPut(Map> map, A a, B b) { List l = map.get(a); if (l == null) map.put(a, l = new ArrayList()); l.add(b); } static HashSet lithashset(A... items) { HashSet set = new HashSet(); for (A a : items) set.add(a); return set; } static File javaxDataDir_dir; // can be set to work on different base dir static File javaxDataDir() { return javaxDataDir_dir != null ? javaxDataDir_dir : new File(userHome(), "JavaX-Data"); } static File javaxDataDir(String... subs) { return newFile(javaxDataDir(), subs); } static boolean fileExists(String path) { return path != null && new File(path).exists(); } static boolean fileExists(File f) { return f != null && f.exists(); } static String formatWithThousands(long l) { return formatWithThousandsSeparator(l); } static double fraction(double d) { return d % 1; } static String n_fancy2(long l, String singular, String plural) { return formatWithThousandsSeparator(l) + " " + trim(l == 1 ? singular : plural); } static String n_fancy2(Collection l, String singular, String plural) { return n_fancy2(l(l), singular, plural); } static String n_fancy2(Map m, String singular, String plural) { return n_fancy2(l(m), singular, plural); } static String n_fancy2(Object[] a, String singular, String plural) { return n_fancy2(l(a), singular, plural); } static List collectField(Iterable c, String field) { List l = new ArrayList(); if (c != null) for (Object a : c) l.add(getOpt(a, field)); return l; } static List collectField(String field, Iterable c) { return collectField(c, field); } static PersistableThrowable persistableThrowable(Throwable e) { return e == null ? null : new PersistableThrowable(e); } static Throwable innerException(Throwable e) { return getInnerException(e); } static Throwable getException(Runnable r) { try { callF(r); return null; } catch (Throwable e) { return e; } } static Object sleepQuietly_monitor = new Object(); static void sleepQuietly() { try { assertFalse(isAWTThread()); synchronized(sleepQuietly_monitor) { sleepQuietly_monitor.wait(); } } catch (Exception __e) { throw rethrow(__e); } } static B mapPutOrRemove(Map map, A key, B value) { if (map != null && key != null) if (value != null) return map.put(key, value); else return map.remove(key); return null; } static String joinNempties(String sep, String... strings) { return joinStrings(sep, strings); } static String joinNempties(String sep, Collection strings) { return joinStrings(sep, strings); } static String stream2string(InputStream in) { return utf8streamToString(in); } static Map syncMRUCache(int size) { return synchroMap(new MRUCache(size)); } static Object callOpt_withVarargs(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { Class c = (Class) o; _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me == null) { // TODO: varargs return null; } if ((me.getModifiers() & Modifier.STATIC) == 0) return null; return invokeMethod(me, null, args); } else { Class c = o.getClass(); _MethodCache cache = callOpt_getCache(c); Method me = cache.findMethod(method, args); if (me != null) return invokeMethod(me, o, args); // try varargs List methods = cache.cache.get(method); if (methods != null) methodSearch: for (Method m : methods) { { if (!(m.isVarArgs())) continue; } Object[] newArgs = massageArgsForVarArgsCall(m, args); if (newArgs != null) return invokeMethod(m, o, newArgs); } return null; } } catch (Exception __e) { throw rethrow(__e); } } static boolean emptyString(String s) { return s == null || s.length() == 0; } static List newSubList(List l, int startIndex, int endIndex) { return cloneList(subList(l, startIndex, endIndex)); } static List newSubList(List l, int startIndex) { return cloneList(subList(l, startIndex)); } static List subList(List l, int startIndex) { return subList(l, startIndex, l(l)); } static List subList(int startIndex, int endIndex, List l) { return subList(l, startIndex, endIndex); } static List subList(List l, int startIndex, int endIndex) { if (l == null) return null; int n = l(l); startIndex = Math.max(0, startIndex); endIndex = Math.min(n, endIndex); if (startIndex >= endIndex) return ll(); if (startIndex == 0 && endIndex == n) return l; return l.subList(startIndex, endIndex); } static int lastIndexOf(String a, String b) { return a == null || b == null ? -1 : a.lastIndexOf(b); } static int lastIndexOf(String a, char b) { return a == null ? -1 : a.lastIndexOf(b); } static Object pcallFunction(Object f, Object... args) { try { return callFunction(f, args); } catch (Throwable __e) { _handleException(__e); } return null; } static Set syncIdentityHashSet() { return (Set) synchronizedSet(identityHashSet()); } static Map syncHashMap() { return synchroHashMap(); } static List> _threadInfo_makers = synchroList(); static Object _threadInfo() { if (empty(_threadInfo_makers)) return null; HashMap map = new HashMap(); pcallFAll(_threadInfo_makers, map); return map; } static List> _threadInheritInfo_retrievers = synchroList(); static void _threadInheritInfo(Object info) { if (info == null) return; pcallFAll(_threadInheritInfo_retrievers, (Map) info); } static Map newWeakMap() { return newWeakHashMap(); } static WeakReference newWeakReference(A a) { return a == null ? null : new WeakReference(a); } static boolean sameSnippetID(String a, String b) { if (!isSnippetID(a) || !isSnippetID(b)) return false; return parseSnippetID(a) == parseSnippetID(b); } static volatile String caseID_caseID; static String caseID() { return caseID_caseID; } static void caseID(String id) { caseID_caseID = id; } static A printException(A e) { printStackTrace(e); return e; } static void lock(Lock lock) { try { ping(); if (lock == null) return; try { lock.lockInterruptibly(); } catch (InterruptedException e) { print("Locking interrupted! I probably deadlocked, oops."); printStackTrace(e); rethrow(e); } ping(); } catch (Exception __e) { throw rethrow(__e); } } static void lock(Lock lock, String msg) { print("Locking: " + msg); lock(lock); } static void lock(Lock lock, String msg, long timeout) { print("Locking: " + msg); lockOrFail(lock, timeout); } static ReentrantLock lock() { return fairLock(); } static File computerIDFile() { return javaxDataDir("Basic Info/computer-id.txt"); } static String makeRandomID(int length) { return makeRandomID(length, defaultRandomGenerator()); } static String makeRandomID(int length, Random random) { char[] id = new char[length]; for (int i = 0; i < id.length; i++) id[i] = (char) ((int) 'a' + random.nextInt(26)); return new String(id); } static String makeRandomID(Random r, int length) { return makeRandomID(length, r); } static void unlock(Lock lock, String msg) { if (lock == null) return; print("Unlocking: " + msg); lock.unlock(); } static void unlock(Lock lock) { if (lock == null) return; lock.unlock(); } static A[] makeArray(Class type, int n) { return (A[]) Array.newInstance(type, n); } static String getStackTrace2(Throwable e) { return hideCredentials(getStackTrace(unwrapTrivialExceptionWraps(e)) + replacePrefix("java.lang.RuntimeException: ", "FAIL: ", hideCredentials(str(innerException2(e)))) + "\n"); } static String formatWithThousandsSeparator(long l) { return NumberFormat.getInstance(new Locale("en_US")).format(l); } static String joinStrings(String sep, String... strings) { return joinStrings(sep, Arrays.asList(strings)); } static String joinStrings(String sep, Collection strings) { StringBuilder buf = new StringBuilder(); for (String s : unnull(strings)) if (nempty(s)) { if (nempty(buf)) buf.append(sep); buf.append(s); } return str(buf); } static String utf8streamToString(InputStream in) { return readerToString(utf8bufferedReader(in)); } static Object callFunction(Object f, Object... args) { return callF(f, args); } static Set synchronizedSet() { return synchroHashSet(); } static Set synchronizedSet(Set set) { return Collections.synchronizedSet(set); } static Set identityHashSet() { return Collections.newSetFromMap(new IdentityHashMap()); } static void lockOrFail(Lock lock, long timeout) { try { ping(); if (!lock.tryLock(timeout, TimeUnit.MILLISECONDS)) { String s = "Couldn't acquire lock after " + timeout + " ms."; if (lock instanceof ReentrantLock) { ReentrantLock l = (ReentrantLock) lock; s += " Hold count: " + l.getHoldCount() + ", owner: " + call(l, "getOwner"); } throw fail(s); } ping(); } catch (Exception __e) { throw rethrow(__e); } } static ReentrantLock fairLock() { return new ReentrantLock(true); } static Random defaultRandomGenerator() { return ThreadLocalRandom.current(); } static Throwable unwrapTrivialExceptionWraps(Throwable e) { if (e == null) return e; while (e.getClass() == RuntimeException.class && e.getCause() != null && eq(e.getMessage(), str(e.getCause()))) e = e.getCause(); return e; } static String replacePrefix(String prefix, String replacement, String s) { if (!startsWith(s, prefix)) return s; return replacement + substring(s, l(prefix)); } static Throwable innerException2(Throwable e) { if (e == null) return null; while (empty(e.getMessage()) && e.getCause() != null) e = e.getCause(); return e; } static String readerToString(Reader r) { try { try { StringBuilder buf = new StringBuilder(); int n = 0; while (true) { int ch = r.read(); if (ch < 0) break; buf.append((char) ch); ++n; //if ((n % loadPage_verboseness) == 0) print(" " + n + " chars read"); } return buf.toString(); } finally { r.close(); } } catch (Exception __e) { throw rethrow(__e); } } static Set synchroHashSet() { return Collections.synchronizedSet(new HashSet()); } // immutable, has strong refs final static class _MethodCache { final Class c; final HashMap> cache = new HashMap(); _MethodCache(Class c) { this.c = c; _init(); } void _init() { Class _c = c; while (_c != null) { for (Method m : _c.getDeclaredMethods()) if (!reflection_isForbiddenMethod(m)) multiMapPut(cache, m.getName(), makeAccessible(m)); _c = _c.getSuperclass(); } // add default methods - this might lead to a duplication // because the overridden method is also added, but it's not // a problem except for minimal performance loss. for (Class intf : allInterfacesImplementedBy(c)) for (Method m : intf.getDeclaredMethods()) if (m.isDefault() && !reflection_isForbiddenMethod(m)) multiMapPut(cache, m.getName(), makeAccessible(m)); } // Returns only matching methods Method findMethod(String method, Object[] args) { try { List m = cache.get(method); if (m == null) return null; int n = m.size(); for (int i = 0; i < n; i++) { Method me = m.get(i); if (call_checkArgs(me, args, false)) return me; } return null; } catch (Exception __e) { throw rethrow(__e); } } Method findStaticMethod(String method, Object[] args) { try { List m = cache.get(method); if (m == null) return null; int n = m.size(); for (int i = 0; i < n; i++) { Method me = m.get(i); if (isStaticMethod(me) && call_checkArgs(me, args, false)) return me; } return null; } catch (Exception __e) { throw rethrow(__e); } } }static abstract class VF1 implements IVF1 { public abstract void get(A a); }// elements are put to front when added (not when accessed) static class MRUCache extends LinkedHashMap { int maxSize = 10; MRUCache() {} MRUCache(int maxSize) { this.maxSize = maxSize;} protected boolean removeEldestEntry(Map.Entry eldest) { return size() > maxSize; } Object _serialize() { return ll(maxSize, cloneLinkedHashMap(this)); } static MRUCache _deserialize(List l) { MRUCache m = new MRUCache(); m.maxSize = (int) first(l); m.putAll((LinkedHashMap) second(l)); return m; } }static abstract class CloseableIterableIterator extends IterableIterator implements AutoCloseable { public void close() throws Exception {} }static ThreadLocal DynamicObject_loading = new ThreadLocal(); static class DynamicObject { String className; // just the name, without the "main$" LinkedHashMap fieldValues; DynamicObject() {} // className = just the name, without the "main$" DynamicObject(String className) { this.className = className;} Map _map() { return fieldValues; } public String toString() { return getClass() == DynamicObject.class ? "dyn " + className : super.toString(); } }static interface IResourceLoader { String loadSnippet(String snippetID); String getTranspiled(String snippetID); // with libs int getSnippetType(String snippetID); String getSnippetTitle(String snippetID); File loadLibrary(String snippetID); File pathToJavaXJar(); File getSnippetJar(String snippetID, String transpiledSrc); }static class PersistableThrowable { String className; String msg; String stacktrace; PersistableThrowable() {} PersistableThrowable(Throwable e) { if (e == null) className = "Crazy Null Error"; else { className = getClassName(e).replace('/', '.'); msg = e.getMessage(); stacktrace = getStackTrace_noRecord(e); } } public String toString() { return nempty(msg) ? className + ": " + msg : className; } }static abstract class F0 { abstract A get(); }// you still need to implement hasNext() and next() static abstract class IterableIterator implements Iterator, Iterable { public Iterator iterator() { return this; } public void remove() { unsupportedOperation(); } }static class Pair implements Comparable> { A a; B b; Pair() {} Pair(A a, B b) { this.b = b; this.a = a;} public int hashCode() { return hashCodeFor(a) + 2*hashCodeFor(b); } public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Pair)) return false; Pair t = (Pair) o; return eq(a, t.a) && eq(b, t.b); } public String toString() { return "<" + a + ", " + b + ">"; } public int compareTo(Pair p) { if (p == null) return 1; int i = ((Comparable) a).compareTo(p.a); if (i != 0) return i; return ((Comparable) b).compareTo(p.b); } } static interface IVF1 { void get(A a); } static boolean reflection_isForbiddenMethod(Method m) { return m.getDeclaringClass() == Object.class && eqOneOf(m.getName(), "finalize", "clone", "registerNatives"); } static Set allInterfacesImplementedBy(Class c) { if (c == null) return null; HashSet set = new HashSet(); allInterfacesImplementedBy_find(c, set); return set; } static void allInterfacesImplementedBy_find(Class c, Set set) { if (c.isInterface() && !set.add(c)) return; do { for (Class intf : c.getInterfaces()) allInterfacesImplementedBy_find(intf, set); } while ((c = c.getSuperclass()) != null); } static Method findMethod(Object o, String method, Object... args) { return findMethod_cached(o, method, args); } static boolean findMethod_checkArgs(Method m, Object[] args, boolean debug) { Class[] types = m.getParameterTypes(); if (types.length != args.length) { if (debug) System.out.println("Bad parameter length: " + args.length + " vs " + types.length); return false; } for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) { if (debug) System.out.println("Bad parameter " + i + ": " + args[i] + " vs " + types[i]); return false; } return true; } static Method findStaticMethod(Class c, String method, Object... args) { Class _c = c; while (c != null) { for (Method m : c.getDeclaredMethods()) { if (!m.getName().equals(method)) continue; if ((m.getModifiers() & Modifier.STATIC) == 0 || !findStaticMethod_checkArgs(m, args)) continue; return m; } c = c.getSuperclass(); } return null; } static boolean findStaticMethod_checkArgs(Method m, Object[] args) { Class[] types = m.getParameterTypes(); if (types.length != args.length) return false; for (int i = 0; i < types.length; i++) if (!(args[i] == null || isInstanceX(types[i], args[i]))) return false; return true; } static LinkedHashMap cloneLinkedHashMap(Map map) { return map == null ? new LinkedHashMap() : new LinkedHashMap(map); } static A second(List l) { return get(l, 1); } static A second(Iterable l) { if (l == null) return null; Iterator it = iterator(l); if (!it.hasNext()) return null; it.next(); return it.hasNext() ? it.next() : null; } static A second(A[] bla) { return bla == null || bla.length <= 1 ? null : bla[1]; } static B second(Pair p) { return p == null ? null : p.b; } static char second(String s) { return charAt(s, 1); } static File loadLibrary(String snippetID) { return loadBinarySnippet(snippetID); } static UnsupportedOperationException unsupportedOperation() { throw new UnsupportedOperationException(); } static int hashCodeFor(Object a) { return a == null ? 0 : a.hashCode(); } static Method findMethod_cached(Object o, String method, Object... args) { try { if (o == null) return null; if (o instanceof Class) { _MethodCache cache = callOpt_getCache(((Class) o)); List methods = cache.cache.get(method); if (methods != null) for (Method m : methods) if (isStaticMethod(m) && findMethod_checkArgs(m, args, false)) return m; return null; } else { _MethodCache cache = callOpt_getCache(o.getClass()); List methods = cache.cache.get(method); if (methods != null) for (Method m : methods) if (findMethod_checkArgs(m, args, false)) return m; return null; } } catch (Exception __e) { throw rethrow(__e); } } static char charAt(String s, int i) { return s != null && i >= 0 && i < s.length() ? s.charAt(i) : '\0'; } static File loadBinarySnippet(String snippetID) { try { IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.loadLibrary(snippetID); long id = parseSnippetID(snippetID); if (isImageServerSnippet(id)) return loadImageAsFile(snippetID); File f = DiskSnippetCache_getLibrary(id); if (fileSize(f) == 0) f = loadDataSnippetToFile(snippetID); return f; } catch (Exception __e) { throw rethrow(__e); } } static boolean isImageServerSnippet(long id) { return id >= 1100000 && id < 1200000; } static File loadImageAsFile(String snippetIDOrURL) { try { if (isURL(snippetIDOrURL)) throw fail("not implemented"); if (!isSnippetID(snippetIDOrURL)) throw fail("Not a URL or snippet ID: " + snippetIDOrURL); String snippetID = "" + parseSnippetID(snippetIDOrURL); File file = imageSnippetCacheFile(snippetID); if (fileSize(file) > 0) return file; String imageURL = snippetImageURL_noHttps(snippetID); System.err.println("Loading image: " + imageURL); byte[] data = loadBinaryPage(imageURL); saveBinaryFile(file, data); return file; } catch (Exception __e) { throw rethrow(__e); } } // If you change this, also change DiskSnippetCache_fileToLibID static File DiskSnippetCache_file(long snippetID) { return new File(getGlobalCache(), "data_" + snippetID + ".jar"); } // Data files are immutable, use centralized cache public static File DiskSnippetCache_getLibrary(long snippetID) throws IOException { File file = DiskSnippetCache_file(snippetID); return file.exists() ? file : null; } public static void DiskSnippetCache_putLibrary(long snippetID, byte[] data) throws IOException { saveBinaryFile(DiskSnippetCache_file(snippetID), data); } static byte[] loadDataSnippetImpl(String snippetID) throws IOException { byte[] data; try { URL url = new URL(dataSnippetLink(snippetID)); print("Loading library: " + hideCredentials(url)); try { data = loadBinaryPage(url.openConnection()); } catch (RuntimeException e) { data = null; } if (data == null || data.length == 0) { url = new URL(tb_mainServer() + "/blobs/" + parseSnippetID(snippetID)); print("Loading library: " + hideCredentials(url)); data = loadBinaryPage(url.openConnection()); } print("Bytes loaded: " + data.length); } catch (FileNotFoundException e) { throw new IOException("Binary snippet #" + snippetID + " not found or not public"); } return data; } static long fileSize(String path) { return getFileSize(path); } static long fileSize(File f) { return getFileSize(f); } static File loadDataSnippetToFile(String snippetID) { try { snippetID = fsI(snippetID); IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return rl.loadLibrary(snippetID); File f = DiskSnippetCache_file(parseSnippetID(snippetID)); List urlsTried = new ArrayList(); List errors = new ArrayList(); try { URL url = addAndReturn(urlsTried, new URL(dataSnippetLink(snippetID))); print("Loading library: " + hideCredentials(url)); try { loadBinaryPageToFile(openConnection(url), f); if (fileSize(f) == 0) throw fail(); } catch (Throwable e) { errors.add(e); url = addAndReturn(urlsTried, new URL(tb_mainServer() + "/blobs/" + psI(snippetID))); print(e); print("Trying other server: " + hideCredentials(url)); loadBinaryPageToFile(openConnection(url), f); print("Got bytes: " + fileSize(f)); } // TODO: check if we hit the "LOADING" message if (fileSize(f) == 0) throw fail(); System.err.println("Bytes loaded: " + fileSize(f)); } catch (Throwable e) { //printStackTrace(e); errors.add(e); throw fail("Binary snippet " + snippetID + " not found or not public. URLs tried: " + allToString(urlsTried) + ", errors: " + allToString(errors)); } return f; } catch (Exception __e) { throw rethrow(__e); } } static boolean loadBufferedImage_useImageCache = true; static BufferedImage loadBufferedImage(String snippetIDOrURLOrFile) { try { ping(); if (snippetIDOrURLOrFile == null) return null; if (isURL(snippetIDOrURLOrFile)) return imageIO_readURL(snippetIDOrURLOrFile); if (isAbsolutePath(snippetIDOrURLOrFile)) return loadBufferedImage(new File(snippetIDOrURLOrFile)); if (!isSnippetID(snippetIDOrURLOrFile)) throw fail("Not a URL or snippet ID or file: " + snippetIDOrURLOrFile); String snippetID = "" + parseSnippetID(snippetIDOrURLOrFile); IResourceLoader rl = vm_getResourceLoader(); if (rl != null) return loadBufferedImage(rl.loadLibrary(snippetID)); File dir = imageSnippetsCacheDir(); if (loadBufferedImage_useImageCache) { dir.mkdirs(); File file = new File(dir, snippetID + ".png"); if (file.exists() && file.length() != 0) try { return ImageIO.read(file); } catch (Throwable e) { e.printStackTrace(); // fall back to loading from sourceforge } } String imageURL = snippetImageURL_http(snippetID); print("Loading image: " + imageURL); BufferedImage image = imageIO_readURL(imageURL); if (loadBufferedImage_useImageCache) { File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis()); ImageIO.write(image, "png", tempFile); tempFile.renameTo(new File(dir, snippetID + ".png")); //Log.info("Cached image."); } //Log.info("Loaded image."); return image; } catch (Exception __e) { throw rethrow(__e); } } static BufferedImage loadBufferedImage(File file) { try { return file.isFile() ? ImageIO.read(file) : null; } catch (Exception __e) { throw rethrow(__e); } } static boolean isURL(String s) { return startsWithOneOf(s, "http://", "https://", "file:"); } static File imageSnippetCacheFile(String snippetID) { File dir = imageSnippetsCacheDir(); if (!loadBufferedImage_useImageCache) return null; return new File(dir, parseSnippetID(snippetID) + ".png"); } static String snippetImageURL_noHttps(String snippetID) { return snippetImageURL_noHttps(snippetID, "png"); } static String snippetImageURL_noHttps(String snippetID, String contentType) { return snippetImageURL(snippetID, contentType) .replace("https://www.botcompany.de:8443/", "http://www.botcompany.de:8080/") .replace("https://botcompany.de/", "http://botcompany.de/"); } static ThreadLocal>> loadBinaryPage_responseHeaders = new ThreadLocal(); static ThreadLocal> loadBinaryPage_extraHeaders = new ThreadLocal(); static byte[] loadBinaryPage(String url) { try { print("Loading " + url); return loadBinaryPage(loadPage_openConnection(new URL(url))); } catch (Exception __e) { throw rethrow(__e); } } static byte[] loadBinaryPage(URLConnection con) { try { Map extraHeaders = getAndClearThreadLocal(loadBinaryPage_extraHeaders); setHeaders(con); for (String key : keys(extraHeaders)) con.setRequestProperty(key, extraHeaders.get(key)); return loadBinaryPage_noHeaders(con); } catch (Exception __e) { throw rethrow(__e); } } static byte[] loadBinaryPage_noHeaders(URLConnection con) { try { ByteArrayOutputStream buf = new ByteArrayOutputStream(); InputStream inputStream = con.getInputStream(); loadBinaryPage_responseHeaders.set(con.getHeaderFields()); long len = 0; try { len = con.getContentLength/*Long*/(); } catch (Throwable e) { printStackTrace(e); } int n = 0; while (true) { int ch = inputStream.read(); if (ch < 0) break; buf.write(ch); if (++n % 100000 == 0) println(" " + n + (len != 0 ? "/" + len : "") + " bytes loaded."); } inputStream.close(); return buf.toByteArray(); } catch (Exception __e) { throw rethrow(__e); } } /** writes safely (to temp file, then rename) */ public static byte[] saveBinaryFile(String fileName, byte[] contents) { try { File file = new File(fileName); File parentFile = file.getParentFile(); if (parentFile != null) parentFile.mkdirs(); String tempFileName = fileName + "_temp"; FileOutputStream fileOutputStream = newFileOutputStream(tempFileName); fileOutputStream.write(contents); fileOutputStream.close(); if (file.exists() && !file.delete()) throw new IOException("Can't delete " + fileName); if (!new File(tempFileName).renameTo(file)) throw new IOException("Can't rename " + tempFileName + " to " + fileName); vmBus_send("wroteFile", file); return contents; } catch (Exception __e) { throw rethrow(__e); } } static byte[] saveBinaryFile(File fileName, byte[] contents) { return saveBinaryFile(fileName.getPath(), contents); } static String dataSnippetLink(String snippetID) { long id = parseSnippetID(snippetID); if (id >= 1100000 && id < 1200000) return imageServerURL() + id; if (id >= 1200000 && id < 1300000) { // Woody files, actually String pw = muricaPassword(); if (empty(pw)) throw fail("Please set 'murica password by running #1008829"); return "http://butter.botcompany.de:8080/1008823/raw/" + id + "?_pass=" + pw; // XXX, although it typically gets hidden when printing } return fileServerURL() + "/" + id /*+ "?_pass=" + muricaPassword()*/; } static A addAndReturn(Collection c, A a) { if (c != null) c.add(a); return a; } static void loadBinaryPageToFile(String url, File file) { try { print("Loading " + url); loadBinaryPageToFile(openConnection(new URL(url)), file); } catch (Exception __e) { throw rethrow(__e); } } static void loadBinaryPageToFile(URLConnection con, File file) { try { setHeaders(con); loadBinaryPageToFile_noHeaders(con, file); } catch (Exception __e) { throw rethrow(__e); } } static void loadBinaryPageToFile_noHeaders(URLConnection con, File file) { try { File ftemp = new File(f2s(file) + "_temp"); FileOutputStream buf = newFileOutputStream(mkdirsFor(ftemp)); try { InputStream inputStream = con.getInputStream(); long len = 0; try { len = con.getContentLength/*Long*/(); } catch (Throwable e) { printStackTrace(e); } String pat = " {*}" + (len != 0 ? "/" + len : "") + " bytes loaded."; copyStreamWithPrints(inputStream, buf, pat); inputStream.close(); buf.close(); file.delete(); renameFile_assertTrue(ftemp, file); } finally { if (buf != null) buf.close(); } } catch (Exception __e) { throw rethrow(__e); } } static List allToString(Iterable c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } static List allToString(Object[] c) { List l = new ArrayList(); for (Object o : unnull(c)) l.add(str(o)); return l; } static BufferedImage imageIO_readURL(String url) { try { if (startsWith(url, "https:")) // Java is still buggy there :( disableCertificateValidation(); return ImageIO.read(new URL(url)); } catch (Exception __e) { throw rethrow(__e); } } static boolean isAbsolutePath(String s) { return s != null && new File(s).isAbsolute(); } static boolean isAbsolutePath(File f) { return f != null && f.isAbsolute(); } static File imageSnippetsCacheDir() { return javaxCachesDir("Image-Snippets"); } static String snippetImageURL_http(String snippetID) { return snippetImageURL_http(snippetID, "png"); } static String snippetImageURL_http(String snippetID, String contentType) { return replacePrefix("https://", "http://", snippetImageURL(snippetID, contentType)).replace(":8443", ":8080"); } static String snippetImageURL(long snippetID) { return snippetImageURL(fsI(snippetID)); } static String snippetImageURL(String snippetID) { return snippetImageURL(snippetID, "png"); } static String snippetImageURL(String snippetID, String contentType) { if (isURL(snippetID)) return snippetID; long id = parseSnippetID(snippetID); String url; if (isImageServerSnippet(id)) url = imageServerLink(id); else //url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + id + "&contentType=image/" + contentType; url = "https://botcompany.de/img/" + id; return url; } static A println(A a) { return print(a); } static String imageServerURL() { return or2(trim(loadTextFile(javaxDataDir("image-server-url.txt"))), "http://botcompany.de/images/raw/"); } static volatile boolean muricaPassword_pretendNotAuthed = false; static String muricaPassword() { if (muricaPassword_pretendNotAuthed) return null; return trim(loadTextFile(muricaPasswordFile())); } static String fileServerURL() { return "https://botcompany.de/files"; } public static File mkdirsFor(File file) { return mkdirsForFile(file); } static void copyStreamWithPrints(InputStream in, OutputStream out, String pat) { try { byte[] buf = new byte[65536]; int total = 0; while (true) { int n = in.read(buf); if (n <= 0) return; out.write(buf, 0, n); if ((total+n)/100000 > total/100000) print(pat.replace("{*}", str(roundDownTo(total, 100000)))); total += n; } } catch (Exception __e) { throw rethrow(__e); } } static void renameFile_assertTrue(File a, File b) { try { if (!a.exists()) throw fail("Source file not found: " + f2s(a)); if (b.exists()) throw fail("Target file exists: " + f2s(b)); mkdirsForFile(b); if (!a.renameTo(b)) throw fail("Can't rename " + f2s(a) + " to " + f2s(b)); } catch (Exception __e) { throw rethrow(__e); } } static String imageServerLink(String md5OrID) { if (possibleMD5(md5OrID)) return "https://botcompany.de/images/md5/" + md5OrID; return imageServerLink(parseSnippetID(md5OrID)); } static String imageServerLink(long id) { return "https://botcompany.de/images/" + id; } static String or2(String a, String b) { return nempty(a) ? a : b; } static String or2(String a, String b, String c) { return or2(or2(a, b), c); } static File muricaPasswordFile() { return new File(javaxSecretDir(), "murica/muricaPasswordFile"); } static int roundDownTo(int x, int n) { return x/n*n; } static long roundDownTo(long x, long n) { return x/n*n; } static boolean possibleMD5(String s) { return isMD5(s); } static boolean isMD5(String s) { return l(s) == 32 && isLowerHexString(s); } static boolean isLowerHexString(String s) { for (int i = 0; i < l(s); i++) { char c = s.charAt(i); if (c >= '0' && c <= '9' || c >= 'a' && c <= 'f') { // ok } else return false; } return true; } }