protected String decodeQueryPercents()

in velocity-tools-generic/src/main/java/org/apache/velocity/tools/generic/LinkTool.java [1760:1822]


    protected String decodeQueryPercents(String url)
    {
        StringBuilder out = new StringBuilder(url.length());
        boolean inQuery = false, havePercent = false, haveTwo = false;
        for (int i=0; i<url.length(); i++)
        {
            char c = url.charAt(i);
            if (inQuery)
            {
                if (havePercent)
                {
                    if (haveTwo)
                    {
                        out.append('%');
                        if (c != '5')
                        {
                            out.append('2').append(c);
                        }
                        havePercent = haveTwo = false;
                    }
                    else if (c == '2')
                    {
                        haveTwo = true;
                    }
                    else
                    {
                        out.append('%').append(c);
                        havePercent = false;
                    }
                }
                else if (c == '%')
                {
                    havePercent = true;
                }
                else
                {
                    out.append(c);
                }
                if (c == '#')
                {
                    inQuery = false;
                }
            }
            else
            {
                out.append(c);
                if (c == '?')
                {
                    inQuery = true;
                }
            }
        }
        // if things ended part way
        if (havePercent)
        {
            out.append('%');
            if (haveTwo)
            {
                out.append('2');
            }
        }
        return out.toString();
    }