General code optmisations and cleanup. (#15)

Updated `version_compare` based on this issue: #13 
Got rid of `replace_string_contents` helper func.
Replaced #define macros with static consts.
Moved comments from headers to source files.
This commit is contained in:
Ravin Perera
2019-10-10 12:57:46 +05:30
committed by GitHub
parent 374424f98f
commit d8581f7ce9
12 changed files with 331 additions and 299 deletions

View File

@@ -1,18 +1,19 @@
#include <string>
#include <sodium.h>
#include <sstream>
using namespace std;
namespace util
{
void replace_string_contents(string &str, const char *bytes, size_t bytes_len)
{
if (str.length() > 0)
str.clear();
str.append(bytes, bytes_len);
}
/**
* Encodes provided bytes to base64 string.
*
* @param bin Bytes to encode.
* @param bin_len Bytes length.
* @param encoded_string String reference to assign the base64 encoded output.
*/
int base64_encode(const unsigned char *bin, size_t bin_len, string &encoded_string)
{
// Get length of encoded result from sodium.
@@ -29,10 +30,17 @@ int base64_encode(const unsigned char *bin, size_t bin_len, string &encoded_stri
return -1;
// Assign the encoded char* onto the provided string reference.
replace_string_contents(encoded_string, base64chars, base64_len);
encoded_string = string(base64chars, base64_len);
return 0;
}
/**
* Decodes provided base64 string into bytes.
*
* @param base64_str Base64 string to decode.
* @param decoded Decoded bytes.
* @param decoded_len Decoded bytes length.
*/
int base64_decode(const string &base64_str, unsigned char *decoded, size_t decoded_len)
{
const char *b64_end;
@@ -49,35 +57,34 @@ int base64_decode(const string &base64_str, unsigned char *decoded, size_t decod
return 0;
}
// v1 < v2 -> -1
// v1 == v2 -> 0
// v1 > v2 -> +1
int version_compare(const string &v1, const string &v2)
/**
* Compare two version strings in the format of "1.12.3".
* v1 < v2 -> returns -1
* v1 == v2 -> returns 0
* v1 > v2 -> returns +1
* Error -> returns -2
*/
int version_compare(const string &x, const string &y)
{
size_t i = 0, j = 0;
while (i < v1.length() || j < v2.length())
istringstream ix(x), iy(y);
while (ix.good() || iy.good())
{
int acc1 = 0, acc2 = 0;
int cx = 0, cy = 0;
ix >> cx;
iy >> cy;
while (i < v1.length() && v1[i] != '.')
{
acc1 = acc1 * 10 + (v1[i] - '0');
i++;
}
while (j < v2.length() && v2[j] != '.')
{
acc2 = acc2 * 10 + (v2[j] - '0');
j++;
}
if ((!ix.eof() && !ix.good()) || (!iy.eof() && !iy.good()))
return -2;
if (acc1 < acc2)
if (cx > cy)
return 1;
if (cx < cy)
return -1;
if (acc1 > acc2)
return +1;
++i;
++j;
ix.ignore();
iy.ignore();
}
return 0;
}