| 750 | } |
| 751 | |
| 752 | std::optional<const QByteArray> SourceData::getEncodingFromTag(const QByteArray& s, const QByteArray& encodingTag) |
| 753 | { |
| 754 | qsizetype encodingPos = s.indexOf(encodingTag); |
| 755 | if(encodingPos >= 0) |
| 756 | { |
| 757 | qsizetype apostrophPos = s.indexOf('"', encodingPos + encodingTag.length()); |
| 758 | qsizetype apostroph2Pos = s.indexOf('\'', encodingPos + encodingTag.length()); |
| 759 | char apostroph = '"'; |
| 760 | if(apostroph2Pos >= 0 && (apostrophPos < 0 || apostroph2Pos < apostrophPos)) |
| 761 | { |
| 762 | apostroph = '\''; |
| 763 | apostrophPos = apostroph2Pos; |
| 764 | } |
| 765 | |
| 766 | qsizetype encodingEnd = s.indexOf(apostroph, apostrophPos + 1); |
| 767 | if(encodingEnd >= 0) // e.g.: <meta charset="utf-8"> or <?xml version="1.0" encoding="ISO-8859-1"?> |
| 768 | { |
| 769 | QByteArray encoding = s.mid(apostrophPos + 1, encodingEnd - (apostrophPos + 1)); |
| 770 | QStringDecoder decoder = QStringDecoder(encoding); |
| 771 | if(decoder.isValid()) |
| 772 | return encoding.toUpper(); |
| 773 | } |
| 774 | else // e.g.: <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> |
| 775 | { |
| 776 | QByteArray encoding = s.mid(encodingPos + encodingTag.length(), apostrophPos - (encodingPos + encodingTag.length())); |
| 777 | QStringDecoder decoder = QStringDecoder(encoding); |
| 778 | if(decoder.isValid()) |
| 779 | return encoding.toUpper(); |
| 780 | } |
| 781 | } |
| 782 | return {}; |
| 783 | } |
| 784 | |
| 785 | std::optional<const QByteArray> SourceData::detectEncoding(const char* buf, qint64 size) |
| 786 | { |