You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4cxx-dev@logging.apache.org by ca...@apache.org on 2004/10/25 19:05:09 UTC

cvs commit: logging-log4cxx/tests/src/helpers stringtokenizertestcase.cpp

carnold     2004/10/25 10:05:09

  Modified:    include/log4cxx/helpers stringtokenizer.h
               src      stringtokenizer.cpp
  Added:       tests/src/helpers stringtokenizertestcase.cpp
  Log:
  LOGCXX-30: StringTokenizer uses evil strtok function
  
  Revision  Changes    Path
  1.8       +3 -4      logging-log4cxx/include/log4cxx/helpers/stringtokenizer.h
  
  Index: stringtokenizer.h
  ===================================================================
  RCS file: /home/cvs/logging-log4cxx/include/log4cxx/helpers/stringtokenizer.h,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- stringtokenizer.h	22 Oct 2004 17:35:29 -0000	1.7
  +++ stringtokenizer.h	25 Oct 2004 17:05:09 -0000	1.8
  @@ -37,10 +37,9 @@
   			String nextToken();
   
   		protected:
  -			TCHAR * str;
  -			String delim;
  -			TCHAR * token;
  -			TCHAR * state;
  +                        String src;
  +                        String delim;
  +                        size_t pos;
   
                   private:
                           //   prevent copy and assignment statements
  
  
  
  1.7       +18 -38    logging-log4cxx/src/stringtokenizer.cpp
  
  Index: stringtokenizer.cpp
  ===================================================================
  RCS file: /home/cvs/logging-log4cxx/src/stringtokenizer.cpp,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- stringtokenizer.cpp	13 Aug 2004 12:27:48 -0000	1.6
  +++ stringtokenizer.cpp	25 Oct 2004 17:05:09 -0000	1.7
  @@ -1,70 +1,50 @@
   /*
    * Copyright 2003,2004 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/stringtokenizer.h>
   
   using namespace log4cxx;
   using namespace log4cxx::helpers;
   
   StringTokenizer::StringTokenizer(const String& str, const String& delim)
  -: delim(delim), state(0)
  +: src(str), delim(delim), pos(0)
   {
  -	this->str = new TCHAR[str.length() + 1];
  -
  -#ifdef LOG4CXX_UNICODE
  -	wcscpy(this->str, str.c_str());
  -#if defined(WIN32) || defined(_WIN32)
  -	token = wcstok(this->str, this->delim.c_str());
  -#else
  -	token = wcstok(this->str, this->delim.c_str(), &state);
  -#endif
  -#else
  -	strcpy(this->str, str.c_str());
  -	token = strtok(this->str, this->delim.c_str());
  -#endif
   }
   
   StringTokenizer::~StringTokenizer()
   {
  -	delete this->str;
   }
   
   bool StringTokenizer::hasMoreTokens() const
   {
  -	return (token != 0);
  +        return (pos != String::npos
  +            && src.find_first_not_of(delim, pos) != String::npos);
   }
   
   String StringTokenizer::nextToken()
   {
  -	if (token == 0)
  -	{
  -		throw NoSuchElementException();
  -	}
  -
  -	String currentToken = token;
  -
  -#ifdef LOG4CXX_UNICODE
  -#if defined(WIN32) || defined(_WIN32)
  -	token = wcstok(0, delim.c_str());
  -#else
  -	token = wcstok(0, delim.c_str(), &state);
  -#endif
  -#else
  -	token = strtok(0, delim.c_str());
  -#endif
  -
  -	return currentToken;
  +        if (pos != String::npos) {
  +            size_t nextPos = src.find_first_not_of(delim, pos);
  +            if (nextPos != String::npos) {
  +               pos = src.find_first_of(delim, nextPos);
  +               if (pos == String::npos) {
  +                 return src.substr(nextPos);
  +               }
  +               return src.substr(nextPos, pos - nextPos);
  +            }
  +        }
  +        throw NoSuchElementException();
   }
  
  
  
  1.1                  logging-log4cxx/tests/src/helpers/stringtokenizertestcase.cpp
  
  Index: stringtokenizertestcase.cpp
  ===================================================================
  /*
   * Copyright 2004 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/stringtokenizer.h>
  #include <cppunit/TestFixture.h>
  #include <cppunit/extensions/HelperMacros.h>
  
  
  using namespace log4cxx;
  using namespace log4cxx::helpers;
  
  class StringTokenizerTestCase : public CppUnit::TestFixture
  {
  	CPPUNIT_TEST_SUITE(StringTokenizerTestCase);
  		CPPUNIT_TEST(testNextTokenEmptyString);
                  CPPUNIT_TEST(testHasMoreTokensEmptyString);
                  CPPUNIT_TEST(testNextTokenAllDelim);
                  CPPUNIT_TEST(testHasMoreTokensAllDelim);
                  CPPUNIT_TEST(test1);
                  CPPUNIT_TEST(test2);
                  CPPUNIT_TEST(test3);
                  CPPUNIT_TEST(test4);
                  CPPUNIT_TEST(test5);
                  CPPUNIT_TEST(test6);
  	CPPUNIT_TEST_SUITE_END();
  
  public:
          void testNextTokenEmptyString() {
             String src;
             String delim(" ");
             StringTokenizer tokenizer(src, delim);
             try {
               String token(tokenizer.nextToken());
             } catch (NoSuchElementException &ex) {
               return;
             }
             CPPUNIT_ASSERT(false);
          }
  
          void testHasMoreTokensEmptyString() {
             String src;
             String delim(" ");
             StringTokenizer tokenizer(src, delim);
             CPPUNIT_ASSERT_EQUAL(false, tokenizer.hasMoreTokens());
          }
  
          void testNextTokenAllDelim() {
             String src("===");
             String delim("=");
             StringTokenizer tokenizer(src, delim);
             try {
               String token(tokenizer.nextToken());
             } catch (NoSuchElementException &ex) {
               return;
             }
             CPPUNIT_ASSERT(false);
          }
  
          void testHasMoreTokensAllDelim() {
             String src("===");
             String delim("=");
             StringTokenizer tokenizer(src, delim);
             CPPUNIT_ASSERT_EQUAL(false, tokenizer.hasMoreTokens());
          }
  
          void testBody(const String& src, const String& delim) {
             StringTokenizer tokenizer(src, delim);
             CPPUNIT_ASSERT_EQUAL(true, tokenizer.hasMoreTokens());
             CPPUNIT_ASSERT_EQUAL((String) "log4j", tokenizer.nextToken());
             CPPUNIT_ASSERT_EQUAL(true, tokenizer.hasMoreTokens());
             CPPUNIT_ASSERT_EQUAL((String) "properties", tokenizer.nextToken());
             CPPUNIT_ASSERT_EQUAL(true, tokenizer.hasMoreTokens());
             CPPUNIT_ASSERT_EQUAL((String) "txt", tokenizer.nextToken());
             CPPUNIT_ASSERT_EQUAL(false, tokenizer.hasMoreTokens());
             try {
                String token(tokenizer.nextToken());
             } catch (NoSuchElementException& ex) {
               return;
             }
             CPPUNIT_ASSERT(false);
          }
  
          void test1() {
            String src("log4j.properties.txt");
            String delim(".");
            testBody(src, delim);
          }
  
          void test2() {
            String src(".log4j.properties.txt");
            String delim(".");
            testBody(src, delim);
          }
  
          void test3() {
            String src("log4j.properties.txt.");
            String delim(".");
            testBody(src, delim);
          }
  
          void test4() {
            String src("log4j..properties....txt");
            String delim(".");
            testBody(src, delim);
          }
  
          void test5() {
            String src("log4j properties,txt");
            String delim(" ,");
            testBody(src, delim);
          }
  
          void test6() {
             String src(" log4j properties,txt ");
             String delim(" ,");
             testBody(src, delim);
          }
  
  };
  
  CPPUNIT_TEST_SUITE_REGISTRATION(StringTokenizerTestCase);