<commit_before><?php

/**
 *    Copyright 2015-2017 ppy Pty. Ltd.
 *
 *    This file is part of osu!web. osu!web is distributed with the hope of
 *    attracting more community contributions to the core ecosystem of osu!.
 *
 *    osu!web is free software: you can redistribute it and/or modify
 *    it under the terms of the Affero GNU General Public License version 3
 *    as published by the Free Software Foundation.
 *
 *    osu!web is distributed 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 osu!web.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Like array_search but returns null if not found instead of false.
 * Strict mode only.
 */
function array_search_null($value, $array)
{
    $key = array_search($value, $array, true);

    if ($key !== false) {
        return $key;
    }
}

function background_image($url, $proxy = true)
{
    if (!present($url)) {
        return '';
    }

    $url = $proxy ? proxy_image($url) : $url;

    return sprintf(' style="background-image:url(\'%s\');" ', e($url));
}

function es_query_and_words($words)
{
    $parts = preg_split("/\s+/", $words, null, PREG_SPLIT_NO_EMPTY);

    if (empty($parts)) {
        return;
    }

    $partsEscaped = [];

    foreach ($parts as $part) {
        $partsEscaped[] = str_replace('-', '%2D', urlencode(strtolower($part)));
    }

    return implode(' AND ', $partsEscaped);
}

function flag_path($country)
{
    return '/images/flags/'.$country.'.png';
}

function get_valid_locale($requestedLocale)
{
    if (in_array($requestedLocale, config('app.available_locales'), true)) {
        return $requestedLocale;
    }

    return array_first(
        config('app.available_locales'),
        function ($value) use ($requestedLocale) {
            return starts_with($requestedLocale, $value);
        },
        config('app.fallback_locale')
    );
}

function html_excerpt($body, $limit = 300)
{
    // not using strip_tags because <br> and <p> needs to be converted to space
    $body = preg_replace('#<[^>]+>#', ' ', $body);

    if (strlen($body) < $limit) {
        return $body;
    }

    return substr($body, 0, $limit).'...';
}

function json_date($date)
{
    return json_time($date->startOfDay());
}

function json_time($time)
{
    if ($time !== null) {
        return $time->toIso8601String();
    }
}

function locale_flag($locale)
{
    return App\Libraries\LocaleMeta::flagFor($locale);
}

function locale_name($locale)
{
    return App\Libraries\LocaleMeta::nameFor($locale);
}

function locale_for_moment($locale)
{
    if ($locale === 'en') {
        return;<commit_msg>Cut with multibyte-safe function<commit_after><?php

/**
 *    Copyright 2015-2017 ppy Pty. Ltd.
 *
 *    This file is part of osu!web. osu!web is distributed with the hope of
 *    attracting more community contributions to the core ecosystem of osu!.
 *
 *    osu!web is free software: you can redistribute it and/or modify
 *    it under the terms of the Affero GNU General Public License version 3
 *    as published by the Free Software Foundation.
 *
 *    osu!web is distributed 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 osu!web.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * Like array_search but returns null if not found instead of false.
 * Strict mode only.
 */
function array_search_null($value, $array)
{
    $key = array_search($value, $array, true);

    if ($key !== false) {
        return $key;
    }
}

function background_image($url, $proxy = true)
{
    if (!present($url)) {
        return '';
    }

    $url = $proxy ? proxy_image($url) : $url;

    return sprintf(' style="background-image:url(\'%s\');" ', e($url));
}

function es_query_and_words($words)
{
    $parts = preg_split("/\s+/", $words, null, PREG_SPLIT_NO_EMPTY);

    if (empty($parts)) {
        return;
    }

    $partsEscaped = [];

    foreach ($parts as $part) {
        $partsEscaped[] = str_replace('-', '%2D', urlencode(strtolower($part)));
    }

    return implode(' AND ', $partsEscaped);
}

function flag_path($country)
{
    return '/images/flags/'.$country.'.png';
}

function get_valid_locale($requestedLocale)
{
    if (in_array($requestedLocale, config('app.available_locales'), true)) {
        return $requestedLocale;
    }

    return array_first(
        config('app.available_locales'),
        function ($value) use ($requestedLocale) {
            return starts_with($requestedLocale, $value);
        },
        config('app.fallback_locale')
    );
}

function html_excerpt($body, $limit = 300)
{
    // not using strip_tags because <br> and <p> needs to be converted to space
    $body = preg_replace('#<[^>]+>#', ' ', $body);

    if (strlen($body) < $limit) {
        return $body;
    }

    return mb_substr($body, 0, $limit).'...';
}

function json_date($date)
{
    return json_time($date->startOfDay());
}

function json_time($time)
{
    if ($time !== null) {
        return $time->toIso8601String();
    }
}

function locale_flag($locale)
{
    return App\Libraries\LocaleMeta::flagFor($locale);
}

function locale_name($locale)
{
    return App\Libraries\LocaleMeta::nameFor($locale);
}

function locale_for_moment($locale)
{
    if ($locale === 'en') {
        return;
{'fraction_non_alphanumeric': 0.1422277053225238, 'fraction_numerical': 0.00634473034896017, 'mean_word_length': 3.1393143690736687, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '4375342', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1777, 'n_words': 500}