mirror of https://github.com/axmolengine/axmol.git
Improve code quality
This commit is contained in:
parent
a95a00e96b
commit
232387ac11
|
@ -401,21 +401,21 @@ std::string getDataMD5Hash(const Data& data)
|
|||
|
||||
if (data.isNull())
|
||||
{
|
||||
return std::string();
|
||||
return std::string{};
|
||||
}
|
||||
|
||||
MD5state_st state;
|
||||
uint8_t digest[MD5_DIGEST_LENGTH];
|
||||
char hexOutput[(MD5_DIGEST_LENGTH << 1) + 1] = {0};
|
||||
char hexOutput[(MD5_DIGEST_LENGTH << 1)] = {0};
|
||||
|
||||
MD5_Init(&state);
|
||||
MD5_Update(&state, data.getBytes(), (int)data.getSize());
|
||||
MD5_Final(digest, &state);
|
||||
|
||||
for (int di = 0; di < 16; ++di)
|
||||
sprintf(hexOutput + di * 2, "%02x", digest[di]);
|
||||
char2hex(hexOutput + di * 2, digest[di]);
|
||||
|
||||
return hexOutput;
|
||||
return std::string{hexOutput, (size_t)32};
|
||||
}
|
||||
|
||||
LanguageType getLanguageTypeByISO2(const char* code)
|
||||
|
@ -718,42 +718,25 @@ void killCurrentProcess()
|
|||
#endif
|
||||
}
|
||||
|
||||
unsigned char nibble2hex(unsigned char c)
|
||||
{
|
||||
return ((c) < 0xa ? ((c) + '0') : ((c) + 'a' - 10));
|
||||
}
|
||||
|
||||
unsigned char hex2nibble(unsigned char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
return 10 + (c - 'a');
|
||||
}
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
return 10 + (c - 'A');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string urlEncode(std::string_view s)
|
||||
{
|
||||
std::string encoded;
|
||||
for (const char c : s)
|
||||
if (!s.empty())
|
||||
{
|
||||
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~')
|
||||
encoded.reserve(s.length() * 3 / 2);
|
||||
for (const char c : s)
|
||||
{
|
||||
encoded = encoded + c;
|
||||
}
|
||||
else
|
||||
{
|
||||
char hex[4];
|
||||
snprintf(hex, sizeof(hex), "%%%02x", c);
|
||||
encoded = encoded + hex;
|
||||
if (isalnum((uint8_t)c) || c == '-' || c == '_' || c == '.' || c == '~')
|
||||
{
|
||||
encoded.push_back(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
encoded.push_back('%');
|
||||
|
||||
char hex[2];
|
||||
encoded.append(char2hex(hex, c, 'A'), sizeof(hex));
|
||||
}
|
||||
}
|
||||
}
|
||||
return encoded;
|
||||
|
@ -762,25 +745,29 @@ std::string urlEncode(std::string_view s)
|
|||
std::string urlDecode(std::string_view st)
|
||||
{
|
||||
std::string decoded;
|
||||
const char* s = st.data();
|
||||
const size_t length = st.length();
|
||||
for (unsigned int i = 0; i < length; ++i)
|
||||
if (!st.empty())
|
||||
{
|
||||
if (!s[i])
|
||||
break;
|
||||
const char* s = st.data();
|
||||
const size_t length = st.length();
|
||||
decoded.reserve(length * 2 / 3);
|
||||
for (unsigned int i = 0; i < length; ++i)
|
||||
{
|
||||
if (!s[i])
|
||||
break;
|
||||
|
||||
if (s[i] == '%')
|
||||
{
|
||||
decoded.push_back(hex2char(s + i + 1));
|
||||
i = i + 2;
|
||||
}
|
||||
else if (s[i] == '+')
|
||||
{
|
||||
decoded.push_back(' ');
|
||||
}
|
||||
else
|
||||
{
|
||||
decoded.push_back(s[i]);
|
||||
if (s[i] == '%')
|
||||
{
|
||||
decoded.push_back(hex2char(s + i + 1));
|
||||
i = i + 2;
|
||||
}
|
||||
else if (s[i] == '+')
|
||||
{
|
||||
decoded.push_back(' ');
|
||||
}
|
||||
else
|
||||
{
|
||||
decoded.push_back(s[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return decoded;
|
||||
|
|
|
@ -356,20 +356,46 @@ inline bool isPOT(int number)
|
|||
return ((number > 0) && (number & (number - 1)) == 0);
|
||||
}
|
||||
|
||||
// Convert a nibble ASCII hex digit
|
||||
CC_DLL unsigned char nibble2hex(unsigned char c);
|
||||
|
||||
// Convert ASCII hex digit to a nibble (four bits, 0 - 15).
|
||||
//
|
||||
// Use unsigned to avoid signed overflow UB.
|
||||
CC_DLL unsigned char hex2nibble(unsigned char c);
|
||||
inline unsigned char hex2nibble(unsigned char c)
|
||||
{
|
||||
if (c >= '0' && c <= '9')
|
||||
{
|
||||
return c - '0';
|
||||
}
|
||||
else if (c >= 'a' && c <= 'f')
|
||||
{
|
||||
return 10 + (c - 'a');
|
||||
}
|
||||
else if (c >= 'A' && c <= 'F')
|
||||
{
|
||||
return 10 + (c - 'A');
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Convert a nibble ASCII hex digit
|
||||
inline unsigned char nibble2hex(unsigned char c, unsigned char a = 'a')
|
||||
{
|
||||
return ((c) < 0xa ? ((c) + '0') : ((c) + a - 10));
|
||||
}
|
||||
|
||||
// Convert ASCII hex string (two characters) to byte.
|
||||
//
|
||||
// E.g., "0B" => 0x0B, "af" => 0xAF.
|
||||
inline char hex2char(const char* p)
|
||||
{
|
||||
return hex2nibble(p[0]) * 16 + hex2nibble(p[1]);
|
||||
return hex2nibble((uint8_t)p[0]) << 4 | hex2nibble(p[1]);
|
||||
}
|
||||
|
||||
// Convert byte to ASCII hex string (two characters).
|
||||
inline char* char2hex(char* p, unsigned char c, unsigned char a = 'a')
|
||||
{
|
||||
p[0] = nibble2hex(c >> 4, a);
|
||||
p[1] = nibble2hex(c & (uint8_t)0xf, a);
|
||||
return p;
|
||||
}
|
||||
|
||||
CC_DLL std::string urlEncode(std::string_view s);
|
||||
|
|
Loading…
Reference in New Issue