Skip to content
Open
Show file tree
Hide file tree
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
41 changes: 27 additions & 14 deletions src/main/cpp/stringhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,27 @@
#include <iterator>
#include <algorithm>
#include <cctype>
#include <stdexcept>

using namespace LOG4CXX_NS;
using namespace LOG4CXX_NS::helpers;

namespace
{
void checkFullyParsed(const std::string& value, std::size_t pos)
{
while (pos < value.size() && std::isspace(static_cast<unsigned char>(value[pos])))
{
++pos;
}

if (pos != value.size())
{
throw std::invalid_argument("unexpected trailing characters");
}
}
}

bool StringHelper::equalsIgnoreCase(const LogString& s1, const logchar* upper, const logchar* lower)
{
for (const auto& item : s1)
Expand Down Expand Up @@ -109,24 +126,20 @@ bool StringHelper::endsWith(const LogString& s, const LogString& suffix)

int StringHelper::toInt(const LogString& s)
{
#if LOG4CXX_LOGCHAR_IS_UNICHAR
std::string as;
Transcoder::encode(s, as);
return std::stoi(as);
#else
return std::stoi(s);
#endif
LOG4CXX_ENCODE_CHAR(as, s);
std::size_t pos = 0;
int value = std::stoi(as, &pos);
checkFullyParsed(as, pos);
return value;
}

int64_t StringHelper::toInt64(const LogString& s)
{
#if LOG4CXX_LOGCHAR_IS_UNICHAR
std::string as;
Transcoder::encode(s, as);
return std::stoll(as);
#else
return std::stoll(s);
#endif
LOG4CXX_ENCODE_CHAR(as, s);
std::size_t pos = 0;
auto value = std::stoll(as, &pos);
checkFullyParsed(as, pos);
return value;
}

void StringHelper::toString(int n, LogString& dst)
Expand Down
45 changes: 45 additions & 0 deletions src/test/cpp/helpers/stringhelpertestcase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <log4cxx/helpers/stringhelper.h>
#include "../insertwide.h"
#include "../logunit.h"
#include <stdexcept>


using namespace log4cxx;
Expand All @@ -42,6 +43,13 @@ LOGUNIT_CLASS(StringHelperTestCase)
LOGUNIT_TEST( testEndsWith3 );
LOGUNIT_TEST( testEndsWith4 );
LOGUNIT_TEST( testEndsWith5 );
LOGUNIT_TEST( testToIntAcceptsSign );
LOGUNIT_TEST( testToIntParsesWholeString );
LOGUNIT_TEST_EXCEPTION( testToIntRejectsEmptyString, std::invalid_argument );
LOGUNIT_TEST_EXCEPTION( testToIntRejectsWhitespaceOnly, std::invalid_argument );
LOGUNIT_TEST_EXCEPTION( testToIntRejectsEmbeddedWhitespace, std::invalid_argument );
LOGUNIT_TEST_EXCEPTION( testToIntRejectsTrailingCharacters, std::invalid_argument );
LOGUNIT_TEST_EXCEPTION( testToInt64RejectsTrailingCharacters, std::invalid_argument );
LOGUNIT_TEST( testFormatEmptyPattern );
LOGUNIT_TEST( testFormatMissingArgument );
LOGUNIT_TEST_SUITE_END();
Expand Down Expand Up @@ -131,6 +139,43 @@ LOGUNIT_CLASS(StringHelperTestCase)
LOGUNIT_ASSERT_EQUAL(false, StringHelper::startsWith(LOG4CXX_STR("foobar"), LOG4CXX_STR("abc")));
}

void testToIntAcceptsSign()
{
LOGUNIT_ASSERT_EQUAL(42, StringHelper::toInt(LOG4CXX_STR("+42")));
LOGUNIT_ASSERT_EQUAL(-42, StringHelper::toInt(LOG4CXX_STR("-42")));
}

void testToIntParsesWholeString()
{
LOGUNIT_ASSERT_EQUAL(42, StringHelper::toInt(LOG4CXX_STR(" 42 \t")));
LOGUNIT_ASSERT_EQUAL(int64_t(1234567890123LL), StringHelper::toInt64(LOG4CXX_STR("1234567890123")));
}

void testToIntRejectsEmptyString()
{
StringHelper::toInt(LOG4CXX_STR(""));
}

void testToIntRejectsWhitespaceOnly()
{
StringHelper::toInt(LOG4CXX_STR(" "));
}

void testToIntRejectsEmbeddedWhitespace()
{
StringHelper::toInt(LOG4CXX_STR("12 34"));
}

void testToIntRejectsTrailingCharacters()
{
StringHelper::toInt(LOG4CXX_STR("123abc"));
}

void testToInt64RejectsTrailingCharacters()
{
StringHelper::toInt64(LOG4CXX_STR("123abc"));
}

void testFormatEmptyPattern()
{
std::vector<LogString> params;
Expand Down
Loading