The comparison function looks a bit heavy. If it is not important for some reasons that the stored strings keep their trailing and leading whitespace, you could make sure that the strings are trimmed when you store them in a pn_report_row_t.
Another thing to reduce code might be to store the strings in an array instead of individual variables. You can provide an enum, so each index has a descriptive name (yours aren't very descriptive).
A small fault is that comparison operators should not modify what is being compared and therefore they should take the arguments by const reference.
There's also a small problem with the trimming function. The return statement should be moved to the end of the function, otherwise it won't return anything if the string is empty. (The test whether the string is empty is not very important: find_first_not_of would simply return npos and it is OK to clear an empty string.)
Code:
std::string siemens_trim(std::string inputStr)
{
if (!inputStr.empty())
{
/*get the first white space in the string*/
std::string::size_type startPos = inputStr.find_first_not_of("\n\t ");
if (startPos != std::string::npos)
{
/*get the last white space in the string*/
std::string::size_type endPos = inputStr.find_last_not_of("\n\t ");
/*trim the white spaces*/
inputStr = inputStr.substr(startPos, endPos - startPos + 1);
}
else
{
inputStr.clear();
}
}
return inputStr;
}
class pn_report_row_t
{
public:
enum
{
cpItemId, cpObjStr, mfgObjStr, morItemId, morObjStr, sorItemId, sorObjStr,
manufacturerType, orderDesc, addnOrderDesc, objStr, lastOrdDt, howImpact,
maxField
};
const std::string& get(unsigned index) const
{
assert(index < maxField);
return fields[index];
}
void set(const std::string& value, unsigned index)
{
assert(index < maxField);
fields[index] = siemens_trim(value);
}
private:
std::string fields[maxField];
};
bool operator< (const pn_report_row_t& primaryRow, const pn_report_row_t& secondaryRow)
{
for (unsigned i = 0; i != pn_report_row_t::maxField; ++i) {
if (primaryRow.get(i) != secondaryRow.get(i)) {
return primaryRow.get(i) < secondaryRow.get(i);
}
}
return false;
}
You could also optimize the trimming function a bit by avoiding some unnecessary copying. (A single exit point in a function is a good thing, but it isn't that important in C++ where clean-up can be managed by objects.)
Code:
std::string siemens_trim(const std::string& inputStr)
{
if (!inputStr.empty())
{
/*get the first white space in the string*/
std::string::size_type startPos = inputStr.find_first_not_of("\n\t ");
if (startPos != std::string::npos)
{
/*get the last white space in the string*/
std::string::size_type endPos = inputStr.find_last_not_of("\n\t ");
/*trim the white spaces*/
return inputStr.substr(startPos, endPos - startPos + 1);
}
}
return std::string();
}