static PatternAndParamCount globToRegex(String glob, int numGroupsToSkip) {
        int numGroups = 0;
        if (glob.charAt(0) != '/') {
            glob = "/**/" + glob;
            numGroupsToSkip++; // Do not capture the prefix a user did not specify.
        }

        final int pathPatternLen = glob.length();
        final StringBuilder buf = new StringBuilder(pathPatternLen).append("^/");
        int asterisks = 0;
        char beforeAsterisk = '/';

        for (int i = 1; i < pathPatternLen; i++) { // Start from '1' to skip the first '/'.
            final char c = glob.charAt(i);
            if (c == '*') {
                asterisks++;
                if (asterisks > 2) {
                    throw new IllegalArgumentException(
                            "contains a path pattern with invalid wildcard characters: " + glob +
                            " (only * and ** are allowed)");
                }
                continue;
            }

            switch (asterisks) {
                case 1:
                    if (numGroupsToSkip <= 0) {
                        buf.append('(');
                        numGroups++;
                    }

                    // Handle '/*/' specially.
                    if (beforeAsterisk == '/' && c == '/') {
                        buf.append("[^/]+");
                    } else {
                        buf.append("[^/]*");
                    }

                    if (numGroupsToSkip <= 0) {
                        buf.append(')');
                    } else {
                        numGroupsToSkip--;
                    }
                    break;
                case 2:
                    // Handle '/**/' specially.
                    if (beforeAsterisk == '/' && c == '/') {
                        buf.append("(?:");

                        if (numGroupsToSkip <= 0) {
                            buf.append('(');
                            numGroups++;
                        }

                        buf.append(".+");

                        if (numGroupsToSkip <= 0) {
                            buf.append(')');
                        } else {
                            numGroupsToSkip--;
                        }

                        buf.append("/)?");
                        asterisks = 0;
                        continue;
                    } else {
                        if (numGroupsToSkip <= 0) {
                            buf.append('(');
                            numGroups++;
                        }

                        buf.append(".*");

                        if (numGroupsToSkip <= 0) {
                            buf.append(')');
                        } else {
                            numGroupsToSkip--;
                        }
                        break;
                    }
            }

            asterisks = 0;
            beforeAsterisk = c;

            switch (c) {
                case '\\':
                case '.':
                case '^':
                case '$':
                case '?':
                case '+':
                case '{':
                case '}':
                case '[':
                case ']':
                case '(':
                case ')':
                case '|':
                    buf.append('\\');
                    buf.append(c);
                    break;
                default:
                    buf.append(c);
            }
        }

        // Handle the case where the pattern ends with asterisk(s).
        switch (asterisks) {
            case 1:
                if (numGroupsToSkip <= 0) {
                    buf.append('(');
                    numGroups++;
                }

                if (beforeAsterisk == '/') {
                    // '/*<END>'
                    buf.append("[^/]+");
                } else {
                    buf.append("[^/]*");
                }

                if (numGroupsToSkip <= 0) {
                    buf.append(')');
                }
                break;
            case 2:
                if (numGroupsToSkip <= 0) {
                    buf.append('(');
                    numGroups++;
                }

                buf.append(".*");

                if (numGroupsToSkip <= 0) {
                    buf.append(')');
                }
                break;
        }

        return new PatternAndParamCount(Pattern.compile(buf.append('$').toString()), numGroups);
    }
/" + glob;
        pathPattern = aGlob;
        paths = ImmutableList.of(pattern.pattern(), aGlob);
    }

    @Override
    PathMapping doWithPrefix(String prefix) {
        final String prefixGlob;
        int numGroupsToSkip = this.numGroupsToSkip;
        if (glob.startsWith("/")) {
            prefixGlob = concatPaths(prefix, glob);
        } else {
            prefixGlob = concatPaths(prefix + "*
core/src/main/java/com/linecorp/armeria/server/GlobPathMapping.java