package heufybot.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class StringUtils
{
    public static String join(Collection<String> strings, String sep)
    {
        StringBuilder builder = new StringBuilder();
        Iterator<String> itr = strings.iterator();
        while (itr.hasNext())
        {
            builder.append(itr.next());
            if (itr.hasNext())
            {
                builder.append(sep);
            }
        }
        return builder.toString();
    }

    public static List<String> parseStringtoList(String input, String split)
    {
        List<String> list = new ArrayList<String>();
        String[] splitted = input.split(split);
        for (String element : splitted)
        {
            list.add(element);
        }
        return list;
    }

    public static String escapeRegex(String regex)
    {
        return regex.replaceAll("\\*", "\\*").replaceAll("\\+", "\\\\+").replaceAll("\\|", "\\\\|")
                .replaceAll("\\{", "\\\\{").replaceAll("\\[", "\\\\[").replaceAll("\\(", "\\\\(")
                .replaceAll("\\)", "\\\\)").replaceAll("\\^", "\\\\^").replaceAll("\\$", "\\\\$")
                .replaceAll("\\.", "\\\\.").replaceAll("\\#", "\\\\#").replaceAll("\\~", "")
                .replaceAll(" ", "");
    }

    public static String toValid3ByteUTF8String(String s)
    {
        int length = s.length();
        StringBuilder b = new StringBuilder(length);
        for (int offset = 0; offset < length;)
        {
            final int codepoint = s.codePointAt(offset);

            if (codepoint > new String("\uFFFF").codePointAt(0))
            {
                b.append("\uFFFD");
            }
            else
            {
                if (Character.isValidCodePoint(codepoint))
                {
                    b.appendCodePoint(codepoint);
                }
                else
                {
                    b.append("\uFFFD");
                }
            }
            offset += Character.charCount(codepoint);
        }
        return b.toString();
    }

    public static String removeColors(String line)
    {
        int length = line.length();
        StringBuilder buffer = new StringBuilder();
        int i = 0;
        while (i < length)
        {
            char ch = line.charAt(i);
            if (ch == '\u0003')
            {
                i++;
                if (i < length)
                {
                    ch = line.charAt(i);
                    if (Character.isDigit(ch))
                    {
                        i++;
                        if (i < length)
                        {
                            ch = line.charAt(i);
                            if (Character.isDigit(ch))
                            {
                                i++;
                            }
                        }
                        if (i < length)
                        {
                            ch = line.charAt(i);
                            if (ch == ',')
                            {
                                i++;
                                if (i < length)
                                {
                                    ch = line.charAt(i);
                                    if (Character.isDigit(ch))
                                    {
                                        i++;
                                        if (i < length)
                                        {
                                            ch = line.charAt(i);
                                            if (Character.isDigit(ch))
                                            {
                                                i++;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        i--;
                                    }
                                }
                                else
                                {
                                    i--;
                                }
                            }
                        }
                    }
                }
            }
            else if (ch == '\u000f')
            {
                i++;
            }
            else
            {
                buffer.append(ch);
                i++;
            }
        }
        return buffer.toString();
    }

    public static String removeFormatting(String line)
    {
        int length = line.length();
        StringBuilder buffer = new StringBuilder();
        for (int i = 0; i < length; i++)
        {
            char ch = line.charAt(i);
            if (ch == '\u000f' || ch == '\u0002' || ch == '\u001f' || ch == '\u0016')
            {
                // Don't add this character.
            }
            else
            {
                buffer.append(ch);
            }
        }
        return buffer.toString();
    }

    public static String removeFormattingAndColors(String line)
    {
        return removeFormatting(removeColors(line));
    }

    public static int tryParseInt(String intString)
    {
        try
        {
            return Integer.parseInt(intString);
        }
        catch (NumberFormatException e)
        {
            return -1;
        }
    }

    public static long tryParseLong(String longString)
    {
        try
        {
            return Long.parseLong(longString);
        }
        catch (NumberFormatException e)
        {
            return -1;
        }
    }
}

@@ -5698,10 +5698,226 @@
 %7D%0A    %7D%0A
+    %0A    public static double tryParseDouble(String doubleString)%0A    %7B%0A    %09try%0A    %09%7B%0A    %09%09return Double.parseDouble(doubleString);%0A    %09%7D%0A    %09catch (NumberFormatException e)%0A    %09%7B%0A    %09%09return -1;%0A    %09%7D%0A    %7D%0A
 %7D%0A

Add a util function to convert strings to longs