<commit_before>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <boost/shared_ptr.hpp>
#include <unicode/regex.h>

#include <comphelper/configuration.hxx>
#include <officecfg/Office/Common.hxx>
#include <opencl/openclconfig.hxx>
#include <opencl/platforminfo.hxx>
#include <rtl/ustring.hxx>
#include <sal/types.h>

OpenCLConfig::OpenCLConfig() :
    mbUseOpenCL(true)
{
    // This entry we have had for some time (when blacklisting was
    // done elsewhere in the code), so presumably there is a known
    // good reason for it.
    maBlackList.insert(ImplMatcher("Windows", "", "Intel\\(R\\) Corporation", "", "9\\.17\\.10\\.2884"));

    // This is what I have tested on Linux and it works for our unit tests.
    maWhiteList.insert(ImplMatcher("Linux", "", "Advanced Micro Devices, Inc\\.", "", "1445\\.5 \\(sse2,avx\\)"));

    // For now, assume that AMD, Intel and NVIDIA drivers are good
    maWhiteList.insert(ImplMatcher("", "", "Advanced Micro Devices, Inc\\.", "", ""));
    maWhiteList.insert(ImplMatcher("", "", "Intel\\(R\\) Corporation", "", ""));
    maWhiteList.insert(ImplMatcher("", "", "NVIDIA Corporation", "", ""));
}

bool OpenCLConfig::operator== (const OpenCLConfig& r) const
{
    return (mbUseOpenCL == r.mbUseOpenCL &&
            maBlackList == r.maBlackList &&
            maWhiteList == r.maWhiteList &&
            true);
}

bool OpenCLConfig::operator!= (const OpenCLConfig& r) const
{
    return !operator== (r);
}

