1
0
Fork 0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-12-23 10:09:43 +01:00

Support hex entities in metadata

This commit is contained in:
leo60228 2020-06-15 20:31:54 -04:00
parent f0ec627628
commit a99e976402
3 changed files with 31 additions and 8 deletions

View file

@ -209,13 +209,23 @@ void UtilityClass::updateglow()
} }
} }
bool is_positive_num(const std::string& str) bool is_positive_num(const std::string& str, bool hex)
{ {
for (size_t i = 0; i < str.length(); i++) for (size_t i = 0; i < str.length(); i++)
{ {
if (!std::isdigit(static_cast<unsigned char>(str[i]))) if (hex)
{ {
return false; if (!std::isxdigit(static_cast<unsigned char>(str[i])))
{
return false;
}
}
else
{
if (!std::isdigit(static_cast<unsigned char>(str[i])))
{
return false;
}
} }
} }
return true; return true;

View file

@ -11,7 +11,7 @@ std::vector<std::string> split(const std::string &s, char delim, std::vector<std
std::vector<std::string> split(const std::string &s, char delim); std::vector<std::string> split(const std::string &s, char delim);
bool is_positive_num(const std::string& str); bool is_positive_num(const std::string& str, bool hex);
#define INBOUNDS(index, vector) ((int) index >= 0 && (int) index < (int) vector.size()) #define INBOUNDS(index, vector) ((int) index >= 0 && (int) index < (int) vector.size())

View file

@ -20,6 +20,9 @@
#include <utf8/unchecked.h> #include <utf8/unchecked.h>
#include <physfs.h> #include <physfs.h>
#include <inttypes.h>
#include <cstdio>
edlevelclass::edlevelclass() edlevelclass::edlevelclass()
{ {
tileset=0; tileset=0;
@ -161,16 +164,26 @@ std::string find_tag(const std::string& buf, const std::string& start, const std
size_t start_pos = 0; size_t start_pos = 0;
while ((start_pos = value.find("&#", start_pos)) != std::string::npos) while ((start_pos = value.find("&#", start_pos)) != std::string::npos)
{ {
bool hex = value[start_pos + 2] == 'x';
size_t end = value.find(';', start_pos); size_t end = value.find(';', start_pos);
std::string number(value.substr(start_pos + 2, end - start_pos)); size_t real_start = start_pos + 2 + ((int) hex);
std::string number(value.substr(real_start, end - real_start));
if (!is_positive_num(number)) if (!is_positive_num(number, hex))
{ {
return ""; return "";
} }
int character = atoi(number.c_str()); uint32_t character = 0;
int utf32[] = {character, 0}; if (hex)
{
sscanf(number.c_str(), "%" SCNx32, &character);
}
else
{
sscanf(number.c_str(), "%" SCNu32, &character);
}
uint32_t utf32[] = {character, 0};
std::string utf8; std::string utf8;
utf8::unchecked::utf32to8(utf32, utf32 + 1, std::back_inserter(utf8)); utf8::unchecked::utf32to8(utf32, utf32 + 1, std::back_inserter(utf8));
value.replace(start_pos, end - start_pos + 1, utf8); value.replace(start_pos, end - start_pos + 1, utf8);