Skip to content

Commit

Permalink
fix query result printing for utf8
Browse files Browse the repository at this point in the history
  • Loading branch information
aziz-mu committed Dec 7, 2022
1 parent b739582 commit bdb5669
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/processor/result/flat_tuple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

#include "common/type_utils.h"
#include "common/utils.h"
#include "utf8proc.h"
#include "utf8proc_wrapper.h"

using namespace kuzu::utf8proc;

namespace kuzu {
namespace processor {
Expand Down Expand Up @@ -103,7 +107,17 @@ string FlatTuple::toString(const vector<uint32_t>& colsWidth, const string& deli
if (colsWidth[i] != 0) {
value = " " + value + " ";
}
result << left << setw((int)colsWidth[i]) << setfill(' ') << value;
uint32_t fieldLen = 0;
uint32_t chrIter = 0;
while (chrIter < value.length()) {
fieldLen += Utf8Proc::renderWidth(value.c_str(), chrIter);
chrIter = utf8proc_next_grapheme(value.c_str(), value.length(), chrIter);
}
if (colsWidth[i] != 0) {
result << value << string(colsWidth[i] - fieldLen, ' ');
} else {
result << value;
}
if (i != resultValues.size() - 1) {
result << delimiter;
}
Expand Down
15 changes: 12 additions & 3 deletions tools/shell/embedded_shell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,18 @@ void EmbeddedShell::printExecutionResult(QueryResult& queryResult) const {
if (tuple->getResultValue(i)->isNullVal()) {
continue;
}
uint32_t fieldLen = tuple->getResultValue(i)->toString().length() + 2;
colsWidth[i] =
max(colsWidth[i], (fieldLen > colsWidth[i]) ? fieldLen : colsWidth[i]);
string tupleString = tuple->getResultValue(i)->toString();
uint32_t fieldLen = 0;
uint32_t chrIter = 0;
while (chrIter < tupleString.length()) {
fieldLen += Utf8Proc::renderWidth(tupleString.c_str(), chrIter);
chrIter =
utf8proc_next_grapheme(tupleString.c_str(), tupleString.length(), chrIter);
}
// An extra 2 spaces are added for an extra space on either
// side of the string.
fieldLen += 2;
colsWidth[i] = max(colsWidth[i], fieldLen);
}
}
for (auto width : colsWidth) {
Expand Down

0 comments on commit bdb5669

Please sign in to comment.