From 3af85ba0197a7e06ece68d66eecc546d906dba95 Mon Sep 17 00:00:00 2001 From: Peter Gyorok Date: Mon, 27 Jun 2022 17:21:14 +0100 Subject: [PATCH] escape some characters in string display --- src/main/studio/kdb/K.java | 29 ++++++++++++++++++++++++----- src/test/studio/kdb/KTest.java | 6 ++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/main/studio/kdb/K.java b/src/main/studio/kdb/K.java index fefebdec..ae453ae7 100755 --- a/src/main/studio/kdb/K.java +++ b/src/main/studio/kdb/K.java @@ -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); } @@ -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 { @@ -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) { @@ -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