2008-05-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

Refactored. Use std::string instead of size and char*.
	* Data.cc
	* Data.h
	* test/Data.cc
pull/1/head
Tatsuhiro Tsujikawa 2008-05-21 07:18:31 +00:00
parent ad6ffd7f33
commit 65c7d8f26c
4 changed files with 36 additions and 42 deletions

View File

@ -1,3 +1,10 @@
2008-05-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Refactored. Use std::string instead of size and char*.
* Data.cc
* Data.h
* test/Data.cc
2008-05-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com> 2008-05-21 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Added trimSelf(). Rewritten trim() to use trimSelf(). Added trimSelf(). Rewritten trim() to use trimSelf().

View File

@ -39,50 +39,32 @@
namespace aria2 { namespace aria2 {
Data::Data(const unsigned char* data, size_t len, bool number):number(number) Data::Data(const unsigned char* data, size_t len, bool number):
{ _data(reinterpret_cast<const char*>(data), len),
init(data, len); number(number) {}
}
Data::Data(const char* data, size_t len, bool number):number(number) { Data::Data(const char* data, size_t len, bool number):
init(reinterpret_cast<const unsigned char*>(data), len); _data(data, len),
} number(number) {}
Data::Data(const std::string& data, bool number):number(number) Data::Data(const std::string& data, bool number):_data(data), number(number) {}
{
init(reinterpret_cast<const unsigned char*>(data.c_str()), data.size());
}
void Data::init(const unsigned char* src, size_t slen) Data::~Data() {}
{
if(src) {
data = new unsigned char[slen];
memcpy(data, src, slen);
len = slen;
} else {
data = 0;
len = 0;
}
}
Data::~Data() { const std::string& Data::toString() const {
delete [] data; return _data;
}
std::string Data::toString() const {
return std::string(&data[0], &data[len]);
} }
const unsigned char* Data::getData() const { const unsigned char* Data::getData() const {
if(this->len == 0) { if(_data.empty()) {
return NULL; return 0;
} else { } else {
return data; return reinterpret_cast<const unsigned char*>(_data.c_str());
} }
} }
size_t Data::getLen() const { size_t Data::getLen() const {
return len; return _data.size();
} }
int32_t Data::toInt() const { int32_t Data::toInt() const {
@ -90,11 +72,7 @@ int32_t Data::toInt() const {
} }
int64_t Data::toLLInt() const { int64_t Data::toLLInt() const {
if(len == 0) { return strtoll(_data.c_str(), 0, 10);
return 0;
} else {
return strtoll(toString().c_str(), NULL, 10);
}
} }
bool Data::isNumber() const { bool Data::isNumber() const {

View File

@ -42,8 +42,7 @@ namespace aria2 {
class Data : public MetaEntry { class Data : public MetaEntry {
private: private:
size_t len; std::string _data;
unsigned char* data;
bool number; bool number;
void init(const unsigned char* data, size_t len); void init(const unsigned char* data, size_t len);
@ -58,9 +57,9 @@ public:
Data(const std::string& data, bool number = false); Data(const std::string& data, bool number = false);
~Data(); virtual ~Data();
std::string toString() const; const std::string& toString() const;
int32_t toInt() const; int32_t toInt() const;
int64_t toLLInt() const; int64_t toLLInt() const;

View File

@ -1,4 +1,5 @@
#include "Data.h" #include "Data.h"
#include "A2STR.h"
#include <cstring> #include <cstring>
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
@ -32,7 +33,10 @@ void DataTest::testToString() {
CPPUNIT_ASSERT_EQUAL(std::string("aria2"), data.toString()); CPPUNIT_ASSERT_EQUAL(std::string("aria2"), data.toString());
Data null(reinterpret_cast<const char*>(0), 0); Data null(reinterpret_cast<const char*>(0), 0);
CPPUNIT_ASSERT_EQUAL(std::string(""), null.toString()); CPPUNIT_ASSERT_EQUAL(A2STR::NIL, null.toString());
Data zeroLengthString(A2STR::NIL);
CPPUNIT_ASSERT_EQUAL(A2STR::NIL, zeroLengthString.toString());
} }
void DataTest::testGetData() { void DataTest::testGetData() {
@ -44,6 +48,9 @@ void DataTest::testGetData() {
CPPUNIT_ASSERT(null.getData() == 0); CPPUNIT_ASSERT(null.getData() == 0);
CPPUNIT_ASSERT_EQUAL((size_t)0, null.getLen()); CPPUNIT_ASSERT_EQUAL((size_t)0, null.getLen());
Data zeroLengthString(A2STR::NIL);
CPPUNIT_ASSERT(zeroLengthString.getData() == 0);
CPPUNIT_ASSERT_EQUAL((size_t)0, zeroLengthString.getLen());
} }
void DataTest::testToInt() { void DataTest::testToInt() {
@ -55,6 +62,9 @@ void DataTest::testToInt() {
Data alpha("abc", 3); Data alpha("abc", 3);
CPPUNIT_ASSERT_EQUAL((int32_t)0, alpha.toInt()); CPPUNIT_ASSERT_EQUAL((int32_t)0, alpha.toInt());
Data zeroLengthString("");
CPPUNIT_ASSERT_EQUAL(0, (int)zeroLengthString.toLLInt());
} }
void DataTest::testToLLInt() { void DataTest::testToLLInt() {