Add support for hexadecimal HTML escape codes

This commit is contained in:
Andrzej Rybczak
2025-02-26 02:35:09 +01:00
parent 2f9a65e177
commit e8f1c4bd48
2 changed files with 15 additions and 1 deletions

View File

@@ -1,5 +1,6 @@
# ncmpcpp-0.10.2 (2025-??-??) # ncmpcpp-0.10.2 (2025-??-??)
* Update lyrics fetchers. * Update lyrics fetchers.
* Add support for hexadecimal HTML escape codes.
# ncmpcpp-0.10.1 (2024-10-24) # ncmpcpp-0.10.1 (2024-10-24)
* Fix compilation with `libc++`. * Fix compilation with `libc++`.

View File

@@ -20,16 +20,29 @@
#include <algorithm> #include <algorithm>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
#include <cstdlib>
#include "utility/html.h" #include "utility/html.h"
std::string unescapeHtmlUtf8(const std::string &data) std::string unescapeHtmlUtf8(const std::string &data)
{ {
int base;
size_t offset;
std::string result; std::string result;
for (size_t i = 0, j; i < data.length(); ++i) for (size_t i = 0, j; i < data.length(); ++i)
{ {
if (data[i] == '&' && data[i+1] == '#' && (j = data.find(';', i)) != std::string::npos) if (data[i] == '&' && data[i+1] == '#' && (j = data.find(';', i)) != std::string::npos)
{ {
int n = atoi(&data.c_str()[i+2]); if (data[i+2] == 'x')
{
offset = 3;
base = 16;
}
else
{
offset = 2;
base = 10;
}
int n = strtol(&data.c_str()[i+offset], nullptr, base);
if (n >= 0x800) if (n >= 0x800)
{ {
result += (0xe0 | ((n >> 12) & 0x0f)); result += (0xe0 | ((n >> 12) & 0x0f));