Don't append character to std::string.

pull/4/head
Tatsuhiro Tsujikawa 2011-11-12 21:27:24 +09:00
parent 5347efb967
commit db5cc4db27
11 changed files with 84 additions and 68 deletions

View File

@ -311,16 +311,12 @@ std::string createSockPoolKey
std::string key;
if(!username.empty()) {
key += util::percentEncode(username);
key += '@';
key += "@";
}
key += host;
key += A2STR::COLON_C;
key += util::uitos(port);
key += fmt(":%u", port);
if(!proxyhost.empty()) {
key += A2STR::SLASH_C;
key += proxyhost;
key += A2STR::COLON_C;
key += util::uitos(proxyport);
key += fmt("/%s:%u", proxyhost.c_str(), proxyport);
}
return key;
}

View File

@ -208,7 +208,7 @@ void HttpHeader::fill
std::string::const_iterator> p = util::stripIter(first, j);
if(!name.empty() && p.first != p.second) {
if(!value.empty()) {
value += ' ';
value += " ";
}
value.append(p.first, p.second);
}

View File

@ -313,7 +313,7 @@ std::string HttpRequest::createProxyRequest() const
std::pair<std::string, std::string> HttpRequest::getProxyAuthString() const
{
std::string authText = proxyRequest_->getUsername();
authText += ':';
authText += ":";
authText += proxyRequest_->getPassword();
std::string val = "Basic ";
val += base64::encode(authText.begin(), authText.end());

View File

@ -61,28 +61,32 @@ std::string encode(InputIterator first, InputIterator last)
}
size_t r = len%3;
InputIterator j = last-r;
char temp[4];
while(first != j) {
int n = static_cast<unsigned char>(*first++) << 16;
n += static_cast<unsigned char>(*first++) << 8;
n += static_cast<unsigned char>(*first++);
res += CHAR_TABLE[n >> 18];
res += CHAR_TABLE[(n >> 12) & 0x3fu];
res += CHAR_TABLE[(n >> 6) & 0x3fu];
res += CHAR_TABLE[n & 0x3fu];
temp[0] = CHAR_TABLE[n >> 18];
temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu];
temp[3] = CHAR_TABLE[n & 0x3fu];
res.append(temp, sizeof(temp));
}
if(r == 2) {
int n = static_cast<unsigned char>(*first++) << 16;
n += static_cast<unsigned char>(*first++) << 8;
res += CHAR_TABLE[n >> 18];
res += CHAR_TABLE[(n >> 12) & 0x3fu];
res += CHAR_TABLE[(n >> 6) & 0x3fu];
res += '=';
temp[0] = CHAR_TABLE[n >> 18];
temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
temp[2] = CHAR_TABLE[(n >> 6) & 0x3fu];
temp[3] = '=';
res.append(temp, sizeof(temp));
} else if(r == 1) {
int n = static_cast<unsigned char>(*first++) << 16;
res += CHAR_TABLE[n >> 18];
res += CHAR_TABLE[(n >> 12) & 0x3fu];
res += '=';
res += '=';
temp[0] = CHAR_TABLE[n >> 18];
temp[1] = CHAR_TABLE[(n >> 12) & 0x3fu];
temp[2] = '=';
temp[3] = '=';
res.append(temp, sizeof(temp));
}
return res;
}

View File

