1
0
mirror of https://github.com/TerryCavanagh/VVVVVV.git synced 2024-06-29 07:58:30 +02: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,14 +209,24 @@ 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 (hex)
{
if (!std::isxdigit(static_cast<unsigned char>(str[i])))
{
return false;
}
}
else
{ {
if (!std::isdigit(static_cast<unsigned char>(str[i]))) if (!std::isdigit(static_cast<unsigned char>(str[i])))
{ {
return false; 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);