namespace {

css::uno::Sequence<OUString> SetOfImplMatcherToStringSequence(const OpenCLConfig::ImplMatcherSet& rSet)
{
    css::uno::Sequence<OUString> result(rSet.size());

    size_t n(0);
    for (auto i = rSet.cbegin(); i != rSet.cend(); ++i)
    {
        result[n++] =
            (*i).maOS.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B") + "/" +
            (*i).maOSVersion.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B") + "/" +
            (*i).maPlatformVendor.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B") + "/" +
            (*i).maDevice.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B") + "/" +
            (*i).maDriverVersion.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B");
    }

    return result;
}

OUString getToken(const OUString& string, sal_Int32& index)
{
    OUString token(string.getToken(0, '/', index));
    OUString result;
    sal_Int32 i(0);
    sal_Int32 p;
    while ((p = token.indexOf('%', i)) >= 0)
    {
        if (p > i)
            result += token.copy(i, p - i);
        if (p < token.getLength() - 2)
        {
            result += OUString(static_cast<sal_Unicode>(token.copy(p+1, 2).toInt32(16)));
            i = p + 3;
        }
        else
        {
            i = token.getLength();
        }
    }
    result += token.copy(i);

    return result;
}

OpenCLConfig::ImplMatcherSet StringSequenceToSetOfImplMatcher(const css::uno::Sequence<OUString>& rSequence)
{
    OpenCLConfig::ImplMatcherSet result;

    for (auto i = rSequence.begin(); i != rSequence.end(); ++i)
    {
        OpenCLConfig::ImplMatcher m;
        sal_Int32 index(0);
        m.maOS = getToken(*i, index);
        m.maOSVersion = getToken(*i, index);
        m.maPlatformVendor = getToken(*i, index);
        m.maDevice = getToken(*i, index);
        m.maDriverVersion = getToken(*i, index);

        result.insert(m);
    }

    return result;
}

bool match(const OUString& rPattern, const OUString& rInput)
{
    if (rPattern == "")
        return true;

    UErrorCode nIcuError(U_ZERO_ERROR);
    icu::UnicodeString sIcuPattern(reinterpret_cast<const UChar*>(rPattern.getStr()), rPattern.getLength());
    icu::UnicodeString sIcuInput(reinterpret_cast<const UChar*>(rInput.getStr()), rInput.getLength());
    RegexMatcher aMatcher(sIcuPattern, sIcuInput, 0, nIcuError);

    if (U_SUCCESS(nIcuError) && aMatcher.matches(nIcuError) && U_SUCCESS(nIcuError))
        return true;

    return false;
}

bool match(const OpenCLConfig::ImplMatcher& rListEntry, const OpenCLPlatformInfo& rPlatform, const OpenCLDeviceInfo& rDevice)
{
#if defined WNT
    if (rListEntry.maOS != "" && rListEntry.maOS != "Windows")
        return false;
#elif defined LINUX
    if (rListEntry.maOS != "" && rListEntry.maOS != "Linux")
        return false;
#elif defined MACOSX
    if (rListEntry.maOS != "" && rListEntry.maOS != "OS X")
        return false;<commit_msg>opencl: Use appropriate OUString functions on string constants<commit_after>/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <boost/shared_ptr.hpp>
#include <unicode/regex.h>

#include <comphelper/configuration.hxx>
#include <officecfg/Office/Common.hxx>
#include <opencl/openclconfig.hxx>
#include <opencl/platforminfo.hxx>
#include <rtl/ustring.hxx>
#include <sal/types.h>

OpenCLConfig::OpenCLConfig() :
    mbUseOpenCL(true)
{
    // This entry we have had for some time (when blacklisting was
    // done elsewhere in the code), so presumably there is a known
    // good reason for it.
    maBlackList.insert(ImplMatcher("Windows", "", "Intel\\(R\\) Corporation", "", "9\\.17\\.10\\.2884"));

    // This is what I have tested on Linux and it works for our unit tests.
    maWhiteList.insert(ImplMatcher("Linux", "", "Advanced Micro Devices, Inc\\.", "", "1445\\.5 \\(sse2,avx\\)"));

    // For now, assume that AMD, Intel and NVIDIA drivers are good
    maWhiteList.insert(ImplMatcher("", "", "Advanced Micro Devices, Inc\\.", "", ""));
    maWhiteList.insert(ImplMatcher("", "", "Intel\\(R\\) Corporation", "", ""));
    maWhiteList.insert(ImplMatcher("", "", "NVIDIA Corporation", "", ""));
}

bool OpenCLConfig::operator== (const OpenCLConfig& r) const
{
    return (mbUseOpenCL == r.mbUseOpenCL &&
            maBlackList == r.maBlackList &&
            maWhiteList == r.maWhiteList &&
            true);
}

bool OpenCLConfig::operator!= (const OpenCLConfig& r) const
{
    return !operator== (r);
}

namespace {

css::uno::Sequence<OUString> SetOfImplMatcherToStringSequence(const OpenCLConfig::ImplMatcherSet& rSet)
{
    css::uno::Sequence<OUString> result(rSet.size());

    size_t n(0);
    for (auto i = rSet.cbegin(); i != rSet.cend(); ++i)
    {
        result[n++] =
            (*i).maOS.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B") + "/" +
            (*i).maOSVersion.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B") + "/" +
            (*i).maPlatformVendor.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B") + "/" +
            (*i).maDevice.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B") + "/" +
            (*i).maDriverVersion.replaceAll("%", "%25").replaceAll("/", "%2F").replaceAll(";", "%3B");
    }

    return result;
}

OUString getToken(const OUString& string, sal_Int32& index)
{
    OUString token(string.getToken(0, '/', index));
    OUString result;
    sal_Int32 i(0);
    sal_Int32 p;
    while ((p = token.indexOf('%', i)) >= 0)
    {
        if (p > i)
            result += token.copy(i, p - i);
        if (p < token.getLength() - 2)
        {
            result += OUString(static_cast<sal_Unicode>(token.copy(p+1, 2).toInt32(16)));
            i = p + 3;
        }
        else
        {
            i = token.getLength();
        }
    }
    result += token.copy(i);

    return result;
}

OpenCLConfig::ImplMatcherSet StringSequenceToSetOfImplMatcher(const css::uno::Sequence<OUString>& rSequence)
{
    OpenCLConfig::ImplMatcherSet result;

    for (auto i = rSequence.begin(); i != rSequence.end(); ++i)
    {
        OpenCLConfig::ImplMatcher m;
        sal_Int32 index(0);
        m.maOS = getToken(*i, index);
        m.maOSVersion = getToken(*i, index);
        m.maPlatformVendor = getToken(*i, index);
        m.maDevice = getToken(*i, index);
        m.maDriverVersion = getToken(*i, index);

        result.insert(m);
    }

    return result;
}

bool match(const OUString& rPattern, const OUString& rInput)
{
    if (rPattern.isEmpty())
        return true;

    UErrorCode nIcuError(U_ZERO_ERROR);
    icu::UnicodeString sIcuPattern(reinterpret_cast<const UChar*>(rPattern.getStr()), rPattern.getLength());
    icu::UnicodeString sIcuInput(reinterpret_cast<const UChar*>(rInput.getStr()), rInput.getLength());
    RegexMatcher aMatcher(sIcuPattern, sIcuInput, 0, nIcuError);

    if (U_SUCCESS(nIcuError) && aMatcher.matches(nIcuError) && U_SUCCESS(nIcuError))
        return true;

    return false;
}

bool match(const OpenCLConfig::ImplMatcher& rListEntry, const OpenCLPlatformInfo& rPlatform, const OpenCLDeviceInfo& rDevice)
{
#if defined WNT
    if (!rListEntry.maOS.isEmpty() && rListEntry.maOS != "Windows")
        return false;
#elif defined LINUX
    if (!rListEntry.maOS.isEmpty() && rListEntry.maOS != "Linux")
        return false;
#elif defined MACOSX
    if (!rListEntry.maOS.isEmpty() && rListEntry.maOS != "OS X")
        return false;
{'fraction_non_alphanumeric': 0.17705055590518146, 'fraction_numerical': 0.013215859030837005, 'mean_word_length': 3.200440528634361, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7462296', 'n_tokens_mistral': 3252, 'n_tokens_neox': 3142, 'n_words': 718}