@@ -23,84 +23,79 @@ See the Mulan PSL v2 for more details. */
2323#define RECORD_COUNT_LENGTH 40
2424
2525class RecordPrinter {
26- static constexpr size_t COL_WIDTH = 16 ;
2726 size_t num_cols;
2827
2928 public:
3029 RecordPrinter (size_t num_cols_) : num_cols(num_cols_) { assert (num_cols_ > 0 ); }
3130
3231 void print_separator (Context *context) const {
32+ int &offset = *(context->offset_ );
33+ char *buffer = context->data_send_ + offset;
34+
3335 for (size_t i = 0 ; i < num_cols; i++) {
34- constexpr ConstexprString<COL_WIDTH + 2 > str (' -' );
35- if (context->ellipsis_ == false && *context->offset_ + RECORD_COUNT_LENGTH + str.length () < BUFFER_LENGTH) {
36- memcpy (context->data_send_ + *(context->offset_ ), str.c_str (), str.length ());
37- *(context->offset_ ) = *(context->offset_ ) + str.length ();
36+ if (context->ellipsis_ == false && offset + RECORD_COUNT_LENGTH + COL_WIDTH + 3 < BUFFER_LENGTH) {
37+ buffer[0 ] = ' +' ;
38+ memset (buffer + 1 , ' -' , COL_WIDTH + 2 );
39+ offset += COL_WIDTH + 3 ;
40+ buffer += COL_WIDTH + 3 ;
3841 } else {
3942 context->ellipsis_ = true ;
4043 }
4144 }
42- constexpr ConstexprString str ( " + \n " );
43- if (context-> ellipsis_ == false && *context-> offset_ + RECORD_COUNT_LENGTH + str. length () < BUFFER_LENGTH) {
44- memcpy (context-> data_send_ + *(context-> offset_ ), str. c_str (), str. length ()) ;
45- *(context-> offset_ ) = *(context-> offset_ ) + str. length () ;
45+ if (context-> ellipsis_ == false && offset + RECORD_COUNT_LENGTH + 2 < BUFFER_LENGTH) {
46+ buffer[ 0 ] = ' + ' ;
47+ buffer[ 1 ] = ' \n ' ;
48+ offset += 2 ;
4649 } else {
4750 context->ellipsis_ = true ;
4851 }
4952 }
5053
51- template <typename StringType>
52- void print_record (const std::vector<StringType> &rec_str, Context *context) const {
53- assert (rec_str.size () == num_cols);
54- if constexpr (std::is_same_v<StringType, std::string>) {
55- for (const auto &col : rec_str) {
56- std::stringstream ss;
57- if (col.size () > COL_WIDTH) {
58- ss << " | " << std::setw (COL_WIDTH) << std::string_view (col).substr (0 , COL_WIDTH - 3 ) << " ..."
59- << " " ;
60- } else {
61- ss << " | " << std::setw (COL_WIDTH) << col << " " ;
62- }
63- if (context->ellipsis_ == false &&
64- *context->offset_ + RECORD_COUNT_LENGTH + ss.str ().length () < BUFFER_LENGTH) {
65- memcpy (context->data_send_ + *(context->offset_ ), ss.str ().c_str (), ss.str ().length ());
66- *(context->offset_ ) = *(context->offset_ ) + ss.str ().length ();
67- } else {
68- context->ellipsis_ = true ;
69- }
54+ void print_record (const std::vector<std::string_view> &rec_str, Context *context) const {
55+ int &offset = *(context->offset_ );
56+ char *buffer = context->data_send_ + offset;
57+
58+ for (auto col : rec_str) {
59+ if (context->ellipsis_ ) return ;
60+ if (offset + RECORD_COUNT_LENGTH + COL_WIDTH + 3 >= BUFFER_LENGTH) {
61+ context->ellipsis_ = true ;
62+ break ;
7063 }
71- } else if constexpr (std::is_same_v<StringType, std::string_view>) {
72- for (auto col : rec_str) {
73- std::stringstream ss;
74- if (col.size () > COL_WIDTH) {
75- ss << " | " << std::setw (COL_WIDTH) << col.substr (0 , COL_WIDTH - 3 ) << " ..." << " " ;
76- } else {
77- ss << " | " << std::setw (COL_WIDTH) << col << " " ;
78- }
79- if (context->ellipsis_ == false &&
80- *context->offset_ + RECORD_COUNT_LENGTH + ss.str ().length () < BUFFER_LENGTH) {
81- memcpy (context->data_send_ + *(context->offset_ ), ss.str ().c_str (), ss.str ().length ());
82- *(context->offset_ ) = *(context->offset_ ) + ss.str ().length ();
83- } else {
84- context->ellipsis_ = true ;
85- }
64+ strcpy (buffer, " | " );
65+ if (col.size () > COL_WIDTH) {
66+ memcpy (buffer + 2 , col.data (), COL_WIDTH - 3 );
67+ strcpy (buffer, " ... " );
68+ } else {
69+ memset (buffer + 2 , ' ' , COL_WIDTH - col.size ());
70+ memcpy (buffer + 2 + COL_WIDTH - col.size (), col.data (), col.size ());
71+ buffer[COL_WIDTH + 2 ] = ' ' ;
8672 }
87- } else {
88- std::cerr << " unsupported type: " << typeid (StringType).name () << std::endl;
73+
74+ buffer += COL_WIDTH + 3 ;
75+ offset += COL_WIDTH + 3 ;
8976 }
90- constexpr ConstexprString str (" |\n " );
91- if (context->ellipsis_ == false && *context->offset_ + RECORD_COUNT_LENGTH + str.length () < BUFFER_LENGTH) {
92- memcpy (context->data_send_ + *(context->offset_ ), str.c_str (), str.length ());
93- *(context->offset_ ) = *(context->offset_ ) + str.length ();
77+
78+ if (context->ellipsis_ == false && offset + RECORD_COUNT_LENGTH + 2 < BUFFER_LENGTH) {
79+ buffer[0 ] = ' |' ;
80+ buffer[1 ] = ' \n ' ;
81+ offset += 2 ;
82+ } else {
83+ context->ellipsis_ = true ;
9484 }
9585 }
9686
9787 static void print_record_count (size_t num_rec, Context *context) {
98- std::string str;
99- if (context->ellipsis_ == true ) {
100- str = " ... ...\n " ;
88+ int &offset = *(context->offset_ );
89+ char *buffer = context->data_send_ + offset;
90+
91+ if (context->ellipsis_ ) {
92+ sprintf (buffer, " ... ...\n " );
93+
94+ buffer += 8 ;
95+ offset += 8 ;
10196 }
102- str += " Total record(s): " + std::to_string (num_rec) + ' \n ' ;
103- memcpy (context-> data_send_ + *(context-> offset_ ), str. c_str (), str. length () );
104- *(context-> offset_ ) = *(context-> offset_ ) + str. length () ;
97+
98+ size_t size = sprintf (buffer, " Total record(s): %ld \n " , num_rec );
99+ offset += size ;
105100 }
106101};
0 commit comments