diff --git a/ChangeLog b/ChangeLog index 0e7e482e..03cfa004 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2008-02-24 Tatsuhiro Tsujikawa + + Rewritten itos and uitos using template. llitos and ullitos are + replaced with itos and uitos respectively. + * src/Util.{h, cc} + * test/UtilTest.cc + 2008-02-24 Tatsuhiro Tsujikawa Fixed the bug that DH key exchange sometimes fails due to bad handling diff --git a/src/DHTMessageTracker.cc b/src/DHTMessageTracker.cc index c2341ff5..02f3ccad 100644 --- a/src/DHTMessageTracker.cc +++ b/src/DHTMessageTracker.cc @@ -88,7 +88,7 @@ DHTMessageTracker::messageArrived(const Dictionary* d, SharedHandle message = _factory->createResponseMessage(entry->getMessageType(), d, targetNode); int64_t rtt = entry->getElapsedMillis(); - _logger->debug("RTT is %s", Util::llitos(rtt).c_str()); + _logger->debug("RTT is %s", Util::itos(rtt).c_str()); targetNode->updateRTT(rtt); SharedHandle callback = entry->getCallback(); return std::pair, SharedHandle >(message, callback); diff --git a/src/DHTQueryMessage.cc b/src/DHTQueryMessage.cc index 32340abe..1ded98ab 100644 --- a/src/DHTQueryMessage.cc +++ b/src/DHTQueryMessage.cc @@ -68,7 +68,7 @@ std::string DHTQueryMessage::toString() const return "dht query "+getMessageType()+ " TransactionID="+Util::toHex(_transactionID)+ " Remote:"+ - _remoteNode->getIPAddress()+":"+Util::itos(_remoteNode->getPort())+ + _remoteNode->getIPAddress()+":"+Util::uitos(_remoteNode->getPort())+ ", id="+Util::toHex(_remoteNode->getID(), DHT_ID_LENGTH)+ ", "+toStringOptional(); } diff --git a/src/DHTResponseMessage.cc b/src/DHTResponseMessage.cc index 055c4c7e..5a66daae 100644 --- a/src/DHTResponseMessage.cc +++ b/src/DHTResponseMessage.cc @@ -67,7 +67,7 @@ std::string DHTResponseMessage::toString() const return "dht response "+getMessageType()+ " TransactionID="+Util::toHex(_transactionID)+ " Remote:"+ - _remoteNode->getIPAddress()+":"+Util::itos(_remoteNode->getPort())+ + _remoteNode->getIPAddress()+":"+Util::uitos(_remoteNode->getPort())+ ", id="+Util::toHex(_remoteNode->getID(), DHT_ID_LENGTH)+ ", "+toStringOptional(); } diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index 11ae3a88..25be7720 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -143,9 +143,9 @@ std::string DefaultBtAnnounce::getAnnounceUrl() { "info_hash="+Util::torrentUrlencode(btContext->getInfoHash(), btContext->getInfoHashLength())+"&"+ "peer_id="+Util::torrentUrlencode(btContext->getPeerId(), 20)+"&"+ - "uploaded="+Util::llitos(stat.getSessionUploadLength())+"&"+ - "downloaded="+Util::llitos(stat.getSessionDownloadLength())+"&"+ - "left="+Util::llitos(left)+"&"+ + "uploaded="+Util::itos(stat.getSessionUploadLength())+"&"+ + "downloaded="+Util::itos(stat.getSessionDownloadLength())+"&"+ + "left="+Util::itos(left)+"&"+ "compact=1"+"&"+ "key="+key+"&"+ "numwant="+Util::itos(numWant)+"&"+ diff --git a/src/DefaultBtProgressInfoFile.cc b/src/DefaultBtProgressInfoFile.cc index 2c813dc5..0c11f33b 100644 --- a/src/DefaultBtProgressInfoFile.cc +++ b/src/DefaultBtProgressInfoFile.cc @@ -206,8 +206,8 @@ void DefaultBtProgressInfoFile::load() in.read(reinterpret_cast(&totalLength), sizeof(totalLength)); if(totalLength != _dctx->getTotalLength()) { throw new DlAbortEx("total length mismatch. expected: %s, actual: %s", - Util::llitos(_dctx->getTotalLength()).c_str(), - Util::llitos(totalLength).c_str()); + Util::itos(_dctx->getTotalLength()).c_str(), + Util::itos(totalLength).c_str()); } int64_t uploadLength; in.read(reinterpret_cast(&uploadLength), sizeof(uploadLength)); diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc index de8b4e34..e28f61b8 100644 --- a/src/DownloadCommand.cc +++ b/src/DownloadCommand.cc @@ -200,7 +200,7 @@ void DownloadCommand::validatePieceHash(const SegmentHandle& segment) } else { logger->info(EX_INVALID_CHUNK_CHECKSUM, segment->getIndex(), - Util::llitos(segment->getPosition(), true).c_str(), + Util::itos(segment->getPosition(), true).c_str(), expectedPieceHash.c_str(), actualPieceHash.c_str()); segment->clear(); diff --git a/src/FileAllocationCommand.cc b/src/FileAllocationCommand.cc index dda7923a..839af620 100644 --- a/src/FileAllocationCommand.cc +++ b/src/FileAllocationCommand.cc @@ -59,7 +59,7 @@ bool FileAllocationCommand::executeInternal() if(_fileAllocationEntry->finished()) { logger->debug(MSG_ALLOCATION_COMPLETED, _timer.difference(), - Util::llitos(_requestGroup->getTotalLength(), true).c_str()); + Util::itos(_requestGroup->getTotalLength(), true).c_str()); _e->_fileAllocationMan->markCurrentFileAllocationEntryDone(); _e->addCommand(_fileAllocationEntry->prepareForNextAction(_e)); diff --git a/src/FtpConnection.cc b/src/FtpConnection.cc index 14879f51..786c5768 100644 --- a/src/FtpConnection.cc +++ b/src/FtpConnection.cc @@ -128,7 +128,7 @@ SocketHandle FtpConnection::sendPort() const void FtpConnection::sendRest(const SegmentHandle& segment) const { - std::string request = "REST "+Util::llitos(segment->getPositionToWrite())+"\r\n"; + std::string request = "REST "+Util::itos(segment->getPositionToWrite())+"\r\n"; logger->info(MSG_SENDING_REQUEST, cuid, request.c_str()); socket->writeData(request); } diff --git a/src/FtpNegotiationCommand.cc b/src/FtpNegotiationCommand.cc index 074d474b..522f5700 100644 --- a/src/FtpNegotiationCommand.cc +++ b/src/FtpNegotiationCommand.cc @@ -215,7 +215,7 @@ bool FtpNegotiationCommand::recvSize() { throw new DlAbortEx(EX_BAD_STATUS, status); } if(size == INT64_MAX || size < 0) { - throw new DlAbortEx(EX_TOO_LARGE_FILE, Util::llitos(size, true).c_str()); + throw new DlAbortEx(EX_TOO_LARGE_FILE, Util::itos(size, true).c_str()); } if(_requestGroup->getPieceStorage().isNull()) { SingleFileDownloadContextHandle dctx = _requestGroup->getDownloadContext(); diff --git a/src/HandshakeExtensionMessage.cc b/src/HandshakeExtensionMessage.cc index fe1b5c97..b8ff0b96 100644 --- a/src/HandshakeExtensionMessage.cc +++ b/src/HandshakeExtensionMessage.cc @@ -65,7 +65,7 @@ std::string HandshakeExtensionMessage::getBencodedData() dic->put("v", v); } if(_tcpPort > 0) { - std::string portStr = Util::itos(_tcpPort); + std::string portStr = Util::uitos(_tcpPort); Data* p = new Data(portStr, true); dic->put("p", p); } @@ -89,7 +89,7 @@ std::string HandshakeExtensionMessage::toString() const s += " client="+Util::urlencode(_clientVersion); } if(_tcpPort > 0) { - s += ", tcpPort="+Util::itos(_tcpPort); + s += ", tcpPort="+Util::uitos(_tcpPort); } for(std::map::const_iterator itr = _extensions.begin(); itr != _extensions.end(); ++itr) { diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index 27e154c3..f98a0988 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -128,7 +128,7 @@ bool HttpRequest::isRangeSatisfied(const RangeHandle& range) const std::string HttpRequest::getHostText(const std::string& host, int32_t port) const { - return host+(port == 80 || port == 443 ? "" : ":"+Util::llitos(port)); + return host+(port == 80 || port == 443 ? "" : ":"+Util::itos(port)); } std::string HttpRequest::createRequest() const @@ -156,10 +156,10 @@ std::string HttpRequest::createRequest() const } if(!segment.isNull() && segment->getLength() > 0 && (request->isKeepAlive() || getStartByte() > 0)) { - requestLine += "Range: bytes="+Util::llitos(getStartByte()); + requestLine += "Range: bytes="+Util::itos(getStartByte()); requestLine += "-"; if(request->isKeepAlive()) { - requestLine += Util::llitos(getEndByte()); + requestLine += Util::itos(getEndByte()); } requestLine += "\r\n"; } diff --git a/src/HttpResponse.cc b/src/HttpResponse.cc index 99322b5b..55a12f67 100644 --- a/src/HttpResponse.cc +++ b/src/HttpResponse.cc @@ -80,12 +80,12 @@ void HttpResponse::validateResponse() const RangeHandle responseRange = httpHeader->getRange(); if(!httpRequest->isRangeSatisfied(responseRange)) { throw new DlAbortEx(EX_INVALID_RANGE_HEADER, - Util::llitos(httpRequest->getStartByte(), true).c_str(), - Util::llitos(httpRequest->getEndByte(), true).c_str(), - Util::llitos(httpRequest->getEntityLength(), true).c_str(), - Util::llitos(responseRange->getStartByte(), true).c_str(), - Util::llitos(responseRange->getEndByte(), true).c_str(), - Util::llitos(responseRange->getEntityLength(), true).c_str()); + Util::itos(httpRequest->getStartByte(), true).c_str(), + Util::itos(httpRequest->getEndByte(), true).c_str(), + Util::itos(httpRequest->getEntityLength(), true).c_str(), + Util::itos(responseRange->getStartByte(), true).c_str(), + Util::itos(responseRange->getEndByte(), true).c_str(), + Util::itos(responseRange->getEntityLength(), true).c_str()); } } } diff --git a/src/IteratableChunkChecksumValidator.cc b/src/IteratableChunkChecksumValidator.cc index f80cda74..5fa757af 100644 --- a/src/IteratableChunkChecksumValidator.cc +++ b/src/IteratableChunkChecksumValidator.cc @@ -87,7 +87,7 @@ void IteratableChunkChecksumValidator::validateChunk() } else { _logger->info(EX_INVALID_CHUNK_CHECKSUM, _currentIndex, - Util::llitos(getCurrentOffset(), true).c_str(), + Util::itos(getCurrentOffset(), true).c_str(), _dctx->getPieceHashes()[_currentIndex].c_str(), actualChecksum.c_str()); _bitfield->unsetBit(_currentIndex); diff --git a/src/MultiDiskAdaptor.cc b/src/MultiDiskAdaptor.cc index 9f245c72..223c33df 100644 --- a/src/MultiDiskAdaptor.cc +++ b/src/MultiDiskAdaptor.cc @@ -201,7 +201,7 @@ void MultiDiskAdaptor::writeData(const unsigned char* data, int32_t len, } } if(!writing) { - throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, Util::llitos(offset, true).c_str()); + throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, Util::itos(offset, true).c_str()); } } @@ -244,7 +244,7 @@ int32_t MultiDiskAdaptor::readData(unsigned char* data, int32_t len, int64_t off } } if(!reading) { - throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, Util::llitos(offset, true).c_str()); + throw new DlAbortEx(EX_FILE_OFFSET_OUT_OF_RANGE, Util::itos(offset, true).c_str()); } return totalReadLength; } diff --git a/src/OptionHandlerImpl.h b/src/OptionHandlerImpl.h index 683875e0..5925e3b4 100644 --- a/src/OptionHandlerImpl.h +++ b/src/OptionHandlerImpl.h @@ -89,7 +89,7 @@ public: int32_t v = seq.next(); if(v < _min || _max < v) { std::string msg = _optName+" "+_("must be between %s and %s."); - throw new FatalException(msg.c_str(), Util::llitos(_min).c_str(), Util::llitos(_max).c_str()); + throw new FatalException(msg.c_str(), Util::itos(_min).c_str(), Util::itos(_max).c_str()); } option->put(_optName, optarg); } @@ -114,18 +114,18 @@ public: void parseArg(Option* option, int64_t number) { if((_min == -1 || _min <= number) && (_max == -1 || number <= _max)) { - option->put(_optName, Util::llitos(number)); + option->put(_optName, Util::itos(number)); } else { std::string msg = _optName+" "; if(_min == -1 && _max != -1) { msg += _("must be smaller than or equal to %s."); - throw new FatalException(msg.c_str(), Util::llitos(_max).c_str()); + throw new FatalException(msg.c_str(), Util::itos(_max).c_str()); } else if(_min != -1 && _max != -1) { msg += _("must be between %s and %s."); - throw new FatalException(msg.c_str(), Util::llitos(_min).c_str(), Util::llitos(_max).c_str()); + throw new FatalException(msg.c_str(), Util::itos(_min).c_str(), Util::itos(_max).c_str()); } else if(_min != -1 && _max == -1) { msg += _("must be greater than or equal to %s."); - throw new FatalException(msg.c_str(), Util::llitos(_min).c_str()); + throw new FatalException(msg.c_str(), Util::itos(_min).c_str()); } else { msg += _("must be a number."); throw new FatalException(msg.c_str()); diff --git a/src/Peer.cc b/src/Peer.cc index 793297b3..cecc2d8e 100644 --- a/src/Peer.cc +++ b/src/Peer.cc @@ -53,7 +53,7 @@ Peer::Peer(std::string ipaddr, uint16_t port): _res(0) { resetStatus(); - std::string idSeed = ipaddr+":"+Util::itos(port); + std::string idSeed = ipaddr+":"+Util::uitos(port); #ifdef ENABLE_MESSAGE_DIGEST id = MessageDigestHelper::digestString("sha1", idSeed); #else diff --git a/src/RequestGroup.cc b/src/RequestGroup.cc index f4cedd49..d3a44aaf 100644 --- a/src/RequestGroup.cc +++ b/src/RequestGroup.cc @@ -360,8 +360,8 @@ void RequestGroup::loadAndOpenFile(const BtProgressInfoFileHandle& progressInfoF if(getTotalLength() < outfile.size()) { throw new DlAbortEx(EX_FILE_LENGTH_MISMATCH_BETWEEN_LOCAL_AND_REMOTE, getFilePath().c_str(), - Util::llitos(outfile.size()).c_str(), - Util::llitos(getTotalLength()).c_str()); + Util::itos(outfile.size()).c_str(), + Util::itos(getTotalLength()).c_str()); } _pieceStorage->getDiskAdaptor()->openExistingFile(); _pieceStorage->markPiecesDone(outfile.size()); @@ -514,8 +514,8 @@ void RequestGroup::validateTotalLength(int64_t expectedTotalLength, } if(expectedTotalLength != actualTotalLength) { throw new DlAbortEx(EX_SIZE_MISMATCH, - Util::llitos(expectedTotalLength, true).c_str(), - Util::llitos(actualTotalLength, true).c_str()); + Util::itos(expectedTotalLength, true).c_str(), + Util::itos(actualTotalLength, true).c_str()); } } diff --git a/src/Util.cc b/src/Util.cc index 99890511..71b69ae1 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -59,74 +59,6 @@ namespace aria2 { -template -std::string uint2str(T value, bool comma) -{ - std::string str; - if(value == 0) { - str = "0"; - return str; - } - int32_t count = 0; - while(value) { - ++count; - char digit = value%10+'0'; - str.insert(str.begin(), digit); - value /= 10; - if(comma && count > 3 && count%3 == 1) { - str.insert(str.begin()+1, ','); - } - } - return str; -} - -template -std::string int2str(T value, bool comma) -{ - bool flag = false; - if(value < 0) { - flag = true; - value = -value; - } - std::string str = uint2str(value, comma); - if(flag) { - str.insert(str.begin(), '-'); - } - return str; -} - - - -std::string Util::uitos(uint16_t value, bool comma) -{ - return uint2str(value, comma); -} - -std::string Util::itos(int16_t value, bool comma) -{ - return int2str(value, comma); -} - -std::string Util::uitos(uint32_t value, bool comma) -{ - return uint2str(value, comma); -} - -std::string Util::itos(int32_t value, bool comma) -{ - return int2str(value, comma); -} - -std::string Util::ullitos(uint64_t value, bool comma) -{ - return uint2str(value, comma); -} - -std::string Util::llitos(int64_t value, bool comma) -{ - return int2str(value, comma); -} - std::string Util::trim(const std::string& src, const std::string& trimCharset) { std::string::size_type sp = src.find_first_not_of(trimCharset); @@ -674,7 +606,7 @@ int64_t Util::getRealSize(const std::string& sizeWithUnit) std::string Util::abbrevSize(int64_t size) { if(size < 1024) { - return Util::llitos(size, true); + return Util::itos(size, true); } char units[] = { 'K', 'M' }; int32_t numUnit = sizeof(units)/sizeof(char); @@ -685,7 +617,7 @@ std::string Util::abbrevSize(int64_t size) r = size&0x3ff; size >>= 10; } - return Util::llitos(size, true)+"."+Util::itos(r*10/1024)+units[i]+"i"; + return Util::itos(size, true)+"."+Util::itos(r*10/1024)+units[i]+"i"; } time_t Util::httpGMT(const std::string& httpStdTime) diff --git a/src/Util.h b/src/Util.h index 41de9287..bb045ff0 100644 --- a/src/Util.h +++ b/src/Util.h @@ -66,12 +66,41 @@ public: static std::pair split(const std::string& src, const std::string& delims); - static std::string llitos(int64_t value, bool comma = false); - static std::string ullitos(uint64_t value, bool comma = false); - static std::string itos(int32_t value, bool comma = false); - static std::string uitos(uint32_t value, bool comma = false); - static std::string itos(int16_t value, bool comma = false); - static std::string uitos(uint16_t value, bool comma = false); + template + static std::string uitos(T value, bool comma = false) + { + std::string str; + if(value == 0) { + str = "0"; + return str; + } + int32_t count = 0; + while(value) { + ++count; + char digit = value%10+'0'; + str.insert(str.begin(), digit); + value /= 10; + if(comma && count > 3 && count%3 == 1) { + str.insert(str.begin()+1, ','); + } + } + return str; + } + + template + static std::string itos(T value, bool comma = false) + { + bool flag = false; + if(value < 0) { + flag = true; + value = -value; + } + std::string str = uitos(value, comma); + if(flag) { + str.insert(str.begin(), '-'); + } + return str; + } /** * Computes difference in micro-seconds between tv1 and tv2, diff --git a/test/UtilTest.cc b/test/UtilTest.cc index 37caba7b..b6c46702 100644 --- a/test/UtilTest.cc +++ b/test/UtilTest.cc @@ -41,6 +41,8 @@ class UtilTest:public CppUnit::TestFixture { CPPUNIT_TEST(testParseInt); CPPUNIT_TEST(testParseLLInt); CPPUNIT_TEST(testToString_binaryStream); + CPPUNIT_TEST(testItos); + CPPUNIT_TEST(testUitos); CPPUNIT_TEST_SUITE_END(); private: @@ -75,6 +77,8 @@ public: void testParseInt(); void testParseLLInt(); void testToString_binaryStream(); + void testItos(); + void testUitos(); }; @@ -597,4 +601,48 @@ void UtilTest::testToString_binaryStream() CPPUNIT_ASSERT_EQUAL(data, readData); } +void UtilTest::testItos() +{ + { + int32_t i = 0; + CPPUNIT_ASSERT_EQUAL(std::string("0"), Util::itos(i)); + } + { + int32_t i = 100; + CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i, true)); + } + { + int32_t i = 100; + CPPUNIT_ASSERT_EQUAL(std::string("100"), Util::itos(i)); + } + { + int32_t i = 12345; + CPPUNIT_ASSERT_EQUAL(std::string("12,345"), Util::itos(i, true)); + } + { + int32_t i = 12345; + CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::itos(i)); + } + { + int32_t i = -12345; + CPPUNIT_ASSERT_EQUAL(std::string("-12,345"), Util::itos(i, true)); + } + { + int64_t i = INT64_MAX; + CPPUNIT_ASSERT_EQUAL(std::string("9,223,372,036,854,775,807"), Util::itos(i, true)); + } +} + +void UtilTest::testUitos() +{ + { + uint16_t i = 12345; + CPPUNIT_ASSERT_EQUAL(std::string("12345"), Util::uitos(i)); + } + { + int16_t i = -12345; + CPPUNIT_ASSERT_EQUAL(std::string("/.-,+"), Util::uitos(i)); + } +} + } // namespace aria2