Java Code Examples for jdk.nashorn.internal.runtime.regexp.RegExpResult#getGroups()

The following examples show how to use jdk.nashorn.internal.runtime.regexp.RegExpResult#getGroups() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NativeRegExp.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
NativeArray split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 2
Source File: NativeRegExp.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
NativeArray split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 3
Source File: NativeRegExp.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
NativeArray split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 4
Source File: NativeRegExp.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
NativeArray split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 5
Source File: NativeRegExp.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
NativeArray split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 6
Source File: NativeRegExp.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
NativeArray split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 7
Source File: NativeRegExp.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
Object split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 8
Source File: NativeRegExp.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
Object split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 9
Source File: NativeRegExp.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
NativeArray split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}
 
Example 10
Source File: NativeRegExp.java    From nashorn with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Breaks up a string into an array of substrings based on a regular
 * expression or fixed string.
 *
 * @param string String to match.
 * @param limit  Split limit.
 * @return Array of substrings.
 */
Object split(final String string, final long limit) {
    if (limit == 0L) {
        return new NativeArray();
    }

    final List<Object> matches = new ArrayList<>();

    RegExpResult match;
    final int inputLength = string.length();
    int splitLastLength = -1;
    int splitLastIndex = 0;
    int splitLastLastIndex = 0;

    while ((match = execSplit(string, splitLastIndex)) != null) {
        splitLastIndex = match.getIndex() + match.length();

        if (splitLastIndex > splitLastLastIndex) {
            matches.add(string.substring(splitLastLastIndex, match.getIndex()));
            final Object[] groups = match.getGroups();
            if (groups.length > 1 && match.getIndex() < inputLength) {
                for (int index = 1; index < groups.length && matches.size() < limit; index++) {
                    matches.add(groups[index]);
                }
            }

            splitLastLength = match.length();

            if (matches.size() >= limit) {
                break;
            }
        }

        // bump the index to avoid infinite loop
        if (splitLastIndex == splitLastLastIndex) {
            splitLastIndex++;
        } else {
            splitLastLastIndex = splitLastIndex;
        }
    }

    if (matches.size() < limit) {
        // check special case if we need to append an empty string at the
        // end of the match
        // if the lastIndex was the entire string
        if (splitLastLastIndex == string.length()) {
            if (splitLastLength > 0 || execSplit("", 0) == null) {
                matches.add("");
            }
        } else {
            matches.add(string.substring(splitLastLastIndex, inputLength));
        }
    }

    return new NativeArray(matches.toArray());
}