upgrade some C-arrays to std::array in escape_string.cpp (#1357)
upgrade some C-arrays to std::array in escape_string.cpp
This commit is contained in:
parent
54f052a97b
commit
0df9d1c641
|
@ -279,21 +279,21 @@ namespace libtorrent
|
|||
'4', '5', '6', '7', '8', '9', '+', '/'
|
||||
};
|
||||
|
||||
std::uint8_t inbuf[3];
|
||||
std::uint8_t outbuf[4];
|
||||
std::array<std::uint8_t, 3> inbuf;
|
||||
std::array<std::uint8_t, 4> outbuf;
|
||||
|
||||
std::string ret;
|
||||
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
||||
{
|
||||
// available input is 1,2 or 3 bytes
|
||||
// since we read 3 bytes at a time at most
|
||||
int available_input = (std::min)(3, int(s.end()-i));
|
||||
int available_input = std::min(int(inbuf.size()), int(s.end() - i));
|
||||
|
||||
// clear input buffer
|
||||
std::fill(inbuf, inbuf+3, 0);
|
||||
inbuf.fill(0);
|
||||
|
||||
// read a chunk of input into inbuf
|
||||
std::copy(i, i + available_input, inbuf);
|
||||
std::copy(i, i + available_input, inbuf.begin());
|
||||
i += available_input;
|
||||
|
||||
// encode inbuf to outbuf
|
||||
|
@ -303,13 +303,13 @@ namespace libtorrent
|
|||
outbuf[3] = inbuf[2] & 0x3f;
|
||||
|
||||
// write output
|
||||
for (int j = 0; j < available_input+1; ++j)
|
||||
for (int j = 0; j < available_input + 1; ++j)
|
||||
{
|
||||
ret += base64_table[outbuf[j]];
|
||||
}
|
||||
|
||||
// write pad
|
||||
for (int j = 0; j < 3 - available_input; ++j)
|
||||
for (int j = 0; j < inbuf.size() - available_input; ++j)
|
||||
{
|
||||
ret += '=';
|
||||
}
|
||||
|
@ -335,21 +335,21 @@ namespace libtorrent
|
|||
};
|
||||
char const *base32_table = 0 != (flags & string::lowercase) ? base32_table_lowercase : base32_table_canonical;
|
||||
|
||||
static int const input_output_mapping[] = {0, 2, 4, 5, 7, 8};
|
||||
static std::array<int const, 6> input_output_mapping = {{0, 2, 4, 5, 7, 8}};
|
||||
|
||||
std::uint8_t inbuf[5];
|
||||
std::uint8_t outbuf[8];
|
||||
std::array<std::uint8_t, 5> inbuf;
|
||||
std::array<std::uint8_t, 8> outbuf;
|
||||
|
||||
std::string ret;
|
||||
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
||||
{
|
||||
int available_input = (std::min)(5, int(s.end()-i));
|
||||
int available_input = std::min(int(inbuf.size()), int(s.end()-i));
|
||||
|
||||
// clear input buffer
|
||||
std::fill(inbuf, inbuf+5, 0);
|
||||
inbuf.fill(0);
|
||||
|
||||
// read a chunk of input into inbuf
|
||||
std::copy(i, i + available_input, inbuf);
|
||||
std::copy(i, i + available_input, inbuf.begin());
|
||||
i += available_input;
|
||||
|
||||
// encode inbuf to outbuf
|
||||
|
@ -363,7 +363,7 @@ namespace libtorrent
|
|||
outbuf[7] = inbuf[4] & 0x1f;
|
||||
|
||||
// write output
|
||||
int num_out = input_output_mapping[available_input];
|
||||
int const num_out = input_output_mapping[available_input];
|
||||
for (int j = 0; j < num_out; ++j)
|
||||
{
|
||||
ret += base32_table[outbuf[j]];
|
||||
|
@ -372,7 +372,7 @@ namespace libtorrent
|
|||
if (0 == (flags & string::no_padding))
|
||||
{
|
||||
// write pad
|
||||
for (int j = 0; j < 8 - num_out; ++j)
|
||||
for (int j = 0; j < outbuf.size() - num_out; ++j)
|
||||
{
|
||||
ret += '=';
|
||||
}
|
||||
|
@ -383,19 +383,19 @@ namespace libtorrent
|
|||
|
||||
std::string base32decode(std::string const& s)
|
||||
{
|
||||
std::uint8_t inbuf[8];
|
||||
std::uint8_t outbuf[5];
|
||||
std::array<std::uint8_t, 8> inbuf;
|
||||
std::array<std::uint8_t, 5> outbuf;
|
||||
|
||||
std::string ret;
|
||||
for (std::string::const_iterator i = s.begin(); i != s.end();)
|
||||
{
|
||||
int available_input = std::min(8, int(s.end() - i));
|
||||
int available_input = std::min(int(inbuf.size()), int(s.end() - i));
|
||||
|
||||
int pad_start = 0;
|
||||
if (available_input < 8) pad_start = available_input;
|
||||
|
||||
// clear input buffer
|
||||
std::fill(inbuf, inbuf+8, 0);
|
||||
inbuf.fill(0);
|
||||
for (int j = 0; j < available_input; ++j)
|
||||
{
|
||||
char const in = char(std::toupper(*i++));
|
||||
|
@ -433,7 +433,7 @@ namespace libtorrent
|
|||
int num_out = input_output_mapping[pad_start];
|
||||
|
||||
// write output
|
||||
std::copy(outbuf, outbuf + num_out, std::back_inserter(ret));
|
||||
std::copy(outbuf.begin(), outbuf.begin() + num_out, std::back_inserter(ret));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue