Skip to content
Open
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
53 changes: 31 additions & 22 deletions icu4c/source/test/intltest/ssearch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
#include "ssearch.h"
#include "xmlparser.h"

#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdio.h> // for snprintf

char testId[100];
Expand Down Expand Up @@ -524,40 +527,48 @@ UBool OrderList::matchesAt(int32_t offset, const OrderList &other) const
return true;
}

static char *printOffsets(char *buffer, size_t n, OrderList &list)
static char* printOffsets(std::string &buffer, OrderList &list)
Copy link
Member

Choose a reason for hiding this comment

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

This would all become considerably simpler if the caller wasn't required to pass in any buffer at all and the function simply returned an std::string object by value. That's what any normal C++ code would do nowadays.

{
int32_t size = list.size();
char *s = buffer;
buffer.clear();
buffer.reserve(size * 10 + 2);
Copy link
Member

Choose a reason for hiding this comment

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

Is this some kind of performance optimization, to avoid having to dynamically resize the buffer? If so, I suggest removing it, this is test code, the possible performance gain is irrelevant compared to having a few extra lines of code to maintain.

std::stringstream s(buffer);

for(int32_t i = 0; i < size; i += 1) {
const Order *order = list.get(i);

if (i != 0) {
s += snprintf(s, n, ", ");
s << ", ";
}

s += snprintf(s, n, "(%d, %d)", order->lowOffset, order->highOffset);
s << "(" << order->lowOffset << ", " << order->highOffset << ")";
}

return buffer;
return buffer.data();
}

static char *printOrders(char *buffer, size_t n, OrderList &list)
static char* printOrders(std::string &buffer, OrderList &list)
{
int32_t size = list.size();
char *s = buffer;
buffer.clear();
// 10 chars is enough room for 8 hex digits plus ", "
buffer.reserve(size * 10 + 2);
std::stringstream s(buffer);

for(int32_t i = 0; i < size; i += 1) {
const Order *order = list.get(i);

if (i != 0) {
s += snprintf(s, n, ", ");
s << ", ";
}

s += snprintf(s, n, "%8.8X", order->order);
s << std::hex
<< std::uppercase
<< std::setw(8)
<< std::setfill('0')
<< order->order;
}

return buffer;
return buffer.data();
}

void SSearchTest::offsetTest()
Expand Down Expand Up @@ -631,9 +642,7 @@ void SSearchTest::offsetTest()
errcheckln(status, "Failed to create collator in offsetTest! - %s", u_errorName(status));
return;
}
char buffer[4096]; // A bit of a hack... just happens to be long enough for all the test cases...
// We could allocate one that's the right size by (CE_count * 10) + 2
// 10 chars is enough room for 8 hex digits plus ", ". 2 extra chars for "[" and "]"
std::string buffer;

col->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);

Expand Down Expand Up @@ -673,20 +682,20 @@ void SSearchTest::offsetTest()

if (forwardList.compare(backwardList)) {
logln("Works with \"%s\"", test[i]);
logln("Forward offsets: [%s]", printOffsets(buffer, sizeof(buffer), forwardList));
// logln("Backward offsets: [%s]", printOffsets(buffer, sizeof(buffer), backwardList));
logln("Forward offsets: [%s]", printOffsets(buffer, forwardList));
// logln("Backward offsets: [%s]", printOffsets(buffer, backwardList));

logln("Forward CEs: [%s]", printOrders(buffer, sizeof(buffer), forwardList));
// logln("Backward CEs: [%s]", printOrders(buffer, sizeof(buffer), backwardList));
logln("Forward CEs: [%s]", printOrders(buffer, forwardList));
// logln("Backward CEs: [%s]", printOrders(buffer, backwardList));

logln();
} else {
errln("Fails with \"%s\"", test[i]);
infoln("Forward offsets: [%s]", printOffsets(buffer, sizeof(buffer), forwardList));
infoln("Backward offsets: [%s]", printOffsets(buffer, sizeof(buffer), backwardList));
infoln("Forward offsets: [%s]", printOffsets(buffer, forwardList));
infoln("Backward offsets: [%s]", printOffsets(buffer, backwardList));

infoln("Forward CEs: [%s]", printOrders(buffer, sizeof(buffer), forwardList));
infoln("Backward CEs: [%s]", printOrders(buffer, sizeof(buffer), backwardList));
infoln("Forward CEs: [%s]", printOrders(buffer, forwardList));
infoln("Backward CEs: [%s]", printOrders(buffer, backwardList));

infoln();
}
Expand Down