From c2ac634a32c6067570425d0d35f4b09fe732ec90 Mon Sep 17 00:00:00 2001 From: duy Date: Tue, 29 Oct 2024 22:59:03 +0700 Subject: [PATCH] Check max output size before displaying --- .../java/com/symja/evaluator/OutputForm.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/modules/symja-evaluator/src/main/java/com/symja/evaluator/OutputForm.java b/modules/symja-evaluator/src/main/java/com/symja/evaluator/OutputForm.java index 27250fd..46c96d3 100644 --- a/modules/symja-evaluator/src/main/java/com/symja/evaluator/OutputForm.java +++ b/modules/symja-evaluator/src/main/java/com/symja/evaluator/OutputForm.java @@ -1,19 +1,28 @@ package com.symja.evaluator; +import org.matheclipse.core.basic.Config; import org.matheclipse.core.form.output.OutputFormFactory; import org.matheclipse.core.form.tex.TeXFormFactory; import org.matheclipse.core.interfaces.IExpr; +import org.matheclipse.parser.client.ParserConfig; public class OutputForm { + public static String toString(IExpr result) { if (result == null) { return ""; } try { - OutputFormFactory outputFormFactory = OutputFormFactory.get(true, false); - return outputFormFactory.toString(result); + OutputFormFactory outputFormFactory = OutputFormFactory.get(ParserConfig.PARSER_USE_LOWERCASE_SYMBOLS, false); + String string = outputFormFactory.toString(result); + + if (string.length() > Config.MAX_OUTPUT_SIZE) { + return "Max output size " + Config.MAX_OUTPUT_SIZE + " characters exceeded"; + } + + return string; } catch (Exception e) { - return "Error"; + return "Error: " + e.getMessage(); } } @@ -27,9 +36,14 @@ public static String toLatex(IExpr expr) { if (teXFormFactory.convert(buffer, expr)) { return buffer.toString(); } + + if (buffer.length() > Config.MAX_OUTPUT_SIZE) { + return "Max output size " + Config.MAX_OUTPUT_SIZE + " characters exceeded"; + } + return "Error"; } catch (Exception e) { - return "Error"; + return "Error: " + e.getMessage(); } } }