<commit_before>/*
 * Skaffari - a mail account administration web interface based on Cutelyst
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "utils.h"
#include <Cutelyst/Context>
#include <Cutelyst/Request>
#include <Cutelyst/Plugins/Session/Session>
#include <QTimeZone>
#include <QLocale>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <cmath>

Utils::Utils()
{

}



QDateTime Utils::toUserTZ(Cutelyst::Context *c, const QDateTime &dt)
{
    QDateTime retVal;

    const QTimeZone userTz(Cutelyst::Session::value(c, QStringLiteral("tz"), QStringLiteral("UTC")).toByteArray());

    if (userTz != QTimeZone::utc()) {
        QDateTime _dt = dt;
        _dt.setTimeSpec(Qt::UTC);
        retVal = _dt.toTimeZone(userTz);
    } else {
        retVal = dt;
    }

    return retVal;
}



QString Utils::humanBinarySize(Cutelyst::Context *c, quota_size_t sizeInByte)
{
    QString sizeStr;

    if (sizeInByte < 1048576) {
        float kibFloat = (float)sizeInByte/1024.0f;
        sizeStr = c->locale().toString(kibFloat, 'f', 2);
        sizeStr.append(QLatin1String(" KiB"));
    } else if (sizeInByte < 1073741824) {
        float mibFloat = (float)sizeInByte/1048576.0f;
        sizeStr = c->locale().toString(mibFloat, 'f', 2);
        sizeStr.append(QLatin1String(" MiB"));
    } else if (sizeInByte < 1099511627776) {
        float gibFloat = (float)sizeInByte/1073741824.0f;
        sizeStr = c->locale().toString(gibFloat, 'f', 2);
        sizeStr.append(QLatin1String(" GiB"));
    } else {
        float tibFloat = (float)sizeInByte/1099511627776.0f;
        sizeStr = c->locale().toString(tibFloat, 'f', 2);
        sizeStr.append(QLatin1String(" TiB"));
    }

    return sizeStr;
}


QString Utils::getUserName(Cutelyst::Context *c)
{
    QString userName;
    Q_ASSERT_X(c, "get user name", "invalid context object");
    userName = c->stash(QStringLiteral("userName")).toString();
    return userName;
}


quota_size_t Utils::humanToIntSize(Cutelyst::Context *c, const QString &size, bool *ok)
{
    quota_size_t ret = 0;

    Q_ASSERT_X(ok, "convert human quota string to KiB", "invalid pointer to a boolean succeed value (ok)");

    QRegularExpression regex(QStringLiteral("(\\d+[,.٫]?\\d*)\\s*([KMGT]?i?B?)"), QRegularExpression::CaseInsensitiveOption);
    QRegularExpressionMatch match = regex.match(size);

    if (!match.hasMatch()) {<commit_msg>Utils: improve binary to human readable size<commit_after>/*
 * Skaffari - a mail account administration web interface based on Cutelyst
 * Copyright (C) 2017 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "utils.h"
#include <Cutelyst/Context>
#include <Cutelyst/Request>
#include <Cutelyst/Plugins/Session/Session>
#include <QTimeZone>
#include <QLocale>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <cmath>
#include <limits>

Utils::Utils()
{

}



QDateTime Utils::toUserTZ(Cutelyst::Context *c, const QDateTime &dt)
{
    QDateTime retVal;

    const QTimeZone userTz(Cutelyst::Session::value(c, QStringLiteral("tz"), QStringLiteral("UTC")).toByteArray());

    if (userTz != QTimeZone::utc()) {
        QDateTime _dt = dt;
        _dt.setTimeSpec(Qt::UTC);
        retVal = _dt.toTimeZone(userTz);
    } else {
        retVal = dt;
    }

    return retVal;
}



QString Utils::humanBinarySize(Cutelyst::Context *c, quota_size_t sizeInByte)
{
    QString sizeStr;

#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))

    if (sizeInByte < static_cast<quota_size_t>(std::numeric_limits<qint64>::max())) {
        sizeStr = c->locale().formattedDataSize(static_cast<qint64>(sizeInByte));
    } else {

#endif

        if (sizeInByte < Q_UINT64_C(1048576)) {
            const double kibFloat = static_cast<double>(sizeInByte)/1024.0;
            sizeStr = c->locale().toString(kibFloat, 'f', 2);
            sizeStr.append(QLatin1String(" KiB"));
        } else if (sizeInByte < Q_UINT64_C(1073741824)) {
            const double mibFloat = static_cast<double>(sizeInByte)/1048576.0;
            sizeStr = c->locale().toString(mibFloat, 'f', 2);
            sizeStr.append(QLatin1String(" MiB"));
        } else if (sizeInByte < Q_UINT64_C(1099511627776)) {
            const double gibFloat = static_cast<double>(sizeInByte)/1073741824.0;
            sizeStr = c->locale().toString(gibFloat, 'f', 2);
            sizeStr.append(QLatin1String(" GiB"));
        } else if (sizeInByte < Q_UINT64_C(1125899906842624)) {
            const double tibFloat = static_cast<double>(sizeInByte)/1099511627776.0;
            sizeStr = c->locale().toString(tibFloat, 'f', 2);
            sizeStr.append(QLatin1String(" TiB"));
        } else if (sizeInByte < Q_UINT64_C(1152921504606846976)) {
            const double pibFloat = static_cast<double>(sizeInByte)/1125899906842624.0;
            sizeStr = c->locale().toString(pibFloat, 'f', 2);
            sizeStr.append(QLatin1String(" PiB"));
        } else {
            const double eibFloat = static_cast<double>(sizeInByte)/1152921504606846976.0;
            sizeStr = c->locale().toString(eibFloat, 'f', 2);
            sizeStr.append(QLatin1String(" EiB"));
        }

#if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
    }
#endif

    return sizeStr;
}


QString Utils::getUserName(Cutelyst::Context *c)
{
    QString userName;
    Q_ASSERT_X(c, "get user name", "invalid context object");
    userName = c->stash(QStringLiteral("userName")).toString();
    return userName;
}


quota_size_t Utils::humanToIntSize(Cutelyst::Context *c, const QString &size, bool *ok)
{
    quota_size_t ret = 0;

    Q_ASSERT_X(ok, "convert human quota string to KiB", "invalid pointer to a boolean succeed value (ok)");

    QRegularExpression regex(QStringLiteral("(\\d+[,.٫]?\\d*)\\s*([KMGT]?i?B?)"), QRegularExpression::CaseInsensitiveOption);
    QRegularExpressionMatch match = regex.match(size);

    if (!match.hasMatch()) {
{'fraction_non_alphanumeric': 0.12950637825845812, 'fraction_numerical': 0.03632834165280089, 'mean_word_length': 3.6807268007787153, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '876556', 'n_tokens_mistral': 2521, 'n_tokens_neox': 2314, 'n_words': 600}