@ -275,14 +275,14 @@ void extractFileEntries
error_code::BITTORRENT_PARSE_ERROR);
}
}
std::string utf8Path = strjoin(pathelem.begin(), pathelem.end(), '/',
std::string utf8Path = strjoin(pathelem.begin(), pathelem.end(), "/",
std::ptr_fun(util::encodeNonUtf8));
if(util::detectDirTraversal(utf8Path)) {
throw DL_ABORT_EX2(fmt(MSG_DIR_TRAVERSAL_DETECTED, utf8Path.c_str()),
error_code::BITTORRENT_PARSE_ERROR);
}
std::string pePath =
strjoin(pathelem.begin(), pathelem.end(), '/',
strjoin(pathelem.begin(), pathelem.end(), "/",
std::ptr_fun(static_cast<std::string (*)(const std::string&)>
(util::percentEncode)));
std::vector<std::string> uris;

View File

@ -168,12 +168,12 @@ decodeString
checkEof(first, last);
uint16_t codepoint = util::parseUInt(std::string(uchars, first), 16);
if(codepoint <= 0x007fu) {
s += static_cast<char>(codepoint);
unsigned char temp[1] = { static_cast<char>(codepoint) };
s.append(&temp[0], &temp[sizeof(temp)]);
} else if(codepoint <= 0x07ffu) {
unsigned char c2 = 0x80u | (codepoint & 0x003fu);
unsigned char c1 = 0xC0u | (codepoint >> 6);
s += c1;
s += c2;
unsigned char temp[2] = { 0xC0u | (codepoint >> 6),
0x80u | (codepoint & 0x003fu) };
s.append(&temp[0], &temp[sizeof(temp)]);
} else if(in(codepoint, 0xD800u, 0xDBFFu)) {
// surrogate pair
if(*first != '\\' || first+1 == last ||
@ -195,36 +195,31 @@ decodeString
uint32_t fullcodepoint = 0x010000u;
fullcodepoint += (codepoint & 0x03FFu) << 10;
fullcodepoint += (codepoint2 & 0x03FFu);
unsigned char c4 = 0x80u | (fullcodepoint & 0x003Fu);
unsigned char c3 = 0x80u | ((fullcodepoint >> 6) & 0x003Fu);
unsigned char c2 = 0x80u | ((fullcodepoint >> 12) & 0x003Fu);
unsigned char c1 = 0xf0u | (fullcodepoint >> 18);
s += c1;
s += c2;
s += c3;
s += c4;
unsigned char temp[4] = { 0xf0u | (fullcodepoint >> 18),
0x80u | ((fullcodepoint >> 12) & 0x003Fu),
0x80u | ((fullcodepoint >> 6) & 0x003Fu),
0x80u | (fullcodepoint & 0x003Fu) };
s.append(&temp[0], &temp[sizeof(temp)]);
} else {
unsigned char c3 = 0x80u | (codepoint & 0x003Fu);
unsigned char c2 = 0x80u | ((codepoint >> 6) & 0x003Fu);
unsigned char c1 = 0xE0u | (codepoint >> 12);
s += c1;
s += c2;
s += c3;
unsigned char temp[3] = { 0xE0u | (codepoint >> 12),
0x80u | ((codepoint >> 6) & 0x003Fu),
0x80u | (codepoint & 0x003Fu) };
s.append(&temp[0], &temp[sizeof(temp)]);
}
offset = first;
} else {
if(*first == 'b') {
s += '\b';
s += "\b";
} else if(*first == 'f') {
s += '\f';
s += "\f";
} else if(*first == 'n') {
s += '\n';
s += "\n";
} else if(*first == 'r') {
s += '\r';
s += "\r";
} else if(*first == 't') {
s += '\t';
s += "\t";
} else {
s += *first;
s.append(first, first+1);
}
++first;
offset = first;
@ -278,7 +273,7 @@ decodeNumber
{
std::string s;
if(*first == '-') {
s += *first;
s.append(first, first+1);
++first;
}
std::string::const_iterator offset = first;
@ -292,7 +287,7 @@ decodeNumber
bool fp = false;
if(*first == '.') {
fp = true;
s += *first;
s.append(first, first+1);
++first;
offset = first;
while(first != last && in(*first, '0', '9')) {
@ -304,11 +299,11 @@ decodeNumber
}
if(*first == 'e') {
fp = true;
s += *first;
s.append(first, first+1);
++first;
checkEof(first, last);
if(*first == '+' || *first == '-') {
s += *first;
s.append(first, first+1);
++first;
}
offset = first;
@ -488,7 +483,7 @@ std::string jsonEscape(const std::string& s)
for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi;
++i) {
if(*i == '"' || *i == '\\' || *i == '/') {
t += '\\';
t += "\\";
t += *i;
} else if(*i == '\b') {
t += "\\b";
@ -515,7 +510,7 @@ std::string jsonEscape(const std::string& s)
}
t += temp;
} else {
t += *i;
t.append(i, i+1);
}
}
return t;
@ -574,22 +569,22 @@ decodeGetParams(const std::string& query)
// Assume batch call.
jsonRequest = jsonParam;
} else {
jsonRequest = '{';
jsonRequest = "{";
if(method.first != method.second) {
jsonRequest += "\"method\":\"";
jsonRequest.append(method.first, method.second);
jsonRequest += '"';
jsonRequest += "\"";
}
if(id.first != id.second) {
jsonRequest += ",\"id\":\"";
jsonRequest.append(id.first, id.second);
jsonRequest += '"';
jsonRequest += "\"";
}
if(params.first != params.second) {
jsonRequest += ",\"params\":";
jsonRequest += jsonParam;
}
jsonRequest += '}';
jsonRequest += "}";
}
}
return JsonGetParam(jsonRequest, callback);

View File

@ -127,9 +127,7 @@ InputIterator expandLoop
if(start <= end) {
std::string format;
if(minus-first == colon-minus-1) {
format = "%0";
format += util::uitos(minus-first);
format += 'd';
format = fmt("%%0%lud", minus-first);
} else {
format = "%d";
}

View File

@ -1415,20 +1415,18 @@ bool inPrivateAddress(const std::string& ipv4addr)
{
static const char A2_IP10[] = "10.";
static const char A2_IP192[] = "192.168.";
static const char A2_IP172[] = "172.";
if(util::startsWith(ipv4addr.begin(), ipv4addr.end(),
A2_IP10, vend(A2_IP10)-1) ||
util::startsWith(ipv4addr.begin(), ipv4addr.end(),
A2_IP192, vend(A2_IP192)-1)) {
return true;
}
std::string p172("172.");
if(util::startsWith(ipv4addr.begin(), ipv4addr.end(),
p172.begin(), p172.end())) {
A2_IP172, vend(A2_IP172)-1)) {
for(int i = 16; i <= 31; ++i) {
std::string t(p172);
t += util::itos(i);
t += '.';
if(util::startsWith(ipv4addr.begin(), ipv4addr.end(),
std::string t(fmt("%d.", i));
if(util::startsWith(ipv4addr.begin()+4, ipv4addr.end(),
t.begin(), t.end())) {
return true;
}

View File

@ -141,7 +141,7 @@ std::string uitos(T value, bool comma = false)
++count;
char digit = value%10+'0';
if(comma && count > 3 && count%3 == 1) {
str += ',';
str += ",";
}
str += digit;
value /= 10;

View File

@ -179,6 +179,16 @@ void JsonTest::testDecode()
const String* s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("\"\\/\b\f\n\r\t"), s->s());
}
{
// string: literal + escaped chars.
SharedHandle<ValueBase> r =
json::decode("[\"foo\\u0024b\\u00A2\\u20ACbaz\"]");
const List* list = downcast<List>(r);
CPPUNIT_ASSERT(list);
const String* s = downcast<String>(list->get(0));
CPPUNIT_ASSERT_EQUAL(std::string("foo$b¢€baz"), s->s());
}
}
void JsonTest::testDecode_error()

View File

@ -85,6 +85,7 @@ class UtilTest:public CppUnit::TestFixture {
CPPUNIT_TEST(testIsUtf8String);
CPPUNIT_TEST(testNextParam);
CPPUNIT_TEST(testNoProxyDomainMatch);
CPPUNIT_TEST(testInPrivateAddress);
CPPUNIT_TEST_SUITE_END();
private:
@ -155,6 +156,7 @@ public:
void testIsUtf8String();
void testNextParam();
void testNoProxyDomainMatch();
void testInPrivateAddress();
};
@ -1829,4 +1831,17 @@ void UtilTest::testNoProxyDomainMatch()
CPPUNIT_ASSERT(!util::noProxyDomainMatch("example.org", "www.example.org"));
}
void UtilTest::testInPrivateAddress()
{
CPPUNIT_ASSERT(!util::inPrivateAddress("localhost"));
CPPUNIT_ASSERT(util::inPrivateAddress("192.168.0.1"));
// Only checks prefix..
CPPUNIT_ASSERT(util::inPrivateAddress("10."));
CPPUNIT_ASSERT(!util::inPrivateAddress("172."));
CPPUNIT_ASSERT(!util::inPrivateAddress("172.15.0.0"));
CPPUNIT_ASSERT(util::inPrivateAddress("172.16.0.0"));
CPPUNIT_ASSERT(util::inPrivateAddress("172.31.0.0"));
CPPUNIT_ASSERT(!util::inPrivateAddress("172.32.0.0"));
}
} // namespace aria2