<commit_before>/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.protocol.irc;

import net.java.sip.communicator.util.*;

/**
 * Some IRC-related utility methods.
 *
 * @author <NAME>
 */
public final class Utils
{
    /**
     * Logger.
     */
    private static final Logger LOGGER = Logger.getLogger(Utils.class);

    /**
     * Index indicating the end of the color code.
     */
    private static final int INDEX_END_BACKGROUND_COLOR_CODE = 3;

    /**
     * Private constructor since we do not need to construct anything.
     */
    private Utils()
    {
    }

    /**
     * Parse IRC text message and process possible control codes.
     *
     * TODO Support for color 99 (Transparent)
     *
     * @param text the message
     * @return returns the processed message or null if text message was null,
     *         since there is nothing to modify there
     */
    public static String parseIrcMessage(final String text)
    {
        if (text == null)
        {
            return null;
        }

        FormattedTextBuilder builder = new FormattedTextBuilder();
        for (int i = 0; i < text.length(); i++)
        {
            char val = text.charAt(i);
            switch (val)
            {
            case ControlChar.Bold.CODE:
                if (builder.isActive(ControlChar.Bold.class))
                {
                    builder.cancel(ControlChar.Bold.class, true);
                }
                else
                {
                    builder.apply(new ControlChar.Bold());
                }
                break;
            case ControlChar.Italics.CODE:
                if (builder.isActive(ControlChar.Italics.class))
                {
                    builder.cancel(ControlChar.Italics.class, true);
                }
                else
                {
                    builder.apply(new ControlChar.Italics());
                }
                break;
            case ControlChar.Underline.CODE:
                if (builder.isActive(ControlChar.Underline.class))
                {
                    builder.cancel(ControlChar.Underline.class, true);
                }
                else
                {
                    builder.apply(new ControlChar.Underline());
                }
                break;
            case ControlChar.ColorFormat.CODE:
                // first parse foreground color code
                Color foreground = parseForegroundColor(text.substring(i + 1));
                if (foreground == null)
                {
                    builder.cancel(ControlChar.ColorFormat.class, false);
                }
                else
                {
                    i += 2;
                    Color background =
                        parseBackgroundColor(text.substring(i + 1));
                    if (background != null)
                    {
                        i += INDEX_END_BACKGROUND_COLOR_CODE;
                    }
                    builder.apply(new ControlChar.ColorFormat(foreground,
                        background));
                }
                break;
            case ControlChar.Normal.CODE:
                builder.cancelAll();
                break;
            default:
                // value is a normal character, just append
                builder.append(val);
                break;
            }
        }<commit_msg>Added constant for foreground color code index.<commit_after>/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.protocol.irc;

import net.java.sip.communicator.util.*;

/**
 * Some IRC-related utility methods.
 *
 * @author <NAME>
 */
public final class Utils
{
    /**
     * Logger.
     */
    private static final Logger LOGGER = Logger.getLogger(Utils.class);

    /**
     * Index indicating the end of the foreground color code.
     */
    private static final int INDEX_END_FOREGROUND_COLOR_CODE = 2;

    /**
     * Index indicating the end of the background color code. (additional 1 for
     * comma separating foreground color code from background color code)
     */
    private static final int INDEX_END_BACKGROUND_COLOR_CODE = 3;

    /**
     * Private constructor since we do not need to construct anything.
     */
    private Utils()
    {
    }

    /**
     * Parse IRC text message and process possible control codes.
     *
     * TODO Support for color 99 (Transparent)
     *
     * @param text the message
     * @return returns the processed message or null if text message was null,
     *         since there is nothing to modify there
     */
    public static String parseIrcMessage(final String text)
    {
        if (text == null)
        {
            return null;
        }

        FormattedTextBuilder builder = new FormattedTextBuilder();
        for (int i = 0; i < text.length(); i++)
        {
            char val = text.charAt(i);
            switch (val)
            {
            case ControlChar.Bold.CODE:
                if (builder.isActive(ControlChar.Bold.class))
                {
                    builder.cancel(ControlChar.Bold.class, true);
                }
                else
                {
                    builder.apply(new ControlChar.Bold());
                }
                break;
            case ControlChar.Italics.CODE:
                if (builder.isActive(ControlChar.Italics.class))
                {
                    builder.cancel(ControlChar.Italics.class, true);
                }
                else
                {
                    builder.apply(new ControlChar.Italics());
                }
                break;
            case ControlChar.Underline.CODE:
                if (builder.isActive(ControlChar.Underline.class))
                {
                    builder.cancel(ControlChar.Underline.class, true);
                }
                else
                {
                    builder.apply(new ControlChar.Underline());
                }
                break;
            case ControlChar.ColorFormat.CODE:
                // first parse foreground color code
                Color foreground = parseForegroundColor(text.substring(i + 1));
                if (foreground == null)
                {
                    builder.cancel(ControlChar.ColorFormat.class, false);
                }
                else
                {
                    i += INDEX_END_FOREGROUND_COLOR_CODE;
                    Color background =
                        parseBackgroundColor(text.substring(i + 1));
                    if (background != null)
                    {
                        i += INDEX_END_BACKGROUND_COLOR_CODE;
                    }
                    builder.apply(new ControlChar.ColorFormat(foreground,
                        background));
                }
                break;
            case ControlChar.Normal.CODE:
                builder.cancelAll();
                break;
            default:
                // value is a normal character, just append
                builder.append(val);
                break;
            }
        }
{'fraction_non_alphanumeric': 0.08153509969953565, 'fraction_numerical': 0.00204862059546572, 'mean_word_length': 1.2671826625386997, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '529322', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1718, 'n_words': 450}