<commit_before>package io.github.lonamiwebs.stringlate.classes.locales;

import android.support.annotation.NonNull;

import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LocaleString {

    final private String mLocale;
    final private String mLocaleDisplay;

    // Support a locale with any separator (i.e. "zh", "zh-TW", "zh_rTW", "zh/TW")
    private final static Pattern SANITIZE_PATTERN = Pattern.compile(
            "^(\\w{2})(?:[ -_/]r?(\\w{2}))?$", Pattern.CASE_INSENSITIVE);

    public LocaleString(@NonNull String locale) {
        mLocale = locale;
        mLocaleDisplay = getDisplay(locale);
    }

    public String getCode() {
        return mLocale;
    }

    @Override
    public String toString() {
        return mLocaleDisplay;
    }

    public static String getDisplay(@NonNull String locale) {
        // If there is an hyphen, then a country was also specified
        // Not sure why but the 'r' prefix is needed for the country
        if (locale.contains("-")) {
            for (Locale l : Locale.getAvailableLocales())
                if (!l.getCountry().isEmpty())
                    if (locale.equals(l.getLanguage() + "-r" + l.getCountry()))
                        return l.getDisplayName();
        } else {
            for (Locale l : Locale.getAvailableLocales())
                if (locale.equals(l.getLanguage()))
                    if (l.getCountry().isEmpty()) // Ensure that it's empty (issue #58)
                        return l.getDisplayName();
        }
        return locale;
    }

    // Useful when exporting to issue for example
    public static String getEnglishDisplay(@NonNull String locale) {
        if (locale.contains("-")) {
            for (Locale l : Locale.getAvailableLocales())
                if (!l.getCountry().isEmpty())
                    if (locale.equals(l.getLanguage() + "-r" + l.getCountry()))
                        return l.getDisplayName(Locale.ENGLISH);
        } else {
            for (Locale l : Locale.getAvailableLocales())
                if (locale.equals(l.getLanguage()))
                    if (l.getCountry().isEmpty())
                        return l.getDisplayName(Locale.ENGLISH);
        }
        return locale;
    }

    public static String getFullCode(@NonNull Locale locale) {
        String language = locale.getLanguage();
        String country = locale.getCountry();
        return country.isEmpty() ? language : language + "-r" + country;
    }

    public static boolean isValid(@NonNull String locale) {
        if (locale.contains("-")) {
            for (Locale l : Locale.getAvailableLocales())
                if (!l.getCountry().isEmpty())
                    if (locale.equals(l.getLanguage() + "-r" + l.getCountry()))
                        return true;
        } else {
            for (Locale l : Locale.getAvailableLocales())
                if (locale.equals(l.getLanguage()))
                    return true;
        }
        return false;
    }

    @NonNull
    public static String sanitizeLocale(String locale) {
        locale = locale.trim();
        Matcher m = SANITIZE_PATTERN.matcher(locale);
        if (m.find()) {
            String lang = m.group(1).toLowerCase();
            String country = m.group(2);
            if (country != null)
                return String.format("%s-r%s", lang, country.toUpperCase());
            else
                return lang;
        }<commit_msg>Enhance LocaleString.isValid()<commit_after>package io.github.lonamiwebs.stringlate.classes.locales;

import android.support.annotation.NonNull;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LocaleString {

    final private String mLocale;
    final private String mLocaleDisplay;

    // Support a locale with any separator (i.e. "zh", "zh-TW", "zh_rTW", "zh/TW")
    private final static Pattern SANITIZE_PATTERN = Pattern.compile(
            "^(\\w{2})(?:[ -_/]r?(\\w{2}))?$", Pattern.CASE_INSENSITIVE);

    public LocaleString(@NonNull String locale) {
        mLocale = locale;
        mLocaleDisplay = getDisplay(locale);
    }

    public String getCode() {
        return mLocale;
    }

    @Override
    public String toString() {
        return mLocaleDisplay;
    }

    public static String getDisplay(@NonNull String locale) {
        // If there is an hyphen, then a country was also specified
        // Not sure why but the 'r' prefix is needed for the country
        if (locale.contains("-")) {
            for (Locale l : Locale.getAvailableLocales())
                if (!l.getCountry().isEmpty())
                    if (locale.equals(l.getLanguage() + "-r" + l.getCountry()))
                        return l.getDisplayName();
        } else {
            for (Locale l : Locale.getAvailableLocales())
                if (locale.equals(l.getLanguage()))
                    if (l.getCountry().isEmpty()) // Ensure that it's empty (issue #58)
                        return l.getDisplayName();
        }
        return locale;
    }

    // Useful when exporting to issue for example
    public static String getEnglishDisplay(@NonNull String locale) {
        if (locale.contains("-")) {
            for (Locale l : Locale.getAvailableLocales())
                if (!l.getCountry().isEmpty())
                    if (locale.equals(l.getLanguage() + "-r" + l.getCountry()))
                        return l.getDisplayName(Locale.ENGLISH);
        } else {
            for (Locale l : Locale.getAvailableLocales())
                if (locale.equals(l.getLanguage()))
                    if (l.getCountry().isEmpty())
                        return l.getDisplayName(Locale.ENGLISH);
        }
        return locale;
    }

    public static String getFullCode(@NonNull Locale locale) {
        String language = locale.getLanguage();
        String country = locale.getCountry();
        return country.isEmpty() ? language : language + "-r" + country;
    }

    public static boolean isValid(@NonNull String locale) {
        if (locale.contains("-")) {
            final String[] parts = locale.split("-");
            return isValid(new Locale(parts[0], parts[1].substring(1)));
        }

        return isValid(new Locale(locale));
    }

    private static boolean isValid(final Locale locale) {
        try {
            return locale.getISO3Language() != null && locale.getISO3Country() != null;
        } catch (MissingResourceException ignored) {
            return false;
        }
    }

    @NonNull
    public static String sanitizeLocale(String locale) {
        locale = locale.trim();
        Matcher m = SANITIZE_PATTERN.matcher(locale);
        if (m.find()) {
            String lang = m.group(1).toLowerCase();
            String country = m.group(2);
            if (country != null)
                return String.format("%s-r%s", lang, country.toUpperCase());
            else
                return lang;
        }
{'fraction_non_alphanumeric': 0.12042033513206475, 'fraction_numerical': 0.0024140869071286567, 'mean_word_length': 1.9830580262600592, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5531977', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1877, 'n_words': 488}