Skip to content

Commit

Permalink
K.java - Standardize/fix toString
Browse files Browse the repository at this point in the history
  • Loading branch information
punx120 committed Feb 22, 2021
1 parent 5306329 commit 5d9efdb
Show file tree
Hide file tree
Showing 2 changed files with 378 additions and 56 deletions.
114 changes: 64 additions & 50 deletions src/main/studio/kdb/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ public Adverb(K.KBase o) {
public Object getObject() {
return o;
}

@Override
public String toString(boolean showType) {
try {
LimitedWriter writer = new LimitedWriter(256);
toString(writer, showType);
return writer.toString();
} catch (IOException e) {
e.printStackTrace();
return super.toString(showType);
}
}
}

public static class BinaryPrimitive extends Primitive {
Expand Down Expand Up @@ -241,12 +253,16 @@ public String getDataType() {
}

private final int primitive;
private String s = " ";
private final String s;

public Primitive(String[] ops, int i) {
primitive = i;
if (i >= 0 && i < ops.length)
s = ops[i];
s = i >= 0 && i < ops.length ? ops[i] : " ";
}

@Override
public String toString(boolean showType) {
return toString();
}

@Override
Expand Down Expand Up @@ -275,6 +291,18 @@ public Projection(K.KList objs) {
this.objs = objs;
}

@Override
public String toString(boolean showType) {
try {
LimitedWriter writer = new LimitedWriter(256);
toString(writer, showType);
return writer.toString();
} catch (IOException e) {
e.printStackTrace();
return super.toString(showType);
}
}

public void toString(LimitedWriter w, boolean showType) throws IOException {
boolean listProjection = false;
if ((objs.getLength() > 0) && (objs.at(0) instanceof UnaryPrimitive)) {
Expand Down Expand Up @@ -561,7 +589,7 @@ public KSymbol(String s) {
}

public String toString(boolean showType) {
return s;
return showType ? "`" + s : s;
}

public boolean isNull() {
Expand Down Expand Up @@ -681,18 +709,15 @@ public boolean isNull() {

public String toString(boolean showType) {
if (isNull())
return "0ne";
return showType ? "0Ne" : "0N";
else if (f == Float.POSITIVE_INFINITY)
return "0we";
return showType ? "0We" : "0W";
else if (f == Float.NEGATIVE_INFINITY)
return "-0we";
return showType ? "-0We" : "-0W";
else {
String s = Config.getInstance().getNumberFormat().format(f);
if (showType) {
double epsilon = 1e-9;
double diff = f - Math.round(f);
if ((diff < epsilon) && (diff > -epsilon))
s += "e";
s += "e";
}
return s;
}
Expand Down Expand Up @@ -731,18 +756,15 @@ public boolean isNull() {

public String toString(boolean showType) {
if (isNull())
return "0n";
return showType ? "0nf" : "0n";
else if (d == Double.POSITIVE_INFINITY)
return "0w";
return showType ? "0wf" : "0w";
else if (d == Double.NEGATIVE_INFINITY)
return "-0w";
return showType ? "-0wf" : "-0w";
else {
String s = Config.getInstance().getNumberFormat().format(d);
if (showType) {
double epsilon = 1e-9;
double diff = d - Math.round(d);
if ((diff < epsilon) && (diff > -epsilon))
s += "f";
s += "f";
}
return s;
}
Expand Down Expand Up @@ -875,14 +897,15 @@ public boolean isNull() {
}

public String toString(boolean showType) {
if (isNull())
return "0nz";
else if (time == Double.POSITIVE_INFINITY)
return "0wz";
else if (time == Double.NEGATIVE_INFINITY)
return "-0wz";
else
return sd("yyyy.MM.dd HH:mm:ss.SSS", toTimestamp());
if (isNull()) {
return showType ? "0Nz" : "0N";
} else if (time == Double.POSITIVE_INFINITY) {
return showType ? "0Wz" : "0W";
} else if (time == Double.NEGATIVE_INFINITY) {
return showType ? "-0Wz" : "-0W";
} else {
return sd(showType ? "yyyy.MM.dd HH:mm:ss.SSS'z'" : "yyyy.MM.dd HH:mm:ss.SSS", toTimestamp());
}
}

public void toString(LimitedWriter w, boolean showType) throws IOException {
Expand Down Expand Up @@ -1032,11 +1055,11 @@ public boolean isNull() {

public String toString(boolean showType) {
if (isNull())
return "0Nm";
return showType ? "0Nm" : "0N";
else if (i == Integer.MAX_VALUE)
return "0Wm";
return showType ? "0Wm" : "0W";
else if (i == -Integer.MAX_VALUE)
return "-0Wm";
return showType ? "-0Wm" : "-0W";
else {
int m = i + 24000, y = m / 12;
String s = i2(y / 100) + i2(y % 100) + "." + i2(1 + m % 12);
Expand Down Expand Up @@ -1379,9 +1402,9 @@ public KBase at(int i) {
}

void toStringVector(LimitedWriter w, boolean showType) throws IOException {
if (getLength() == 0)
if (getLength() == 0) {
w.write("`float$()");
else {
} else {
if (getLength() == 1)
w.write(enlist);

Expand All @@ -1401,14 +1424,11 @@ void toStringVector(LimitedWriter w, boolean showType) throws IOException {
w.write("-0w");
printedP = true;
} else {
double epsilon = 1e-9;
double diff = d - Math.round(d);
if (!((diff < epsilon) && (diff > -epsilon)))
printedP = true;
printedP = true;
w.write(nf.format(d));
}
}
if (!printedP)
if (showType && printedP)
w.write("f");
}
}
Expand All @@ -1429,9 +1449,9 @@ public KBase at(int i) {
}

void toStringVector(LimitedWriter w, boolean showType) throws IOException {
if (getLength() == 0)
if (getLength() == 0) {
w.write("`real$()");
else {
} else {
if (getLength() == 1)
w.write(enlist);

Expand All @@ -1451,12 +1471,11 @@ void toStringVector(LimitedWriter w, boolean showType) throws IOException {
w.write("-0W");
printedP = true;
} else {
if (d != ((int) d))
printedP = true;
printedP = true;
w.write(nf.format(d));
}
}
if (!printedP)
if (showType && printedP)
w.write("e");
}
}
Expand Down Expand Up @@ -1580,7 +1599,7 @@ else if (v == -Integer.MAX_VALUE)
w.write(sd("yyyy.MM.dd", new Date(86400000L * (v + 10957))));
}
}
if (printD)
if (showType && printD)
w.write("d");
}
}
Expand Down Expand Up @@ -1670,27 +1689,23 @@ void toStringVector(LimitedWriter w, boolean showType) throws IOException {
if (getLength() == 0)
w.write("`datetime$()");
else {
boolean printZ = true;
if (getLength() == 1)
w.write(enlist);
for (int i = 0; i < getLength(); i++) {
if (i > 0)
w.write(" ");
double d = Array.getDouble(array, i);
if (i > 0)
w.write(" ");
if (Double.isNaN(d))
w.write("0N");
else if (d == Double.POSITIVE_INFINITY)
w.write("0w");
w.write("0W");
else if (d == Double.NEGATIVE_INFINITY)
w.write("-0w");
w.write("-0W");
else {
printZ = false;
w.write(sd("yyyy.MM.dd HH:mm:ss.SSS", new Timestamp(((long) (.5 + 8.64e7 * (d + 10957))))));
}
}
if (printZ)
if (showType)
w.write("z");
}
}
Expand Down Expand Up @@ -1867,7 +1882,6 @@ public void serialise(OutputStream o) throws IOException {
}

void toStringVector(LimitedWriter w, boolean showType) throws IOException {
w.write(super.toString(showType));
if (getLength() == 0)
w.write("`boolean$()");
else {
Expand Down
Loading

0 comments on commit 5d9efdb

Please sign in to comment.