📄 lispformat.java
字号:
/** Handle <code>~<...~></code> - pretty-printing logical block. * (Justification is not implemented.) */class LispPrettyFormat extends ReportFormat{ Format[] segments; Format body; String prefix; String suffix; boolean perLine; boolean seenAt; public int format(Object[] args, int start, Writer dst, FieldPosition fpos) throws java.io.IOException { String pre = prefix; String suf = suffix; OutPort out = dst instanceof OutPort ? (OutPort) dst : null; try { if (seenAt) { if (out != null) out.startLogicalBlock(pre, perLine, suffix); start = ReportFormat.format(body, args, start, dst, fpos); } else { Object curArg = args[start]; Object[] curArr = LispFormat.asArray(curArg); if (curArr == null) pre = suf = ""; if (out != null) out.startLogicalBlock(pre, perLine, suffix); if (curArr == null) ObjectFormat.format(curArg, dst, -1, true); else ReportFormat.format(body, curArr, 0, dst, fpos); start++; } } finally { if (out != null) out.endLogicalBlock(suf); } return start; } public String toString () { StringBuffer sbuf = new StringBuffer(); sbuf.append("LispPrettyFormat["); sbuf.append("prefix: \""); sbuf.append(prefix); sbuf.append("\", suffix: \""); sbuf.append(suffix); sbuf.append("\", body: "); sbuf.append(body); sbuf.append("]"); return sbuf.toString(); }}class LispIterationFormat extends ReportFormat{ int maxIterations; boolean seenAt; boolean seenColon; boolean atLeastOnce; Format body; public static int format(Format body, int maxIterations, Object[] args, int start, Writer dst, boolean seenColon, boolean atLeastOnce) throws java.io.IOException { for (int i = 0; ; i++) { if (i == maxIterations && maxIterations != -1) break; if (start == args.length && (i > 0 || ! atLeastOnce)) break; if (seenColon) { Object curArg = args[start]; Object[] curArr = LispFormat.asArray(curArg); if (curArr == null) { // ? } int result = ReportFormat.format(body, curArr, 0, dst, null); start++; if (ReportFormat.resultCode(result) == LispEscapeFormat.ESCAPE_ALL) break; } else { start = ReportFormat.format(body, args, start, dst, null); if (start < 0) { start = ReportFormat.nextArg(start); break; } } } return start; } public int format(Object[] args, int start, Writer dst, FieldPosition fpos) throws java.io.IOException { int maxIterations = getParam(this.maxIterations, -1, args, start); if (this.maxIterations == LispFormat.PARAM_FROM_LIST) start++; Format body = this.body; if (body == null) { // from args Object arg = args[start++]; if (arg instanceof java.text.Format) body = (java.text.Format) arg; else { try { body = new LispFormat(arg.toString()); } catch (Exception ex) { print(dst, "<invalid argument for \"~{~}\" format>"); return args.length; // FIXME } } } if (seenAt) { return format(body, maxIterations, args, start, dst, seenColon, atLeastOnce); } else { Object arg = args[start]; Object[] curArgs = LispFormat.asArray(arg); if (curArgs == null) dst.write("{"+arg+"}".toString()); else format(body, maxIterations, curArgs, 0, dst, seenColon, atLeastOnce); return start + 1; } } public String toString () { StringBuffer sbuf = new StringBuffer(); sbuf.append("LispIterationFormat["); sbuf.append(body); sbuf.append("]"); return sbuf.toString(); }}class LispChoiceFormat extends ReportFormat{ int param; boolean lastIsDefault; boolean testBoolean; // choice[0] is selected if arg is false. boolean skipIfFalse; Format[] choices; public int format(Object[] args, int start, Writer dst, FieldPosition fpos) throws java.io.IOException { Format fmt; if (testBoolean) // Handles ~:[false~;true~] { fmt = choices[args[start] == Boolean.FALSE ? 0 : 1]; start++; } else if (! skipIfFalse) { int index = getParam(this.param, LispFormat.PARAM_FROM_LIST, args, start); if (param == LispFormat.PARAM_FROM_LIST) start++; if (index < 0 || index >= choices.length) { if (lastIsDefault) index = choices.length - 1; else return start; } fmt = choices[index]; } else { if (args[start] == Boolean.FALSE) return start + 1; fmt = choices[0]; } return ReportFormat.format(fmt, args, start, dst, fpos); }}class LispRepositionFormat extends ReportFormat{ boolean backwards; boolean absolute; int count; public LispRepositionFormat(int count, boolean backwards, boolean absolute) { this.count = count; this.backwards = backwards; this.absolute = absolute; } public int format(Object[] args, int start, Writer dst, FieldPosition fpos) throws java.io.IOException { int count = getParam(this.count, absolute ? 0 : 1, args, start); if (!absolute) { if (backwards) count = -count; count += start; } return count < 0 ? 0 : count > args.length ? args.length : count; }}class LispFreshlineFormat extends ReportFormat{ int count; public LispFreshlineFormat (int count) { this.count = count; } public int format(Object[] args, int start, Writer dst, FieldPosition fpos) throws java.io.IOException { int count = getParam(this.count, 1, args, start); if (this.count == LispFormat.PARAM_FROM_LIST) start++; if (count > 0) { if (dst instanceof OutPort) { ((OutPort) dst).freshLine(); count--; } while (--count >= 0) dst.write('\n'); } return start; }}class LispTabulateFormat extends ReportFormat{ boolean relative; int colnum; int colinc; int padChar; public LispTabulateFormat(int colnum, int colinc, int padChar, boolean relative) { this.colnum = colnum; this.colinc = colinc; this.relative = relative; this.padChar = padChar; } public int format(Object[] args, int start, Writer dst, FieldPosition fpos) throws java.io.IOException { int colnum = getParam(this.colnum, 1, args, start); if (this.colnum == LispFormat.PARAM_FROM_LIST) start++; int colinc = getParam(this.colinc, 1, args, start); if (this.colinc == LispFormat.PARAM_FROM_LIST) start++; // Extension from SLIB: char padChar = getParam(this.padChar, ' ', args, start); if (this.padChar == LispFormat.PARAM_FROM_LIST) start++; int column = -1; if (dst instanceof OutPort) column = ((OutPort) dst).getColumnNumber(); int spaces; if (column >= 0) { if (! relative) { if (column < colnum) spaces = colnum - column; else if (colinc <= 0) spaces = 0; else spaces = colinc - (column - colnum) % colinc; } else { spaces = colnum + colinc - (column + colnum) % colinc; } } else { spaces = relative ? colnum : 2; } while (--spaces >= 0) dst.write(padChar); return start; }}/* Support for ~F, ~$, ~E, ~G. */class LispRealFormat extends ReportFormat{ char op; int arg1; int arg2; int arg3; int arg4; int arg5; int arg6; int arg7; boolean showPlus; boolean internalPad; /** Twice the number of args consumed; odd if any arg is PARAM_FROM_COUNT. */ int argsUsed; LispRealFormat() { argsUsed = (arg1 == LispFormat.PARAM_FROM_COUNT || arg2 == LispFormat.PARAM_FROM_COUNT || arg3 == LispFormat.PARAM_FROM_COUNT || arg4 == LispFormat.PARAM_FROM_COUNT || arg5 == LispFormat.PARAM_FROM_COUNT || arg6 == LispFormat.PARAM_FROM_COUNT || arg7 == LispFormat.PARAM_FROM_COUNT) ? 1 : 0; if (arg1 == LispFormat.PARAM_FROM_LIST) argsUsed += 2; if (arg2 == LispFormat.PARAM_FROM_LIST) argsUsed += 2; if (arg3 == LispFormat.PARAM_FROM_LIST) argsUsed += 2; if (arg4 == LispFormat.PARAM_FROM_LIST) argsUsed += 2; if (arg5 == LispFormat.PARAM_FROM_LIST) argsUsed += 2; if (arg6 == LispFormat.PARAM_FROM_LIST) argsUsed += 2; if (arg7 == LispFormat.PARAM_FROM_LIST) argsUsed += 2; } public Format resolve (Object[] args, int start) { if (op == '$') { FixedRealFormat mfmt = new FixedRealFormat(); int decimals = getParam(this.arg1, 2, args, start); if (this.arg1 == LispFormat.PARAM_FROM_LIST) start++; int digits = getParam(this.arg2, 1, args, start); if (this.arg2 == LispFormat.PARAM_FROM_LIST) start++; int width = getParam(this.arg3, 0, args, start); if (this.arg3 == LispFormat.PARAM_FROM_LIST) start++; char padChar = getParam(this.arg4, ' ', args, start); if (this.arg4 == LispFormat.PARAM_FROM_LIST) start++; mfmt.setMaximumFractionDigits(decimals); mfmt.setMinimumIntegerDigits(digits); mfmt.width = width; mfmt.padChar = padChar; mfmt.internalPad = internalPad; mfmt.showPlus = showPlus; return mfmt; } else if (op == 'F') { FixedRealFormat mfmt = new FixedRealFormat(); int width = getParam(this.arg1, 0, args, start); if (this.arg1 == LispFormat.PARAM_FROM_LIST) start++; int decimals = getParam(this.arg2, -1, args, start); if (this.arg2 == LispFormat.PARAM_FROM_LIST) start++; int scale = getParam(this.arg3, 0, args, start); if (this.arg3 == LispFormat.PARAM_FROM_LIST) start++; mfmt.overflowChar = getParam(this.arg4, '\0', args, start); if (this.arg4 == LispFormat.PARAM_FROM_LIST) start++; char padChar = getParam(this.arg5, ' ', args, start); if (this.arg5 == LispFormat.PARAM_FROM_LIST) start++; mfmt.setMaximumFractionDigits(decimals); mfmt.setMinimumIntegerDigits(0); mfmt.width = width; mfmt.scale = scale; mfmt.padChar = padChar; mfmt.internalPad = internalPad; mfmt.showPlus = showPlus; return mfmt; } else // if (op == 'E' || op == 'G') { ExponentialFormat efmt = new ExponentialFormat(); efmt.exponentShowSign = true; efmt.width = getParam(this.arg1, 0, args, start); if (this.arg1 == LispFormat.PARAM_FROM_LIST) start++; efmt.fracDigits = getParam(this.arg2, -1, args, start); if (this.arg2 == LispFormat.PARAM_FROM_LIST) start++; efmt.expDigits = getParam(this.arg3, 0, args, start); if (this.arg3 == LispFormat.PARAM_FROM_LIST) start++; efmt.intDigits = getParam(this.arg4, 1, args, start); if (this.arg4 == LispFormat.PARAM_FROM_LIST) start++; efmt.overflowChar = getParam(this.arg5, '\0', args, start); if (this.arg5 == LispFormat.PARAM_FROM_LIST) start++; efmt.padChar = getParam(this.arg6, ' ', args, start); if (this.arg6 == LispFormat.PARAM_FROM_LIST) start++; efmt.exponentChar = getParam(this.arg7, 'E', args, start); if (this.arg7 == LispFormat.PARAM_FROM_LIST) start++; efmt.general = op == 'G'; efmt.showPlus = showPlus; return efmt; } } public int format(Object[] args, int start, Writer dst, FieldPosition fpos) throws java.io.IOException { StringBuffer sbuf = new StringBuffer(100); Format fmt = resolve(args, start); start += argsUsed >> 1; RealNum value = (RealNum) args[start++]; fmt.format(value, sbuf, fpos); dst.write(sbuf.toString()); return start; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -