Skip to content

Commit

Permalink
escape some characters in string display
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Gyorok authored and Peter Gyorok committed Jun 27, 2022
1 parent 55bbf7f commit 3af85ba
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/main/studio/kdb/K.java
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ public boolean isNull() {
public StringBuilder format(StringBuilder builder, KFormatContext context) {
builder = super.format(builder, context);
if (context.showType()) {
builder.append("\"").append(c).append("\"");
builder.append("\"").append(escape(c)).append("\"");
} else {
builder.append(c);
}
Expand All @@ -875,6 +875,19 @@ public boolean equals(Object obj) {
}
return c == ((KCharacter) obj).c;
}

public static String escape(char ch) {
switch(ch) {
case '\000':
return "\\000";
case '\"':
return "\\\"";
case '\\':
return "\\\\";
default:
return String.valueOf(ch);
}
}
}

public static class KFloat extends KBase implements ToDouble {
Expand Down Expand Up @@ -1924,6 +1937,8 @@ public String getDataType() {

public KCharacterVector(String value) {
super(value.toCharArray(), 10, "char", "c");
System.out.println("value="+value+" length="+value.length());
new Exception().printStackTrace();
}

public KCharacter at(int i) {
Expand All @@ -1938,17 +1953,21 @@ public String getString() {
protected StringBuilder formatVector(StringBuilder builder, KFormatContext context) {
if (getLength() == 1) {
char ch = Array.getChar(array, 0);
if (ch <= 255) {
if (ch <= 127) { //for UTF-8, any char with code of 128 or above is encoded in multiple bytes
builder.append(enlist);
}
}

if (context.showType()) {
builder.append("\"");
}
builder.append(getString());
if (context.showType()) {
String str = getString();
for (int i=0; i<str.length(); ++i) {
char ch = str.charAt(i);
builder.append(KCharacter.escape(ch));
}
builder.append("\"");
} else {
builder.append(getString());
}
return builder;
}
Expand Down
6 changes: 6 additions & 0 deletions src/test/studio/kdb/KTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ public void testBooleanToString() throws Exception {
public void testCharacterToString() throws Exception {
check(new K.KCharacter(' '), " ", "\" \"");
check(new K.KCharacter('a'), "a", "\"a\"");
check(new K.KCharacter('\000'), "\000", "\"\\000\"");
check(new K.KCharacter('\\'), "\\", "\"\\\\\"");
check(new K.KCharacter('"'), "\"", "\"\\\"\"");

check(new K.KCharacterVector(" a"), " a", "\" a\"");
check(new K.KCharacterVector(""), "", "\"\"");
check(new K.KCharacterVector("a"), "enlist a", "enlist \"a\"");
check(new K.KCharacterVector(" \000"), " \000", "\" \\000\"");
check(new K.KCharacterVector(" \\"), " \\", "\" \\\\\"");
check(new K.KCharacterVector(" \""), " \"", "\" \\\"\"");
}

@Test
Expand Down

0 comments on commit 3af85ba

Please sign in to comment.