Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

escape some characters in string display #133

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should remove this

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