private static String toRegexPattern()

in gogo/runtime/src/main/java/org/apache/felix/gogo/runtime/Expander.java [2486:2620]


    private static String toRegexPattern(String str, boolean shortest)
    {
        boolean inGroup = false;
        StringBuilder sb = new StringBuilder();
        int index = 0;
        while (index < str.length())
        {
            char ch = str.charAt(index++);
            switch (ch)
            {
                case '*':
                    sb.append(shortest ? ".*?" : ".*");
                    break;
                case ',':
                    if (inGroup)
                    {
                        sb.append(")|(?:");
                    }
                    else
                    {
                        sb.append(',');
                    }
                    break;
                case '?':
                    sb.append(".");
                    break;
                case '[':
                    sb.append("[");
                    if (next(str, index) == '^')
                    {
                        sb.append("\\^");
                        ++index;
                    }
                    else
                    {
                        if (next(str, index) == '!')
                        {
                            sb.append('^');
                            ++index;
                        }
                        if (next(str, index) == '-')
                        {
                            sb.append('-');
                            ++index;
                        }
                    }
                    boolean inLeft = false;
                    char left = 0;
                    while (index < str.length())
                    {
                        ch = str.charAt(index++);
                        if (ch == ']')
                        {
                            break;
                        }
                        if (ch == '\\' || ch == '[' || ch == '&' && next(str, index) == '&')
                        {
                            sb.append('\\');
                        }
                        sb.append(ch);
                        if (ch == '-')
                        {
                            if (!inLeft)
                            {
                                throw new PatternSyntaxException("Invalid range", str, index - 1);
                            }
                            if ((ch = next(str, index++)) == EOL || ch == ']')
                            {
                                break;
                            }
                            if (ch < left)
                            {
                                throw new PatternSyntaxException("Invalid range", str, index - 3);
                            }
                            sb.append(ch);
                            inLeft = false;
                        }
                        else
                        {
                            inLeft = true;
                            left = ch;
                        }
                    }
                    if (ch != ']')
                    {
                        throw new PatternSyntaxException("Missing \']", str, index - 1);
                    }
                    sb.append("]");
                    break;
                case '\\':
                    if (index == str.length())
                    {
                        throw new PatternSyntaxException("No character to escape", str, index - 1);
                    }
                    char ch2 = str.charAt(index++);
                    if (isGlobMeta(ch2) || isRegexMeta(ch2))
                    {
                        sb.append('\\');
                    }
                    sb.append(ch2);
                    break;
                case '{':
                    if (inGroup)
                    {
                        throw new PatternSyntaxException("Cannot nest groups", str, index - 1);
                    }
                    sb.append("(?:(?:");
                    inGroup = true;
                    break;
                case '}':
                    if (inGroup)
                    {
                        sb.append("))");
                        inGroup = false;
                    }
                    else
                    {
                        sb.append('}');
                    }
                    break;
                default:
                    if (isRegexMeta(ch))
                    {
                        sb.append('\\');
                    }
                    sb.append(ch);
                    break;
            }
        }
        if (inGroup)
        {
            throw new PatternSyntaxException("Missing \'}", str, index - 1);
        }
        return sb.toString();
    }