<commit_before>// For conditions of distribution and use, see copyright notice in license.txt

#include "StableHeaders.h"
#include "QtUiAsset.h"
#include "AssetAPI.h"

#include <QByteArrayMatcher>
#include <QUrl>

#include "LoggingFunctions.h"
DEFINE_POCO_LOGGING_FUNCTIONS("QtUiAsset")

QtUiAsset::QtUiAsset(AssetAPI *owner, const QString &type_, const QString &name_) :
    IAsset(owner, type_, name_)
{
    patterns_ << "http://" << "https://" << "local://" << "file://";
}

QtUiAsset::~QtUiAsset() 
{
}

bool QtUiAsset::DeserializeFromData(const u8 *data, size_t numBytes)
{
    refs_.clear();
    data_.clear();
    data_.insert(data_.begin(), data, data + numBytes);   

    // Find references
    QByteArray dataQt((const char*)&data_[0], data_.size());
    if (!dataQt.isNull() && !dataQt.isEmpty())
    {
        QByteArrayMatcher matcher;
        foreach(QByteArray pattern, patterns_)
        {
            int from = 0;
            int indexStart = 0;
            matcher.setPattern(pattern);
            while (indexStart != -1)
            {
                indexStart = matcher.indexIn(dataQt, from);
                if (indexStart == -1)
                    break;
                int indexStop = dataQt.indexOf(")", indexStart);
                QUrl ref(dataQt.mid(indexStart, indexStop - indexStart), QUrl::TolerantMode);
                if (ref.isValid())
                    refs_.push_back(AssetReference(ref.toString()));
                from = indexStop;
            }
        }
    }
    else
        LogError("FindReferences: Could not process .ui file content, error: data was null.");

    return true;
}

bool QtUiAsset::SerializeTo(std::vector<u8> &data, const QString &serializationParameters )
{
    if (data_.empty())
        return false;
    data.clear();
    data = data_;
    return true;
}
<commit_msg>QtUiAsset: Fixed a infinite while loop bug. If there is a url in the .ui xml content that but did not have the end char we are looking for on the rest of the file. A QTextBrowser will do this for instance, adds automatically some template html code into itself that has url refs to http specs etc.<commit_after>// For conditions of distribution and use, see copyright notice in license.txt

#include "StableHeaders.h"
#include "QtUiAsset.h"
#include "AssetAPI.h"

#include <QByteArrayMatcher>
#include <QUrl>

#include "LoggingFunctions.h"
DEFINE_POCO_LOGGING_FUNCTIONS("QtUiAsset")

QtUiAsset::QtUiAsset(AssetAPI *owner, const QString &type_, const QString &name_) :
    IAsset(owner, type_, name_)
{
    patterns_ << "http://" << "https://" << "local://" << "file://";
}

QtUiAsset::~QtUiAsset() 
{
}

bool QtUiAsset::DeserializeFromData(const u8 *data, size_t numBytes)
{
    refs_.clear();
    data_.clear();
    data_.insert(data_.begin(), data, data + numBytes);   

    // Find references
    QByteArray dataQt((const char*)&data_[0], data_.size());
    if (!dataQt.isNull() && !dataQt.isEmpty())
    {
        QByteArrayMatcher matcher;
        foreach(QByteArray pattern, patterns_)
        {
            int from = 0;
            int indexStart = 0;
            matcher.setPattern(pattern);
            while (indexStart != -1)
            {
                indexStart = matcher.indexIn(dataQt, from);
                if (indexStart == -1)
                    break;
                // ")" due to Qt style sheets have this syntax most commonly: "some-image-property: url(pattern);"
                int indexStop = dataQt.indexOf(")", indexStart);
                if (indexStop == -1)
                {
                    from = indexStart + 1;
                    continue;
                }
                QUrl ref(dataQt.mid(indexStart, indexStop - indexStart), QUrl::TolerantMode);
                if (ref.isValid())
                    refs_.push_back(AssetReference(ref.toString()));
                from = indexStop;
            }
        }
    }
    else
        LogError("FindReferences: Could not process .ui file content, error: data was null.");

    return true;
}

bool QtUiAsset::SerializeTo(std::vector<u8> &data, const QString &serializationParameters )
{
    if (data_.empty())
        return false;
    data.clear();
    data = data_;
    return true;
}

{'fraction_non_alphanumeric': 0.12384560738811272, 'fraction_numerical': 0.003788775751835188, 'mean_word_length': 2.3550436854646546, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7194182', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1228, 'n_words': 332}