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>
Added trimSelf(). Rewritten trim() to use trimSelf().

View File

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

View File

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

View File

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