carnold 2005/03/09 08:57:02
Modified: src stringhelper.cpp
Added: tests/src/helpers stringhelpertestcase.cpp
Log:
LOGCXX-70: StringHelper::startsWith and endsWith logic errors
Revision Changes Path
1.18 +9 -18 logging-log4cxx/src/stringhelper.cpp
Index: stringhelper.cpp
===================================================================
RCS file: /home/cvs/logging-log4cxx/src/stringhelper.cpp,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- stringhelper.cpp 5 Mar 2005 05:36:58 -0000 1.17
+++ stringhelper.cpp 9 Mar 2005 16:57:02 -0000 1.18
@@ -75,31 +75,22 @@
}
-bool StringHelper::startsWith(const std::wstring& teststr, const
std::wstring& substr)
- {
- bool val = false;
- if(teststr.length() > substr.length()) {
- val = teststr.substr(0, substr.length()) == substr;
- }
-
- return val;
+bool StringHelper::startsWith(const std::string& s, const std::string&
prefix)
+{
+ return s.compare(0, prefix.length(), prefix) == 0;
}
-bool StringHelper::startsWith(const std::string& teststr, const std::string&
substr)
- {
- bool val = false;
- if(teststr.length() > substr.length()) {
- val = teststr.substr(0, substr.length()) == substr;
- }
- return val;
+bool StringHelper::startsWith(const std::wstring& s, const std::wstring&
prefix)
+{
+ return s.compare(0, prefix.length(), prefix) == 0;
}
bool StringHelper::endsWith(const std::string& s, const std::string& suffix)
{
if (suffix.length() <= s.length()) {
- return suffix.compare(s.length() - suffix.length(), suffix.length(),
s) == 0;
+ return s.compare(s.length() - suffix.length(), suffix.length(),
suffix) == 0;
}
return false;
}
@@ -108,7 +99,7 @@
bool StringHelper::endsWith(const std::wstring& s, const std::wstring&
suffix)
{
if (suffix.length() <= s.length()) {
- return suffix.compare(s.length() - suffix.length(), suffix.length(),
s) == 0;
+ return s.compare(s.length() - suffix.length(), suffix.length(),
suffix) == 0;
}
return false;
}
@@ -270,7 +261,7 @@
LogString s(width, LOG4CXX_STR('x'));
s[0] = LOG4CXX_STR('0');
for(int i = width - 1; i >= 2; i--) {
- s[i] = hexdigits[iptr % 0x0F];
+ s[i] = hexdigits[iptr & 0x0F];
iptr = iptr >> 4;
}
return s;
1.1
logging-log4cxx/tests/src/helpers/stringhelpertestcase.cpp
Index: stringhelpertestcase.cpp
===================================================================
/*
* Copyright 2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <log4cxx/helpers/stringhelper.h>
#include <cppunit/extensions/HelperMacros.h>
#include "../insertwide.h"
using namespace log4cxx;
using namespace log4cxx::helpers;
/**
Unit test for StringHelper.
@author Curt Arnold
@since 0.9.8
*/
class StringHelperTestCase : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( StringHelperTestCase );
CPPUNIT_TEST( testWideStartsWith1 );
CPPUNIT_TEST( testWideStartsWith2 );
CPPUNIT_TEST( testWideStartsWith3 );
CPPUNIT_TEST( testWideStartsWith4 );
CPPUNIT_TEST( testWideStartsWith5 );
CPPUNIT_TEST( testStartsWith1 );
CPPUNIT_TEST( testStartsWith2 );
CPPUNIT_TEST( testStartsWith3 );
CPPUNIT_TEST( testStartsWith4 );
CPPUNIT_TEST( testStartsWith5 );
CPPUNIT_TEST( testWideEndsWith1 );
CPPUNIT_TEST( testWideEndsWith2 );
CPPUNIT_TEST( testWideEndsWith3 );
CPPUNIT_TEST( testWideEndsWith4 );
CPPUNIT_TEST( testWideEndsWith5 );
CPPUNIT_TEST( testEndsWith1 );
CPPUNIT_TEST( testEndsWith2 );
CPPUNIT_TEST( testEndsWith3 );
CPPUNIT_TEST( testEndsWith4 );
CPPUNIT_TEST( testEndsWith5 );
CPPUNIT_TEST_SUITE_END();
public:
/**
* Check that startsWith("foobar", "foo") returns true.
*/
void testWideStartsWith1() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::startsWith(L"foobar", L"foo"));
}
/**
* Check that startsWith("foo", "foobar") returns false.
*/
void testWideStartsWith2() {
CPPUNIT_ASSERT_EQUAL(false, StringHelper::startsWith(L"foo", L"foobar"));
}
/**
* Check that startsWith("foobar", "foobar") returns true.
*/
void testWideStartsWith3() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::startsWith(L"foobar", L"foobar"));
}
/**
* Check that startsWith("foobar", "") returns true.
*/
void testWideStartsWith4() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::startsWith(L"foobar", L""));
}
/**
* Check that startsWith("foobar", "abc") returns false.
*/
void testWideStartsWith5() {
CPPUNIT_ASSERT_EQUAL(false, StringHelper::startsWith(L"foobar", L"abc"));
}
/**
* Check that startsWith("foobar", "foo") returns true.
*/
void testStartsWith1() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::startsWith("foobar", "foo"));
}
/**
* Check that startsWith("bar", "foobar") returns false.
*/
void testStartsWith2() {
CPPUNIT_ASSERT_EQUAL(false, StringHelper::startsWith("foo", "foobar"));
}
/**
* Check that startsWith("foobar", "foobar") returns true.
*/
void testStartsWith3() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::startsWith("foobar", "foobar"));
}
/**
* Check that startsWith("foobar", "") returns true.
*/
void testStartsWith4() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::startsWith("foobar", ""));
}
/**
* Check that startsWith("foobar", "abc") returns false.
*/
void testStartsWith5() {
CPPUNIT_ASSERT_EQUAL(false, StringHelper::startsWith("foobar", "abc"));
}
/**
* Check that endsWith("foobar", "bar") returns true.
*/
void testWideEndsWith1() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::endsWith(L"foobar", L"bar"));
}
/**
* Check that endsWith("bar", "foobar") returns false.
*/
void testWideEndsWith2() {
CPPUNIT_ASSERT_EQUAL(false, StringHelper::endsWith(L"bar", L"foobar"));
}
/**
* Check that endsWith("foobar", "foobar") returns true.
*/
void testWideEndsWith3() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::endsWith(L"foobar", L"foobar"));
}
/**
* Check that endsWith("foobar", "") returns true.
*/
void testWideEndsWith4() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::endsWith(L"foobar", L""));
}
/**
* Check that endsWith("foobar", "abc") returns false.
*/
void testWideEndsWith5() {
CPPUNIT_ASSERT_EQUAL(false, StringHelper::startsWith(L"foobar", L"abc"));
}
/**
* Check that endsWith("foobar", "bar") returns true.
*/
void testEndsWith1() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::endsWith("foobar", "bar"));
}
/**
* Check that endsWith("bar", "foobar") returns false.
*/
void testEndsWith2() {
CPPUNIT_ASSERT_EQUAL(false, StringHelper::endsWith("bar", "foobar"));
}
/**
* Check that endsWith("foobar", "foobar") returns true.
*/
void testEndsWith3() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::endsWith("foobar", "foobar"));
}
/**
* Check that endsWith("foobar", "") returns true.
*/
void testEndsWith4() {
CPPUNIT_ASSERT_EQUAL(true, StringHelper::endsWith("foobar", ""));
}
/**
* Check that endsWith("foobar", "abc") returns false.
*/
void testEndsWith5() {
CPPUNIT_ASSERT_EQUAL(false, StringHelper::startsWith("foobar", "abc"));
}
};
CPPUNIT_TEST_SUITE_REGISTRATION(StringHelperTestCase);