package greed.template;

import com.floreysoft.jmte.NamedRenderer;
import com.floreysoft.jmte.RenderFormatInfo;

import java.util.Locale;

/**
 * Greed is good! Cheers!
 */
public class StringUtilRenderer implements NamedRenderer {
    @Override
    public String render(Object o, String param, Locale locale) {
        if (o instanceof String) {
            String result = (String) o;
            for (String func: param.split(",")) {
                if (func.trim().equals("lower")) {
                    result = result.toLowerCase();
                }
                else if (func.trim().equals("upfirst")) {
                    if (result.length() > 0)
                        result = result.substring(0, 1).toUpperCase() + result.substring(1).toLowerCase();
                }
                else if (func.trim().equals("removespace")) {
                    result = result.replaceAll("\\s", "");
                }
                else if (func.trim().equals("unquote")) {
                    if (result.length() >= 2 && result.charAt(0) == '"' && result.charAt(result.length() - 1) == '"')
                        result = result.substring(1, result.length() - 1);
                }
                else if (func.trim().equals("abbr")) {
                    String[] tokens = result.split("\\s+");
                    StringBuilder abbr = new StringBuilder();
                    for (String tok: tokens) {
                        if (allDigits(tok) || allUppercaseOrDigits(tok))
                            abbr.append(tok);
                        else
                            abbr.append(tok.substring(0, 1).toUpperCase());
                    }
                    result = abbr.toString();
                }
            }
            return result;
        }
        return "";
    }

    @Override
    public String getName() {
        return "string";
    }

    private boolean allDigits(String s) {
        for (int i = 0; i < s.length(); ++i)
            if (!Character.isDigit(s.charAt(i)))
                return false;
        return true;
    }

    private boolean allUppercaseOrDigits(String s) {
        for (int i = 0; i < s.length(); ++i)
            if (!Character.isUpperCase(s.charAt(i)) && !Character.isDigit(s.charAt(i)))
                return false;
        return true;
    }

    @Override
    public RenderFormatInfo getFormatInfo() {
        return null;
    }

    @Override
    public Class<?>[] getSupportedClasses() {
        return new Class<?>[]{String.class};
    }
}

@@ -132,16 +132,81 @@
 ocale;%0A%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A%0A
 /**%0A * G
@@ -1763,24 +1763,1511 @@
 toString();%0A
+                %7D else if (func.trim().startsWith(%22contestcategory%22)) %7B%0A                    int separate = 25;%0A                    try %7B%0A                        String s = func.trim().substring(%22contestcategory%22.length());%0A                        separate = Integer.parseInt(s); %0A                    %7D catch (NumberFormatException nfe) %7B%0A                    %7D%0A                    if (result.contains(%22TCHS%22)) %7B%0A                        result = %22TCHS%22;%0A                    %7D else if (result.matches(%22(?i).*(TCO%7C(top%5C%5Cs*coder%5C%5Cs*open)).*%22)) %7B%0A                        result = %22TCO%22;%0A                    %7D else if (result.contains(%22TCCC%22)) %7B%0A                        result = %22TCCC%22;%0A                    %7D else %7B%0A                        String pattern = %22(?i).*(SRM%7C(single%5C%5Cs*round%5C%5Cs*match))%5C%5Cs*(%5C%5Cd+).*%22;%0A                        Pattern r = Pattern.compile(pattern);%0A                        Matcher m = r.matcher(result);%0A                        if (m.find( )) %7B%0A                            int n = 0;%0A                            try %7B%0A                                n = Integer.parseInt( m.group(3) );%0A                            %7D catch (NumberFormatException nfe) %7B%0A                            %7D%0A                            int a = n - n %25 separate;%0A                            int b = a + separate - 1;%0A                            result = %22SRM %22+a+%22-%22+b;%0A                        %7D else %7B%0A                            result = %22Other%22;%0A                        %7D%0A                    %7D%0A
             

Add "contestcategory" renderer.