Skip to content

Commit

Permalink
1. Add PrettyPrintOptions for enabling/disabling braces and for
Browse files Browse the repository at this point in the history
setting Array delimiter.
2. Update `Array::toString` to use `arrow::PrettyPrint` for Array
display.

Co-authored-by: Sarah Gilmore <[email protected]>
  • Loading branch information
kevingurney and sgilmore10 committed Sep 21, 2023
1 parent 7b30ba4 commit d7b7629
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
14 changes: 9 additions & 5 deletions cpp/src/arrow/pretty_print.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ void PrettyPrinter::OpenArray(const Array& array) {
if (!options_.skip_new_lines) {
Indent();
}
(*sink_) << "[";
if (options_.include_braces) {
(*sink_) << "[";
}
if (array.length() > 0) {
Newline();
indent_ += options_.indent_size;
Expand All @@ -101,7 +103,9 @@ void PrettyPrinter::CloseArray(const Array& array) {
Indent();
}
}
(*sink_) << "]";
if (options_.include_braces) {
(*sink_) << "]";
}
}

void PrettyPrinter::Write(std::string_view data) { (*sink_) << data; }
Expand Down Expand Up @@ -151,22 +155,22 @@ class ArrayPrinter : public PrettyPrinter {
IndentAfterNewline();
(*sink_) << "...";
if (!is_last && options_.skip_new_lines) {
(*sink_) << ",";
(*sink_) << options_.delimiter;
}
i = array.length() - window - 1;
} else if (array.IsNull(i)) {
IndentAfterNewline();
(*sink_) << options_.null_rep;
if (!is_last) {
(*sink_) << ",";
(*sink_) << options_.delimiter;
}
} else {
if (indent_non_null_values) {
IndentAfterNewline();
}
RETURN_NOT_OK(func(i));
if (!is_last) {
(*sink_) << ",";
(*sink_) << options_.delimiter;
}
}
Newline();
Expand Down
6 changes: 6 additions & 0 deletions cpp/src/arrow/pretty_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ struct PrettyPrintOptions {

/// If true, display schema metadata when pretty-printing a Schema
bool show_schema_metadata = true;

/// If true, include opening ("[") and closing ("]") braces when pretty-printing an Array
bool include_braces = false;

/// Character to use between Array elements (e.g. ",")
std::string delimiter = ",";
};

/// \brief Print human-readable representation of RecordBatch
Expand Down
13 changes: 12 additions & 1 deletion matlab/src/cpp/arrow/matlab/array/proxy/array.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

#include "libmexclass/proxy/ProxyManager.h"

#include <sstream>
#include "arrow/pretty_print.h"

namespace arrow::matlab::array::proxy {

Array::Array(std::shared_ptr<arrow::Array> array) : array{std::move(array)} {
Expand All @@ -44,7 +47,15 @@ namespace arrow::matlab::array::proxy {

void Array::toString(libmexclass::proxy::method::Context& context) {
::matlab::data::ArrayFactory factory;
const auto str_utf8 = array->ToString();
std::stringstream strs;
arrow::PrettyPrintOptions options;
options.window = 3;
options.skip_new_lines = true;
options.include_braces = false;
options.delimiter = " ";
// MATLAB array display uses 5 spaces between elements.
MATLAB_ERROR_IF_NOT_OK_WITH_CONTEXT(arrow::PrettyPrint(*array, options, &strs), context, error::ARRAY_DISPLAY_FAILED);
const auto str_utf8 = strs.str();
MATLAB_ASSIGN_OR_ERROR_WITH_CONTEXT(const auto str_utf16, arrow::util::UTF8StringToUTF16(str_utf8), context, error::UNICODE_CONVERSION_ERROR_ID);
auto str_mda = factory.createScalar(str_utf16);
context.outputs[0] = str_mda;
Expand Down
1 change: 1 addition & 0 deletions matlab/src/cpp/arrow/matlab/error/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,5 @@ namespace arrow::matlab::error {
static const char* STRUCT_ARRAY_MAKE_FAILED = "arrow:array:StructArrayMakeFailed";
static const char* INDEX_EMPTY_CONTAINER = "arrow:index:EmptyContainer";
static const char* INDEX_OUT_OF_RANGE = "arrow:index:OutOfRange";
static const char* ARRAY_DISPLAY_FAILED = "arrow:array:DisplayFailed";
}

0 comments on commit d7b7629

Please sign in to comment.