text
stringlengths
0
2.2M
// To avoid a "reading uninitialized 'decode'" warning
ASSERTV(LINE, ENCODED, decoded, INPUT, INPUT == decoded);
}
}
}
bsl::string replaceCharEntities(const bsl::string& withEntities)
// Replace select XML character references in the specified 'withEntities'
// with the corresponding ASCII character and return the result.
//
// The following pre-defined character entities are recognized only:
//..
// Entity Char Name
// ====== ==== ====
// &lt; < Less-than
// &gt; > Greater-than
// &amp; & Ampersand
// &apos; ' Apostrophe
// &quot; " Quote
{
// This code is neither fast, nor clever, and it is only correct within the
// confines of the round trip testing in this test driver.
static const struct Entity {
const char *d_ent;
bsl::size_t d_len;
char d_ch;
} ENTITIES[] = {
{ "&lt;", 4, '<' },
{ "&gt;", 4, '>' },
{ "&amp;", 5, '&' },
{ "&apos;", 6, '\'' },
{ "&quot;", 6, '"' },
{ 0, 0, 0 }
};
bsl::string rv(withEntities);
const Entity *entity = ENTITIES;
while (entity->d_ent) {
bsl::size_t pos = rv.find(entity->d_ent, 0);
while (pos != bsl::string::npos) {
rv.replace(pos, entity->d_len, 1, entity->d_ch);
pos = rv.find(entity->d_ent, pos + 1);
}
++entity;
}
return rv;
}
template <class TEST_DATA,
bsl::size_t NUM_DATA>
void printTextRoundTripScalarTester(const TEST_DATA (&DATA)[NUM_DATA])
// Verify round trip of 'printDecimal'/'parseDecimal' for the values in the
// specified 'DATA' array of the deduced 'NUM_DATA' length. The deduced
// 'TEST_DATA' must provide a member scalar 'Type' that will be printed
// from, and parsed into.
{
typedef typename TEST_DATA::Type Type;
typedef typename GetDecodeType<Type>::Type DecodeType;
// Some types we can print/encode from, but cannot parse/decode into.
// 'GetDecodeType' is the level of indirection that solves that issue.
for (bsl::size_t i = 0; i < NUM_DATA; ++i) {
const int LINE = DATA[i].d_line;
const Type INPUT = DATA[i].d_input;
bsl::stringstream ss;
Print::printText(ss, INPUT);
ASSERTV(LINE, ss.good());
const bsl::string ENCODED(replaceCharEntities(ss.str()));
DecodeType decoded;
int rc;
ASSERTV(LINE, 0 == (rc = Util::parseText(&decoded,
ENCODED.data(),
intLength(ENCODED))));
if (0 == rc) {
// To avoid a "reading uninitialized 'decode'" warning
ASSERTV(LINE, ENCODED, decoded, INPUT, INPUT == decoded);
}
}
}
template <class OBJECT_TYPE>
OBJECT_TYPE buildPrintTextInput(const bsl::string_view& HEADER,
const OBJECT_TYPE& INPUT,
const bsl::string_view& TRAILER)
// Build, and return a 'printText' input by concatenating the specified
// 'HEADER', 'INPUT', and ' TRAILER'.
{
OBJECT_TYPE rv(HEADER.begin(), HEADER.end());
rv.insert(rv.end(), INPUT.begin(), INPUT.end());
rv.insert(rv.end(), TRAILER.begin(), TRAILER.end());