undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

View file

@ -0,0 +1,951 @@
//
// Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
// DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
//
// This code is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License version 2 only, as
// published by the Free Software Foundation.
//
// This code is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// version 2 for more details (a copy is included in the LICENSE file that
// accompanied this code).
//
// You should have received a copy of the GNU General Public License version
// 2 along with this work; if not, write to the Free Software Foundation,
// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
//
// Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
// or visit www.oracle.com if you need additional information or have any
// questions.
//
//
// This file contains test cases with BMP characters for regular expressions.
// A test case consists of three lines:
// The first line is a pattern used in the test
// The second line is the input to search for the pattern in
// The third line is a concatentation of the match, the number of groups,
// and the contents of the first four subexpressions.
// Empty lines and lines beginning with comment slashes are ignored.
// Test unsetting of backed off groups
^(\u3042)?\u3042
\u3042
true \u3042 1
^(\u3042\u3042(\u3043\u3043)?)+$
\u3042\u3042\u3043\u3043\u3042\u3042
true \u3042\u3042\u3043\u3043\u3042\u3042 2 \u3042\u3042 \u3043\u3043
((\u3042|\u3043)?\u3043)+
\u3043
true \u3043 2 \u3043
(\u3042\u3042\u3042)?\u3042\u3042\u3042
\u3042\u3042\u3042
true \u3042\u3042\u3042 1
^(\u3042(\u3043)?)+$
\u3042\u3043\u3042
true \u3042\u3043\u3042 2 \u3042 \u3043
^(\u3042(\u3043(\u3044)?)?)?\u3042\u3043\u3044
\u3042\u3043\u3044
true \u3042\u3043\u3044 3
^(\u3042(\u3043(\u3044))).*
\u3042\u3043\u3044
true \u3042\u3043\u3044 3 \u3042\u3043\u3044 \u3043\u3044 \u3044
// use of x modifier
\u3042\u3043\u3044(?x)\u3043la\u3049
\u3042\u3043\u3044\u3043la\u3049
true \u3042\u3043\u3044\u3043la\u3049 0
\u3042\u3043\u3044(?x) bla\u3049
\u3042\u3043\u3044bla\u3049
true \u3042\u3043\u3044bla\u3049 0
\u3042\u3043\u3044(?x) bla\u3049 ble\u3044\u3049
\u3042\u3043\u3044bla\u3049ble\u3044\u3049
true \u3042\u3043\u3044bla\u3049ble\u3044\u3049 0
\u3042\u3043\u3044(?x) bla\u3049 # ignore comment
\u3042\u3043\u3044bla\u3049
true \u3042\u3043\u3044bla\u3049 0
// Simple alternation
\u3042|\u3043
\u3042
true \u3042 0
\u3042|\u3043
\u305B
false 0
\u3042|\u3043
\u3043
true \u3043 0
\u3042|\u3043|\u3044\u3045
\u3044\u3045
true \u3044\u3045 0
\u3042|\u3042\u3045
\u3042\u3045
true \u3042 0
\u305B(\u3042|\u3042\u3044)\u3043
\u305B\u3042\u3044\u3043
true \u305B\u3042\u3044\u3043 1 \u3042\u3044
// Simple char class
[\u3042\u3043\u3044]+
\u3042\u3043\u3042\u3043\u3042\u3043
true \u3042\u3043\u3042\u3043\u3042\u3043 0
[\u3042\u3043\u3044]+
\u3045\u3046\u3047\u3048
false 0
[\u3042\u3043\u3044]+[\u3045\u3046\u3047]+[\u3048\u3049\u304A]+
\u305B\u305B\u305B\u3042\u3042\u3045\u3045\u3048\u3048\u305B\u305B\u305B
true \u3042\u3042\u3045\u3045\u3048\u3048 0
// Range char class
[\u3042-\u3048]+
\u305B\u305B\u305B\u3048\u3048\u3048
true \u3048\u3048\u3048 0
[\u3042-\u3048]+
mmm
false 0
[\u3042-]+
\u305B\u3042-9\u305B
true \u3042- 0
[\u3042-\\u4444]+
\u305B\u3042-9\u305B
true \u305B\u3042 0
// Negated char class
[^\u3042\u3043\u3044]+
\u3042\u3043\u3042\u3043\u3042\u3043
false 0
[^\u3042\u3043\u3044]+
\u3042\u3042\u3042\u3043\u3043\u3043\u3044\u3044\u3044\u3045\u3046\u3047\u3048
true \u3045\u3046\u3047\u3048 0
// Making sure a ^ not in first position matches literal ^
[\u3042\u3043\u3044^\u3043]
\u3043
true \u3043 0
[\u3042\u3043\u3044^\u3043]
^
true ^ 0
// Class union and intersection
[\u3042\u3043\u3044[\u3045\u3046\u3047]]
\u3043
true \u3043 0
[\u3042\u3043\u3044[\u3045\u3046\u3047]]
\u3046
true \u3046 0
[\u3042-\u3045[0-9][\u304e-\u3051]]
\u3042
true \u3042 0
[\u3042-\u3045[0-9][\u304e-\u3051]]
\u3050
true \u3050 0
[\u3042-\u3045[0-9][\u304e-\u3051]]
4
true 4 0
[\u3042-\u3045[0-9][\u304e-\u3051]]
\u3046
false 0
[\u3042-\u3045[0-9][\u304e-\u3051]]
\u3056
false 0
[[\u3042-\u3045][0-9][\u304e-\u3051]]
\u3043
true \u3043 0
[[\u3042-\u3045][0-9][\u304e-\u3051]]
\u305B
false 0
[\u3042-\u3044[\u3045-\u3047[\u3048-\u304A]]]
\u3042
true \u3042 0
[\u3042-\u3044[\u3045-\u3047[\u3048-\u304A]]]
\u3046
true \u3046 0
[\u3042-\u3044[\u3045-\u3047[\u3048-\u304A]]]
\u3049
true \u3049 0
[\u3042-\u3044[\u3045-\u3047[\u3048-\u304A]]]
m
false 0
[\u3042-\u3044[\u3045-\u3047[\u3048-\u304A]]m]
m
true m 0
[\u3042\u3043\u3044[\u3045\u3046\u3047]\u3048\u3049\u304A]
\u3042
true \u3042 0
[\u3042\u3043\u3044[\u3045\u3046\u3047]\u3048\u3049\u304A]
\u3045
true \u3045 0
[\u3042\u3043\u3044[\u3045\u3046\u3047]\u3048\u3049\u304A]
\u3049
true \u3049 0
[\u3042\u3043\u3044[\u3045\u3046\u3047]\u3048\u3049\u304A]
w
false 0
[\u3042-\u3044&&[\u3045-\u3047]]
\u3042
false 0
[\u3042-\u3044&&[\u3045-\u3047]]
\u3046
false 0
[\u3042-\u3044&&[\u3045-\u3047]]
\u305B
false 0
[[\u3042-\u3044]&&[\u3045-\u3047]]
\u3042
false 0
[[\u3042-\u3044]&&[\u3045-\u3047]]
\u3046
false 0
[[\u3042-\u3044]&&[\u3045-\u3047]]
\u305B
false 0
[\u3042-\u3044&&\u3045-\u3047]
\u3042
false 0
[\u3042-\u304e&&\u304e-\u305B]
\u304e
true \u304e 0
[\u3042-\u304e&&\u304e-\u305B&&\u3042-\u3044]
\u304e
false 0
[\u3042-\u304e&&\u304e-\u305B&&\u3042-\u305B]
\u304e
true \u304e 0
[[\u3042-\u304e]&&[\u304e-\u305B]]
\u3042
false 0
[[\u3042-\u304e]&&[\u304e-\u305B]]
\u304e
true \u304e 0
[[\u3042-\u304e]&&[\u304e-\u305B]]
\u305B
false 0
[[\u3042-\u304e]&&[^\u3042-\u3044]]
\u3042
false 0
[[\u3042-\u304e]&&[^\u3042-\u3044]]
\u3045
true \u3045 0
[\u3042-\u304e&&[^\u3042-\u3044]]
\u3042
false 0
[\u3042-\u304e&&[^\u3042-\u3044]]
\u3045
true \u3045 0
[\u3042-\u3044\u3045-\u3047&&[\u3045-\u3047]]
\u3042
false 0
[\u3042-\u3044\u3045-\u3047&&[\u3045-\u3047]]
\u3046
true \u3046 0
[[\u3042-\u3044]&&\u3045-\u3047\u3042-\u3044]
\u3042
true \u3042 0
[[\u3042-\u3044]&&[\u3045-\u3047][\u3042-\u3044]]
\u3042
true \u3042 0
[[\u3042-\u3044][\u3045-\u3047]&&\u3042\u3043\u3044]
\u3042
true \u3042 0
[[\u3042-\u3044][\u3045-\u3047]&&\u3042\u3043\u3044[\u3045\u3046\u3047]]
\u3046
true \u3046 0
[[\u3042-\u3044]&&[\u3043-\u3045]&&[\u3044-\u3046]]
\u3042
false 0
[[\u3042-\u3044]&&[\u3043-\u3045]&&[\u3044-\u3046]]
\u3044
true \u3044 0
[[\u3042-\u3044]&&[\u3043-\u3045][\u3044-\u3046]&&[\u3056-\u305B]]
\u3044
false 0
[\u3042\u3043\u3044[^\u3043\u3044\u3045]]
\u3042
true \u3042 0
[\u3042\u3043\u3044[^\u3043\u3044\u3045]]
\u3045
false 0
[\u3042-\u3044&&\u3042-\u3045&&\u3042-\u3046\u3048\u3049\u304A]
\u3043
true \u3043 0
[\u3042-\u3044&&\u3042-\u3045&&\u3042-\u3046\u3048\u3049\u304A]
\u3048
false 0
[[\u3042[\u3043]]&&[\u3043[\u3042]]]
\u3042
true \u3042 0
[[\u3042]&&[\u3043][\u3044][\u3042]&&[^\u3045]]
\u3042
true \u3042 0
[[\u3042]&&[b][c][\u3042]&&[^d]]
\u3042
true \u3042 0
[[\u3042]&&[\u3043][\u3044][\u3042]&&[^\u3045]]
\u3045
false 0
[[[\u3042-\u3045]&&[\u3044-\u3047]]]
\u3042
false 0
[[[\u3042-\u3045]&&[\u3044-\u3047]]]
\u3044
true \u3044 0
[[[\u3042-\u3045]&&[\u3044-\u3047]]&&[\u3044]]
\u3044
true \u3044 0
[[[\u3042-\u3045]&&[\u3044-\u3047]]&&[\u3044]&&\u3044]
\u3044
true \u3044 0
[[[\u3042-\u3045]&&[\u3044-\u3047]]&&[\u3044]&&\u3044&&\u3044]
\u3044
true \u3044 0
[[[\u3042-\u3045]&&[\u3044-\u3047]]&&[\u3044]&&\u3044&&[\u3044\u3045\u3046]]
\u3044
true \u3044 0
[\u305B[\u3042\u3043\u3044&&\u3043\u3044\u3045]]
\u3044
true \u3044 0
[\u305B[\u3042\u3043\u3044&&\u3043\u3044\u3045]&&[\u3056-\u305B]]
\u305B
true \u305B 0
[\u3059[\u3042\u3043\u3044&&\u3043\u3044\u3045[\u305B]]&&[\u3056-\u305B]]
\u305B
false 0
[\u3059[[w\u305B]\u3042\u3043\u3044&&\u3043\u3044\u3045[\u305B]]&&[\u3056-\u305B]]
\u305B
true \u305B 0
[[\u3042\u3043\u3044]&&[\u3045\u3046\u3047]\u3042\u3043\u3044]
\u3042
true \u3042 0
[[\u3042\u3043\u3044]&&[\u3045\u3046\u3047]\u3059\u305A\u305B[\u3042\u3043\u3044]]
\u3042
true \u3042 0
\pL
\u3042
true \u3042 0
\pL
7
false 0
\p{L}
\u3042
true \u3042 0
\p{IsL}
\u3042
true \u3042 0
\p{InHiragana}
\u3042
true \u3042 0
\p{InHiragana}
\u0370
false 0
\pL\u3043\u3044
\u3042\u3043\u3044
true \u3042\u3043\u3044 0
\u3042[r\p{InGreek}]\u3044
\u3042\u0370\u3044
true \u3042\u0370\u3044 0
\u3042\p{InGreek}
\u3042\u0370
true \u3042\u0370 0
\u3042\P{InGreek}
\u3042\u0370
false 0
\u3042\P{InGreek}
\u3042\u3043
true \u3042\u3043 0
\u3042{^InGreek}
-
error
\u3042\p{^InGreek}
-
error
\u3042\P{^InGreek}
-
error
\u3042\p{InGreek}
\u3042\u0370
true \u3042\u0370 0
\u3042[\p{InGreek}]\u3044
\u3042\u0370\u3044
true \u3042\u0370\u3044 0
\u3042[\P{InGreek}]\u3044
\u3042\u0370\u3044
false 0
\u3042[\P{InGreek}]\u3044
\u3042\u3043\u3044
true \u3042\u3043\u3044 0
\u3042[{^InGreek}]\u3044
\u3042n\u3044
true \u3042n\u3044 0
\u3042[{^InGreek}]\u3044
\u3042\u305B\u3044
false 0
\u3042[\p{^InGreek}]\u3044
-
error
\u3042[\P{^InGreek}]\u3044
-
error
\u3042[\p{InGreek}]
\u3042\u0370
true \u3042\u0370 0
\u3042[r\p{InGreek}]\u3044
\u3042r\u3044
true \u3042r\u3044 0
\u3042[\p{InGreek}r]\u3044
\u3042r\u3044
true \u3042r\u3044 0
\u3042[r\p{InGreek}]\u3044
\u3042r\u3044
true \u3042r\u3044 0
\u3042[^\p{InGreek}]\u3044
\u3042\u0370\u3044
false 0
\u3042[^\P{InGreek}]\u3044
\u3042\u0370\u3044
true \u3042\u0370\u3044 0
\u3042[\p{InGreek}&&[^\u0370]]\u3044
\u3042\u0370\u3044
false 0
// Test the dot metacharacter
\u3042.\u3044.+
\u3042#\u3044%&
true \u3042#\u3044%& 0
\u3042\u3043.
\u3042\u3043\n
false 0
(?s)\u3042\u3043.
\u3042\u3043\n
true \u3042\u3043\n 0
\u3042[\p{L}&&[\P{InGreek}]]\u3044
\u3042\u6000\u3044
true \u3042\u6000\u3044 0
\u3042[\p{L}&&[\P{InGreek}]]\u3044
\u3042r\u3044
true \u3042r\u3044 0
\u3042[\p{L}&&[\P{InGreek}]]\u3044
\u3042\u0370\u3044
false 0
\u3042\p{InGreek}\u3044
\u3042\u0370\u3044
true \u3042\u0370\u3044 0
\u3042\p{Sc}
\u3042$
true \u3042$ 0
\W\w\W
rrrr#\u3048\u3048\u3048
false 0
\u3042\u3043\u3044[\s\u3045\u3046\u3047]*
\u3042\u3043\u3044 \u3045\u3046\u3047
true \u3042\u3043\u3044 \u3045\u3046\u3047 0
\u3042\u3043\u3044[\s\u305A-\u305B]*
\u3042\u3043\u3044 \u305A \u305B
true \u3042\u3043\u3044 \u305A \u305B 0
\u3042\u3043\u3044[\u3042-\u3045\s\u304e-\u3051]*
\u3042\u3043\u3044\u3042\u3042 \u304e\u304f \u3051
true \u3042\u3043\u3044\u3042\u3042 \u304e\u304f \u3051 0
// Test the whitespace escape sequence
\u3042\u3043\s\u3044
\u3042\u3043 \u3044
true \u3042\u3043 \u3044 0
\s\s\s
\u3043l\u3042\u3049 \u3046rr
false 0
\S\S\s
\u3043l\u3042\u3049 \u3046rr
true \u3042\u3049 0
// Test the digit escape sequence
\u3042\u3043\d\u3044
\u3042\u30439\u3044
true \u3042\u30439\u3044 0
\d\d\d
\u3043l\u3042\u304945
false 0
// Test the caret metacharacter
^\u3042\u3043\u3044
\u3042\u3043\u3044\u3045\u3046\u3047
true \u3042\u3043\u3044 0
^\u3042\u3043\u3044
\u3043\u3044\u3045\u3042\u3043\u3044
false 0
// Greedy ? metacharacter
\u3042?\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3043 0
\u3042?\u3043
\u3043
true \u3043 0
\u3042?\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.?\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3043 0
// Reluctant ? metacharacter
\u3042??\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3043 0
\u3042??\u3043
\u3043
true \u3043 0
\u3042??\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.??\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3043 0
// Possessive ? metacharacter
\u3042?+\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3043 0
\u3042?+\u3043
\u3043
true \u3043 0
\u3042?+\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.?+\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3043 0
// Greedy + metacharacter
\u3042+\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
\u3042+\u3043
\u3043
false 0
\u3042+\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.+\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
// Reluctant + metacharacter
\u3042+?\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
\u3042+?\u3043
\u3043
false 0
\u3042+?\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.+?\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
// Possessive + metacharacter
\u3042++\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
\u3042++\u3043
\u3043
false 0
\u3042++\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.++\u3043
\u3042\u3042\u3042\u3042\u3043
false 0
// Greedy Repetition
\u3042{2,3}
\u3042
false 0
\u3042{2,3}
\u3042\u3042
true \u3042\u3042 0
\u3042{2,3}
\u3042\u3042\u3042
true \u3042\u3042\u3042 0
\u3042{2,3}
\u3042\u3042\u3042\u3042
true \u3042\u3042\u3042 0
\u3042{3,}
\u305B\u305B\u305B\u3042\u3042\u3042\u3042\u305B\u305B\u305B
true \u3042\u3042\u3042\u3042 0
\u3042{3,}
\u305B\u305B\u305B\u3042\u3042\u305B\u305B\u305B
false 0
// Reluctant Repetition
\u3042{2,3}?
\u3042
false 0
\u3042{2,3}?
\u3042\u3042
true \u3042\u3042 0
\u3042{2,3}?
\u3042\u3042\u3042
true \u3042\u3042 0
\u3042{2,3}?
\u3042\u3042\u3042\u3042
true \u3042\u3042 0
// Zero width Positive lookahead
\u3042\u3043\u3044(?=\u3045)
\u305B\u305B\u305B\u3042\u3043\u3044\u3045
true \u3042\u3043\u3044 0
\u3042\u3043\u3044(?=\u3045)
\u305B\u305B\u305B\u3042\u3043\u3044\u3046\u3045
false 0
// Zero width Negative lookahead
\u3042\u3043\u3044(?!\u3045)
\u305B\u305B\u3042\u3043\u3044\u3045
false 0
\u3042\u3043\u3044(?!\u3045)
\u305B\u305B\u3042\u3043\u3044\u3046\u3045
true \u3042\u3043\u3044 0
// Zero width Positive lookbehind
\u3042(?<=\u3042)
###\u3042\u3043\u3044
true \u3042 0
\u3042(?<=\u3042)
###\u3043\u3044###
false 0
// Zero width Negative lookbehind
(?<!\u3042)\w
###\u3042\u3043\u3044a###
true a 0
(?<!\u3042)\u3044
\u3043\u3044
true \u3044 0
(?<!\u3042)\u3044
\u3042\u3044
false 0
// Nondeterministic group
(\u3042+\u3043)+
\u3042\u3043\u3042\u3043\u3042\u3043
true \u3042\u3043\u3042\u3043\u3042\u3043 1 \u3042\u3043
(\u3042|\u3043)+
\u3044\u3044\u3044\u3044\u3045
false 1
// Deterministic group
(\u3042\u3043)+
\u3042\u3043\u3042\u3043\u3042\u3043
true \u3042\u3043\u3042\u3043\u3042\u3043 1 \u3042\u3043
(\u3042\u3043)+
\u3042\u3044\u3044\u3044\u3044\u3045
false 1
(\u3042\u3043)*
\u3042\u3043\u3042\u3043\u3042\u3043
true \u3042\u3043\u3042\u3043\u3042\u3043 1 \u3042\u3043
(\u3042\u3043)(\u3044\u3045*)
\u305B\u305B\u305B\u3042\u3043\u3044\u305B\u305B\u305B
true \u3042\u3043\u3044 2 \u3042\u3043 \u3044
\u3042\u3043\u3044(\u3045)*\u3042\u3043\u3044
\u3042\u3043\u3044\u3045\u3045\u3045\u3045\u3045\u3042\u3043\u3044
true \u3042\u3043\u3044\u3045\u3045\u3045\u3045\u3045\u3042\u3043\u3044 1 \u3045
// Back references
(\u3042*)\u3043\u3044\1
\u305B\u305B\u305B\u3042\u3042\u3043\u3044\u3042\u3042\u305B\u305B\u305B
true \u3042\u3042\u3043\u3044\u3042\u3042 1 \u3042\u3042
(\u3042*)\u3043\u3044\1
\u305B\u305B\u305B\u3042\u3042\u3043\u3044\u3042\u305B\u305B\u305B
true \u3042\u3043\u3044\u3042 1 \u3042
(\u3048t*)(\u3045\u3045\u3046)*(\u305A\u3056)\1\3(\u3057\u3057)
\u305B\u305B\u305B\u3048tt\u3045\u3045\u3046\u3045\u3045\u3046\u305A\u3056\u3048tt\u305A\u3056\u3057\u3057\u305B\u305B\u305B
true \u3048tt\u3045\u3045\u3046\u3045\u3045\u3046\u305A\u3056\u3048tt\u305A\u3056\u3057\u3057 4 \u3048tt \u3045\u3045\u3046 \u305A\u3056 \u3057\u3057
// Greedy * metacharacter
\u3042*\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
\u3042*\u3043
\u3043
true \u3043 0
\u3042*\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.*\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
// Reluctant * metacharacter
\u3042*?\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
\u3042*?\u3043
\u3043
true \u3043 0
\u3042*?\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.*?\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
// Possessive * metacharacter
\u3042*+\u3043
\u3042\u3042\u3042\u3042\u3043
true \u3042\u3042\u3042\u3042\u3043 0
\u3042*+\u3043
\u3043
true \u3043 0
\u3042*+\u3043
\u3042\u3042\u3042\u3044\u3044\u3044
false 0
.*+\u3043
\u3042\u3042\u3042\u3042\u3043
false 0
// Case insensitivity
(?iu)\uFF46\uFF4F\uFF4F\uFF42\uFF41\uFF52
\uFF46\uFF2F\uFF4F\uFF42\uFF21\uFF52
true \uFF46\uFF2F\uFF4F\uFF42\uFF21\uFF52 0
\uFF46(?iu)\uFF4F\uFF4F\uFF42\uFF41\uFF52
\uFF46\uFF2F\uFF4F\uFF42\uFF21\uFF52
true \uFF46\uFF2F\uFF4F\uFF42\uFF21\uFF52 0
\uFF46\uFF4F\uFF4F(?iu)\uFF42\uFF41\uFF52
\uFF46\uFF2F\uFF4F\uFF42\uFF21\uFF52
false 0
(?iu)\uFF46\uFF4F\uFF4F[\uFF42\uFF41\uFF52]+
\uFF46\uFF4F\uFF2F\uFF42\uFF21\uFF52
true \uFF46\uFF4F\uFF2F\uFF42\uFF21\uFF52 0
(?iu)\uFF46\uFF4F\uFF4F[\uFF41-\uFF52]+
\uFF46\uFF4F\uFF2F\uFF42\uFF21\uFF52
true \uFF46\uFF4F\uFF2F\uFF42\uFF21\uFF52 0
// Disable metacharacters- test both length <=3 and >3
// So that the BM optimization is part of test
\Q***\E\u3042\u3043\u3044
***\u3042\u3043\u3044
true ***\u3042\u3043\u3044 0
\u3043l\Q***\E\u3042\u3043\u3044
\u3043l***\u3042\u3043\u3044
true \u3043l***\u3042\u3043\u3044 0
\Q***\u3042\u3043\u3044
***\u3042\u3043\u3044
true ***\u3042\u3043\u3044 0
\u3043l\u3042\u3049\Q***\E\u3042\u3043\u3044
\u3043l\u3042\u3049***\u3042\u3043\u3044
true \u3043l\u3042\u3049***\u3042\u3043\u3044 0
\Q***\u3042\u3043\u3044
***\u3042\u3043\u3044
true ***\u3042\u3043\u3044 0
\Q*\u3042\u3043
*\u3042\u3043
true *\u3042\u3043 0
\u3043l\u3042\u3049\Q***\u3042\u3043\u3044
\u3043l\u3042\u3049***\u3042\u3043\u3044
true \u3043l\u3042\u3049***\u3042\u3043\u3044 0
\u3043l\u3042\Q***\u3042\u3043\u3044
\u3043l\u3042***\u3042\u3043\u3044
true \u3043l\u3042***\u3042\u3043\u3044 0
[\043]+
\u3043l\u3042\u3049\u3043l\u3042\u3049#\u3043le\u3044\u3049
true # 0
[\042-\044]+
\u3043l\u3042\u3049\u3043l\u3042\u3049#\u3043le\u3044\u3049
true # 0
[\u1234-\u1236]
\u3043l\u3042\u3049\u3043l\u3042\u3049\u1235\u3043le\u3044\u3049
true \u1235 0
[^\043]*
\u3043l\u3042\u3049\u3043l\u3042\u3049#\u3043le\u3044\u3049
true \u3043l\u3042\u3049\u3043l\u3042\u3049 0

View file

@ -0,0 +1,165 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @summary tests RegExp unicode case-insensitive match (?ui)
* @bug 8360459
* @library /lib/testlibrary/java/lang
* @run junit CaseFoldingTest
*/
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CaseFoldingTest {
@Test
void testUnicodeCaseInsensitiveMatch() throws Throwable {
var testAll = true; // true to test all codepoints defined in CaseFolding.txt
var verbose = true; // true to display all codepoints being tested
var filter = "^.*; [CTS]; .*$"; // update C,T,S to test different type
var excluded = Set.of(
// these 'S' characters failed for known reason. they don't map to their
// folding form with toUpperCase or toLowerCase, only map with case-folding.
// exclude them for now.
0x1fd3, // 1FD3 [lo: 1fd3, up: 1fd3] 0390 [lo: 0390, up: 0390]
0x1fe3, // 1FE3 [lo: 1fe3, up: 1fe3] 03B0 [lo: 03b0, up: 03b0]
0xfb05 // FB05 [lo: fb05, up: fb05] FB06 [lo: fb06, up: fb06]
);
var results = Files.lines(UCDFiles.CASEFOLDING)
.filter(line -> !line.startsWith("#") && line.matches(filter))
.map(line -> {
var strs = line.split("; ");
return new String[] {strs[0], strs[1], strs[2]};
})
.filter(cps -> {
var cp1 = Integer.parseInt(cps[0], 16);
var cp2 = Integer.parseInt(cps[2], 16);
if (excluded.contains(cp1))
return false;
if (testAll) {
return true;
}
// the folding codepoint doesn't map back to the original codepoint.
return Character.toUpperCase(cp2) != cp1 && Character.toLowerCase(cp2) != cp1;
})
.flatMap(cps -> {
// test slice, single & range
var cp = Integer.parseInt(cps[0], 16);
var folding = Integer.parseInt(cps[2], 16);
var errors = testCaseFolding(cp, folding);
if (verbose)
System.out.format(" [%s] %s [lo: %04x, up: %04x] %s [lo: %04x, up: %04x]\n",
cps[1],
cps[0],
Character.toLowerCase(cp),
Character.toUpperCase(cp),
cps[2],
Character.toLowerCase(folding),
Character.toUpperCase(folding)
);
errors.forEach(error -> System.out.print(error));
return errors.stream();
})
.collect(Collectors.toList());
assertEquals(results.size(), 0);
}
private static ArrayList<String> testCaseFolding(int cp, int folding) {
ArrayList<String> errors = new ArrayList<>();
testCaseFolding0(cp, folding, errors, "s-t");
testCaseFolding0(folding, cp, errors, "t-s");
// test all uppercase, lowercase combinations
var up = Character.toUpperCase(cp);
var lo = Character.toLowerCase(cp);
var folding_up = Character.toUpperCase(folding); // folding should be normally lowercase
if (up != cp) {
testCaseFolding0(up, folding, errors, "s(u)-t");
testCaseFolding0(folding, up, errors, "t-s(u)");
if (folding_up != folding) {
testCaseFolding0(up, folding_up, errors, "s(u)-t(u)");
testCaseFolding0(folding_up, up, errors, "t(u)-s(u)");
}
}
if (lo != cp) {
testCaseFolding0(lo, folding, errors, "s(l)-t");
testCaseFolding0(folding, lo, errors, "t-s(l)");
if (folding_up != folding) {
testCaseFolding0(lo, folding_up, errors, "s(l)-t(u)");
testCaseFolding0(folding_up, lo, errors, "t(u)-s(l)");
}
}
return errors;
}
private static void testCaseFolding0(int cp, int folding, ArrayList<String> errors, String type) {
var cp_str = Character.isSupplementaryCodePoint(cp)
? String.format("\\u%04x\\u%04x", (int)Character.highSurrogate(cp), (int)Character.lowSurrogate(cp))
: String.format("\\u%04x", cp);
var t = new String(Character.toChars(folding));
var p = String.format("(?iu)%s", cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] slice: %-20s t: u+%04x (%s)\n", p, folding, type));
}
p = String.format("(?iu)[%s]", cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] single: %-20s t: u+%04x (%s)\n", p, folding, type));
}
p = String.format("(?iu)[%s-%s]", cp_str, cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] range: %-20s t: u+%04x (%s)\n", p, folding, type));
}
// small range
var end_cp = cp + 16;
var end_cp_str = Character.isSupplementaryCodePoint(end_cp)
? String.format("\\u%04x\\u%04x", (int)Character.highSurrogate(end_cp), (int)Character.lowSurrogate(end_cp))
: String.format("\\u%04x", end_cp);
p = String.format("(?iu)[%s-%s]", cp_str, end_cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] range: %-20s t: u+%04x (%s)\n", p, folding, type));
}
end_cp = cp + 128; // bigger than the expanded_casefolding_map.
end_cp_str = Character.isSupplementaryCodePoint(end_cp)
? String.format("\\u%04x\\u%04x", (int)Character.highSurrogate(end_cp), (int)Character.lowSurrogate(end_cp))
: String.format("\\u%04x", end_cp);
p = String.format("(?iu)[%s-%s]", cp_str, end_cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] range: %-20s t: u+%04x (%s)\n", p, folding, type));
}
}
}

View file

@ -0,0 +1,33 @@
#
# Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# This code is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 only, as
# published by the Free Software Foundation.
#
# This code is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
# version 2 for more details (a copy is included in the LICENSE file that
# accompanied this code).
#
# You should have received a copy of the GNU General Public License version
# 2 along with this work; if not, write to the Free Software Foundation,
# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
# or visit www.oracle.com if you need additional information or have any
# questions.
#
# test cases for Grapheme support. Format follows GrahemeBreakTest.txt
# from Unicode.
# https://unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakTest.html
# bug 8216332
÷ 1F468 × 1F3FE ÷ 1F468 × 200D × 1F469 × 200D × 1F466 ÷
÷ 1F468 × 200D × 1F469 × 200D × 1F466 × 200d ÷ 0041 ÷
÷ 1F468 × 200D × 1F469 × 200D × 1F466 ÷ 0041 ÷
÷ 0041 ÷ 1F468 × 200D × 1F469 × 200D × 1F466 ÷ 0041 ÷
÷ 1F468 × 200D × 1F3FE × 200D × 1F469 × 1F3FE × 200D × 1F466 ÷ 0041 ÷

View file

@ -0,0 +1,233 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import jdk.test.lib.RandomFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;
/*
* @test
* @bug 8132995 8312976
* @key randomness
*
* @summary Tests to exercise the optimization described in the bug report.
* @library /test/lib
* @run junit ImmutableMatchResultTest
*/
public class ImmutableMatchResultTest {
private static final int prefixLen;
private static final int infixLen;
private static final int suffixLen;
private static final String group1 = "abc";
private static final String group2 = "wxyz";
private static final String group0;
private static final String in;
private static final String groupResults = "(([a-z]+)([0-9]*))";
private static final String inResults;
private static final String letters1 = "abcd";
private static final String digits1 = "12";
private static final String letters2 = "pqr";
private static final String digits2 = "";
static {
Random rnd = RandomFactory.getRandom();
prefixLen = rnd.nextInt(10);
infixLen = rnd.nextInt(10);
suffixLen = rnd.nextInt(10);
group0 = group1 + "-".repeat(infixLen) + group2;
in = "-".repeat(prefixLen) + group0 + "-".repeat(suffixLen);
inResults = " ".repeat(prefixLen) + letters1 + digits1 + " ".repeat(infixLen) + letters2 + digits2 + " ".repeat(suffixLen);
}
private static void test(CharSequence cs) {
Matcher m = Pattern.compile("(" + group1 + ")-*(" + group2 + ")").matcher(cs);
assertTrue(m.find());
assertEquals(prefixLen, m.start());
assertEquals(prefixLen + group0.length(), m.end());
assertEquals(group0, m.toMatchResult().group());
assertEquals(prefixLen, m.start(1));
assertEquals(prefixLen + group1.length(), m.end(1));
assertEquals(group1, m.toMatchResult().group(1));
assertEquals(prefixLen + group1.length() + infixLen, m.start(2));
assertEquals(prefixLen + group1.length() + infixLen + group2.length(), m.end(2));
assertEquals(group2, m.toMatchResult().group(2));
}
@Test
void testString() {
test(in);
}
@Test
void testStringBuilder() {
test(new StringBuilder(in));
}
@Test
void testStringBuffer() {
test(new StringBuffer(in));
}
@Test
void testCharBuffer() {
test(CharBuffer.wrap(in));
}
private static void testResultsStream(CharSequence cs) {
Matcher m = Pattern.compile(groupResults).matcher(cs);
List<MatchResult> results = m.results().toList();
assertEquals(2, results.size());
int startLetters1 = prefixLen;
int endLetters1 = startLetters1 + letters1.length();
int startDigits1 = endLetters1;
int endDigits1 = startDigits1 + digits1.length();
assertEquals(startLetters1, results.get(0).start());
assertEquals(startLetters1, results.get(0).start(0));
assertEquals(startLetters1, results.get(0).start(1));
assertEquals(startLetters1, results.get(0).start(2));
assertEquals(startDigits1, results.get(0).start(3));
assertEquals(endDigits1, results.get(0).end());
assertEquals(endDigits1, results.get(0).end(0));
assertEquals(endDigits1, results.get(0).end(1));
assertEquals(endLetters1, results.get(0).end(2));
assertEquals(endDigits1, results.get(0).end(3));
assertEquals(letters1 + digits1, results.get(0).group());
assertEquals(letters1 + digits1, results.get(0).group(0));
assertEquals(letters1 + digits1, results.get(0).group(1));
assertEquals(letters1, results.get(0).group(2));
assertEquals(digits1, results.get(0).group(3));
int startLetters2 = endDigits1 + infixLen;
int endLetters2 = startLetters2 + letters2.length();
int startDigits2 = endLetters2;
int endDigits2 = startDigits2 + digits2.length();
assertEquals(startLetters2, results.get(1).start());
assertEquals(startLetters2, results.get(1).start(0));
assertEquals(startLetters2, results.get(1).start(1));
assertEquals(startLetters2, results.get(1).start(2));
assertEquals(startDigits2, results.get(1).start(3));
assertEquals(endDigits2, results.get(1).end());
assertEquals(endDigits2, results.get(1).end(0));
assertEquals(endDigits2, results.get(1).end(1));
assertEquals(endLetters2, results.get(1).end(2));
assertEquals(endDigits2, results.get(1).end(3));
assertEquals(letters2 + digits2, results.get(1).group());
assertEquals(letters2 + digits2, results.get(1).group(0));
assertEquals(letters2 + digits2, results.get(1).group(1));
assertEquals(letters2, results.get(1).group(2));
assertEquals(digits2, results.get(1).group(3));
}
@Test
void testResultsStreamString() {
testResultsStream(inResults);
}
@Test
void testResultsStreamStringBuilder() {
testResultsStream(new StringBuilder(inResults));
}
@Test
void testResultsStreamStringBuffer() {
testResultsStream(new StringBuffer(inResults));
}
@Test
void testResultsStreamCharBuffer() {
testResultsStream(CharBuffer.wrap(inResults));
}
static Arguments[] testGroupsOutsideMatch() {
return new Arguments[]{
arguments("(?<=(\\d{3}))\\D*(?=(\\d{4}))", "-1234abcxyz5678-"),
arguments("(?<=(\\d{3}))\\D*(?=(\\1))", "-1234abcxyz2348-"),
arguments("(?<!(\\d{4}))\\D+(?=(\\d{4}))", "123abcxyz5678-"),
};
}
@ParameterizedTest
@MethodSource
void testGroupsOutsideMatch(String pattern, String text) {
char[] data = text.toCharArray();
Matcher m = Pattern.compile(pattern)
.matcher(CharBuffer.wrap(data));
assertEquals(2, m.groupCount());
assertTrue(m.find());
int start = m.start();
int end = m.end();
String group = m.group();
int prefixStart = m.start(1);
int prefixEnd = m.end(1);
String prefixGroup = m.group(1);
int suffixStart = m.start(2);
int suffixEnd = m.end(2);
String suffixGroup = m.group(2);
MatchResult mr = m.toMatchResult();
Arrays.fill(data, '*'); // spoil original input
assertEquals(start, mr.start());
assertEquals(end, mr.end());
assertEquals(group, mr.group());
assertEquals(prefixStart, mr.start(1));
assertEquals(prefixEnd, mr.end(1));
assertEquals(prefixGroup, mr.group(1));
assertEquals(suffixStart, mr.start(2));
assertEquals(suffixEnd, mr.end(2));
assertEquals(suffixGroup, mr.group(2));
}
}

View file

@ -0,0 +1,371 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8065554 8309515
* @run main NamedGroupsTests
*/
import java.util.Map;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class NamedGroupsTests {
/* An implementation purposely not overriding any default method */
private static class TestMatcherNoNamedGroups implements MatchResult {
@Override
public int start() {
return 0;
}
@Override
public int start(int group) {
return 0;
}
@Override
public int end() {
return 0;
}
@Override
public int end(int group) {
return 0;
}
@Override
public String group() {
return null;
}
@Override
public String group(int group) {
return null;
}
@Override
public int groupCount() {
return 0;
}
}
public static void main(String[] args) {
testMatchResultNoDefault();
testPatternNamedGroups();
testMatcherNamedGroups();
testMatchResultNamedGroups();
testMatcherHasMatch();
testMatchResultHasMatch();
testMatchResultStartEndGroupBeforeMatchOp();
testMatchResultStartEndGroupAfterMatchOp();
testMatchAfterUsePattern();
}
private static void testMatchResultNoDefault() {
TestMatcherNoNamedGroups m = new TestMatcherNoNamedGroups();
try {
m.hasMatch();
} catch (UnsupportedOperationException e) { // swallowing intended
}
try {
m.namedGroups();
} catch (UnsupportedOperationException e) { // swallowing intended
}
try {
m.start("anyName");
} catch (UnsupportedOperationException e) { // swallowing intended
}
try {
m.end("anyName");
} catch (UnsupportedOperationException e) { // swallowing intended
}
try {
m.group("anyName");
} catch (UnsupportedOperationException e) { // swallowing intended
}
}
private static void testMatchResultStartEndGroupBeforeMatchOp() {
Matcher m = Pattern.compile("(?<some>.+?)(?<rest>.*)").matcher("abc");
try {
m.start("anyName");
} catch (IllegalStateException e) { // swallowing intended
}
try {
m.end("anyName");
} catch (IllegalStateException e) { // swallowing intended
}
try {
m.group("anyName");
} catch (IllegalStateException e) { // swallowing intended
}
}
private static void testMatchResultStartEndGroupAfterMatchOp() {
testMatchResultStartEndGroupNoMatch();
testMatchResultStartEndGroupWithMatch();
testMatchResultStartEndGroupNoMatchNoSuchGroup();
testMatchResultStartEndGroupWithMatchNoSuchGroup();
}
private static void testMatchResultStartEndGroupNoMatch() {
Pattern.compile("(?<some>.+?)(?<rest>.*)").matcher("")
.results()
.forEach(r -> {
if (r.start("some") >= 0) {
throw new RuntimeException("start(\"some\")");
}
if (r.start("rest") >= 0) {
throw new RuntimeException("start(\"rest\")");
}
if (r.end("some") >= 0) {
throw new RuntimeException("end(\"some\")");
}
if (r.end("rest") >= 0) {
throw new RuntimeException("end(\"rest\")");
}
if (r.group("some") != null) {
throw new RuntimeException("group(\"some\")");
}
if (r.group("rest") != null) {
throw new RuntimeException("group(\"rest\")");
}
});
}
private static void testMatchResultStartEndGroupWithMatch() {
Pattern.compile("(?<some>.+?)(?<rest>.*)").matcher("abc")
.results()
.forEach(r -> {
if (r.start("some") < 0) {
throw new RuntimeException("start(\"some\")");
}
if (r.start("rest") < 0) {
throw new RuntimeException("start(\"rest\")");
}
if (r.end("some") < 0) {
throw new RuntimeException("end(\"some\")");
}
if (r.end("rest") < 0) {
throw new RuntimeException("end(\"rest\")");
}
if (r.group("some") == null) {
throw new RuntimeException("group(\"some\")");
}
if (r.group("rest") == null) {
throw new RuntimeException("group(\"rest\")");
}
});
}
private static void testMatchResultStartEndGroupNoMatchNoSuchGroup() {
Pattern.compile("(?<some>.+?)(?<rest>.*)").matcher("")
.results()
.forEach(r -> {
try {
r.start("noSuchGroup");
} catch (IllegalArgumentException e) { // swallowing intended
}
try {
r.end("noSuchGroup");
} catch (IllegalArgumentException e) { // swallowing intended
}
try {
r.group("noSuchGroup");
} catch (IllegalArgumentException e) { // swallowing intended
}
});
}
private static void testMatchResultStartEndGroupWithMatchNoSuchGroup() {
Pattern.compile("(?<some>.+?)(?<rest>.*)").matcher("abc")
.results()
.forEach(r -> {
try {
r.start("noSuchGroup");
} catch (IllegalArgumentException e) { // swallowing intended
}
try {
r.end("noSuchGroup");
} catch (IllegalArgumentException e) { // swallowing intended
}
try {
r.group("noSuchGroup");
} catch (IllegalArgumentException e) { // swallowing intended
}
});
}
private static void testMatchResultHasMatch() {
testMatchResultHasMatchNoMatch();
testMatchResultHasMatchWithMatch();
}
private static void testMatchResultHasMatchNoMatch() {
Matcher m = Pattern.compile(".+").matcher("");
m.find();
if (m.toMatchResult().hasMatch()) {
throw new RuntimeException();
}
}
private static void testMatchResultHasMatchWithMatch() {
Matcher m = Pattern.compile(".+").matcher("abc");
m.find();
if (!m.toMatchResult().hasMatch()) {
throw new RuntimeException();
}
}
private static void testMatcherHasMatch() {
testMatcherHasMatchNoMatch();
testMatcherHasMatchWithMatch();
}
private static void testMatcherHasMatchNoMatch() {
Matcher m = Pattern.compile(".+").matcher("");
m.find();
if (m.hasMatch()) {
throw new RuntimeException();
}
}
private static void testMatcherHasMatchWithMatch() {
Matcher m = Pattern.compile(".+").matcher("abc");
m.find();
if (!m.hasMatch()) {
throw new RuntimeException();
}
}
private static void testMatchResultNamedGroups() {
testMatchResultNamedGroupsNoNamedGroups();
testMatchResultNamedGroupsOneNamedGroup();
testMatchResultNamedGroupsTwoNamedGroups();
}
private static void testMatchResultNamedGroupsNoNamedGroups() {
if (!Pattern.compile(".*").matcher("")
.toMatchResult().namedGroups().isEmpty()) {
throw new RuntimeException();
}
}
private static void testMatchResultNamedGroupsOneNamedGroup() {
if (!Pattern.compile("(?<all>.*)").matcher("")
.toMatchResult().namedGroups()
.equals(Map.of("all", 1))) {
throw new RuntimeException();
}
}
private static void testMatchResultNamedGroupsTwoNamedGroups() {
if (!Pattern.compile("(?<some>.+?)(?<rest>.*)").matcher("")
.toMatchResult().namedGroups()
.equals(Map.of("some", 1, "rest", 2))) {
throw new RuntimeException();
}
}
private static void testMatcherNamedGroups() {
testMatcherNamedGroupsNoNamedGroups();
testMatcherNamedGroupsOneNamedGroup();
testMatcherNamedGroupsTwoNamedGroups();
}
private static void testMatcherNamedGroupsNoNamedGroups() {
if (!Pattern.compile(".*").matcher("").namedGroups().isEmpty()) {
throw new RuntimeException();
}
}
private static void testMatcherNamedGroupsOneNamedGroup() {
if (!Pattern.compile("(?<all>.*)").matcher("").namedGroups()
.equals(Map.of("all", 1))) {
throw new RuntimeException();
}
}
private static void testMatcherNamedGroupsTwoNamedGroups() {
if (!Pattern.compile("(?<some>.+?)(?<rest>.*)").matcher("").namedGroups()
.equals(Map.of("some", 1, "rest", 2))) {
throw new RuntimeException();
}
}
private static void testPatternNamedGroups() {
testPatternNamedGroupsNoNamedGroups();
testPatternNamedGroupsOneNamedGroup();
testPatternNamedGroupsTwoNamedGroups();
}
private static void testPatternNamedGroupsNoNamedGroups() {
if (!Pattern.compile(".*").namedGroups().isEmpty()) {
throw new RuntimeException();
}
}
private static void testPatternNamedGroupsOneNamedGroup() {
if (!Pattern.compile("(?<all>.*)").namedGroups()
.equals(Map.of("all", 1))) {
throw new RuntimeException();
}
}
private static void testPatternNamedGroupsTwoNamedGroups() {
if (!Pattern.compile("(?<some>.+?)(?<rest>.*)").namedGroups()
.equals(Map.of("some", 1, "rest", 2))) {
throw new RuntimeException();
}
}
private static void testMatchAfterUsePattern() {
Pattern p1 = Pattern.compile("(?<a>...)(?<b>...)");
Matcher m = p1.matcher("foobar");
if (!m.matches()) {
throw new RuntimeException("matches() expected");
}
if (!m.group("a").equals("foo")) {
throw new RuntimeException("\"foo\" expected for group(\"a\")");
}
Pattern p2 = Pattern.compile("(?<b>...)(?<a>...)");
m.usePattern(p2);
if (!m.matches()) {
throw new RuntimeException("matches() expected");
}
if (!m.group("a").equals("bar")) {
throw new RuntimeException("\"bar\" expected for group(\"a\")");
}
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8223174
* @summary Pattern.compile() can throw confusing NegativeArraySizeException
* @requires os.maxMemory >= 6g & vm.bits == 64 & !vm.musl
* @run testng/othervm -Xms5G -Xmx5G NegativeArraySize
*/
import org.testng.annotations.Test;
import static org.testng.Assert.assertThrows;
import java.util.regex.Pattern;
public class NegativeArraySize {
@Test
public static void testNegativeArraySize() {
assertThrows(OutOfMemoryError.class, () -> Pattern.compile("\\Q" + "a".repeat(42 + Integer.MAX_VALUE / 3)));
}
}

View file

@ -0,0 +1,247 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
final class POSIX_ASCII {
static final int UPPER = 0x00000100;
static final int LOWER = 0x00000200;
static final int DIGIT = 0x00000400;
static final int SPACE = 0x00000800;
static final int PUNCT = 0x00001000;
static final int CNTRL = 0x00002000;
static final int BLANK = 0x00004000;
static final int HEX = 0x00008000;
static final int UNDER = 0x00010000;
static final int ASCII = 0x0000FF00;
static final int ALPHA = (UPPER|LOWER);
static final int ALNUM = (UPPER|LOWER|DIGIT);
static final int GRAPH = (PUNCT|UPPER|LOWER|DIGIT);
static final int WORD = (UPPER|LOWER|UNDER|DIGIT);
static final int XDIGIT = (HEX);
private static final int[] ctype = new int[] {
CNTRL, /* 00 (NUL) */
CNTRL, /* 01 (SOH) */
CNTRL, /* 02 (STX) */
CNTRL, /* 03 (ETX) */
CNTRL, /* 04 (EOT) */
CNTRL, /* 05 (ENQ) */
CNTRL, /* 06 (ACK) */
CNTRL, /* 07 (BEL) */
CNTRL, /* 08 (BS) */
SPACE+CNTRL+BLANK, /* 09 (HT) */
SPACE+CNTRL, /* 0A (LF) */
SPACE+CNTRL, /* 0B (VT) */
SPACE+CNTRL, /* 0C (FF) */
SPACE+CNTRL, /* 0D (CR) */
CNTRL, /* 0E (SI) */
CNTRL, /* 0F (SO) */
CNTRL, /* 10 (DLE) */
CNTRL, /* 11 (DC1) */
CNTRL, /* 12 (DC2) */
CNTRL, /* 13 (DC3) */
CNTRL, /* 14 (DC4) */
CNTRL, /* 15 (NAK) */
CNTRL, /* 16 (SYN) */
CNTRL, /* 17 (ETB) */
CNTRL, /* 18 (CAN) */
CNTRL, /* 19 (EM) */
CNTRL, /* 1A (SUB) */
CNTRL, /* 1B (ESC) */
CNTRL, /* 1C (FS) */
CNTRL, /* 1D (GS) */
CNTRL, /* 1E (RS) */
CNTRL, /* 1F (US) */
SPACE+BLANK, /* 20 SPACE */
PUNCT, /* 21 ! */
PUNCT, /* 22 " */
PUNCT, /* 23 # */
PUNCT, /* 24 $ */
PUNCT, /* 25 % */
PUNCT, /* 26 & */
PUNCT, /* 27 ' */
PUNCT, /* 28 ( */
PUNCT, /* 29 ) */
PUNCT, /* 2A * */
PUNCT, /* 2B + */
PUNCT, /* 2C , */
PUNCT, /* 2D - */
PUNCT, /* 2E . */
PUNCT, /* 2F / */
DIGIT+HEX+0, /* 30 0 */
DIGIT+HEX+1, /* 31 1 */
DIGIT+HEX+2, /* 32 2 */
DIGIT+HEX+3, /* 33 3 */
DIGIT+HEX+4, /* 34 4 */
DIGIT+HEX+5, /* 35 5 */
DIGIT+HEX+6, /* 36 6 */
DIGIT+HEX+7, /* 37 7 */
DIGIT+HEX+8, /* 38 8 */
DIGIT+HEX+9, /* 39 9 */
PUNCT, /* 3A : */
PUNCT, /* 3B ; */
PUNCT, /* 3C < */
PUNCT, /* 3D = */
PUNCT, /* 3E > */
PUNCT, /* 3F ? */
PUNCT, /* 40 @ */
UPPER+HEX+10, /* 41 A */
UPPER+HEX+11, /* 42 B */
UPPER+HEX+12, /* 43 C */
UPPER+HEX+13, /* 44 D */
UPPER+HEX+14, /* 45 E */
UPPER+HEX+15, /* 46 F */
UPPER+16, /* 47 G */
UPPER+17, /* 48 H */
UPPER+18, /* 49 I */
UPPER+19, /* 4A J */
UPPER+20, /* 4B K */
UPPER+21, /* 4C L */
UPPER+22, /* 4D M */
UPPER+23, /* 4E N */
UPPER+24, /* 4F O */
UPPER+25, /* 50 P */
UPPER+26, /* 51 Q */
UPPER+27, /* 52 R */
UPPER+28, /* 53 S */
UPPER+29, /* 54 T */
UPPER+30, /* 55 U */
UPPER+31, /* 56 V */
UPPER+32, /* 57 W */
UPPER+33, /* 58 X */
UPPER+34, /* 59 Y */
UPPER+35, /* 5A Z */
PUNCT, /* 5B [ */
PUNCT, /* 5C \ */
PUNCT, /* 5D ] */
PUNCT, /* 5E ^ */
PUNCT|UNDER, /* 5F _ */
PUNCT, /* 60 ` */
LOWER+HEX+10, /* 61 a */
LOWER+HEX+11, /* 62 b */
LOWER+HEX+12, /* 63 c */
LOWER+HEX+13, /* 64 d */
LOWER+HEX+14, /* 65 e */
LOWER+HEX+15, /* 66 f */
LOWER+16, /* 67 g */
LOWER+17, /* 68 h */
LOWER+18, /* 69 i */
LOWER+19, /* 6A j */
LOWER+20, /* 6B k */
LOWER+21, /* 6C l */
LOWER+22, /* 6D m */
LOWER+23, /* 6E n */
LOWER+24, /* 6F o */
LOWER+25, /* 70 p */
LOWER+26, /* 71 q */
LOWER+27, /* 72 r */
LOWER+28, /* 73 s */
LOWER+29, /* 74 t */
LOWER+30, /* 75 u */
LOWER+31, /* 76 v */
LOWER+32, /* 77 w */
LOWER+33, /* 78 x */
LOWER+34, /* 79 y */
LOWER+35, /* 7A z */
PUNCT, /* 7B { */
PUNCT, /* 7C | */
PUNCT, /* 7D } */
PUNCT, /* 7E ~ */
CNTRL, /* 7F (DEL) */
};
static int getType(int ch) {
return ((ch & 0xFFFFFF80) == 0 ? ctype[ch] : 0);
}
static boolean isType(int ch, int type) {
return (getType(ch) & type) != 0;
}
static boolean isAscii(int ch) {
return ((ch & 0xFFFFFF80) == 0);
}
static boolean isAlpha(int ch) {
return isType(ch, ALPHA);
}
static boolean isDigit(int ch) {
return ((ch-'0')|('9'-ch)) >= 0;
}
static boolean isAlnum(int ch) {
return isType(ch, ALNUM);
}
static boolean isGraph(int ch) {
return isType(ch, GRAPH);
}
static boolean isPrint(int ch) {
return ((ch-0x20)|(0x7E-ch)) >= 0;
}
static boolean isPunct(int ch) {
return isType(ch, PUNCT);
}
static boolean isSpace(int ch) {
return isType(ch, SPACE);
}
static boolean isHexDigit(int ch) {
return isType(ch, HEX);
}
static boolean isCntrl(int ch) {
return isType(ch, CNTRL);
}
static boolean isLower(int ch) {
return ((ch-'a')|('z'-ch)) >= 0;
}
static boolean isUpper(int ch) {
return ((ch-'A')|('Z'-ch)) >= 0;
}
static boolean isWord(int ch) {
return isType(ch, WORD);
}
}

View file

@ -0,0 +1,146 @@
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.util.HashMap;
import java.util.Locale;
public final class POSIX_Unicode {
public static boolean isAlpha(int ch) {
return Character.isAlphabetic(ch);
}
public static boolean isLower(int ch) {
return Character.isLowerCase(ch);
}
public static boolean isUpper(int ch) {
return Character.isUpperCase(ch);
}
// \p{Whitespace}
public static boolean isSpace(int ch) {
return ((((1 << Character.SPACE_SEPARATOR) |
(1 << Character.LINE_SEPARATOR) |
(1 << Character.PARAGRAPH_SEPARATOR)) >> Character.getType(ch)) & 1)
!= 0 ||
(ch >= 0x9 && ch <= 0xd) ||
(ch == 0x85);
}
// \p{gc=Control}
public static boolean isCntrl(int ch) {
return Character.getType(ch) == Character.CONTROL;
}
// \p{gc=Punctuation}
public static boolean isPunct(int ch) {
return ((((1 << Character.CONNECTOR_PUNCTUATION) |
(1 << Character.DASH_PUNCTUATION) |
(1 << Character.START_PUNCTUATION) |
(1 << Character.END_PUNCTUATION) |
(1 << Character.OTHER_PUNCTUATION) |
(1 << Character.INITIAL_QUOTE_PUNCTUATION) |
(1 << Character.FINAL_QUOTE_PUNCTUATION)) >> Character.getType(ch)) & 1)
!= 0;
}
// \p{gc=Decimal_Number}
// \p{Hex_Digit} -> PropList.txt: Hex_Digit
public static boolean isHexDigit(int ch) {
return Character.isDigit(ch) ||
(ch >= 0x0030 && ch <= 0x0039) ||
(ch >= 0x0041 && ch <= 0x0046) ||
(ch >= 0x0061 && ch <= 0x0066) ||
(ch >= 0xFF10 && ch <= 0xFF19) ||
(ch >= 0xFF21 && ch <= 0xFF26) ||
(ch >= 0xFF41 && ch <= 0xFF46);
}
// \p{gc=Decimal_Number}
public static boolean isDigit(int ch) {
return Character.isDigit(ch);
};
// \p{alpha}
// \p{digit}
public static boolean isAlnum(int ch) {
return Character.isAlphabetic(ch) || Character.isDigit(ch);
}
// \p{Whitespace} --
// [\N{LF} \N{VT} \N{FF} \N{CR} \N{NEL} -> 0xa, 0xb, 0xc, 0xd, 0x85
// \p{gc=Line_Separator}
// \p{gc=Paragraph_Separator}]
public static boolean isBlank(int ch) {
int type = Character.getType(ch);
return isSpace(ch) &&
ch != 0xa & ch != 0xb && ch !=0xc && ch != 0xd && ch != 0x85 &&
type != Character.LINE_SEPARATOR &&
type != Character.PARAGRAPH_SEPARATOR;
}
// [^
// \p{space}
// \p{gc=Control}
// \p{gc=Surrogate}
// \p{gc=Unassigned}]
public static boolean isGraph(int ch) {
int type = Character.getType(ch);
return !(isSpace(ch) ||
Character.CONTROL == type ||
Character.SURROGATE == type ||
Character.UNASSIGNED == type);
}
// \p{graph}
// \p{blank}
// -- \p{cntrl}
public static boolean isPrint(int ch) {
return (isGraph(ch) || isBlank(ch)) && !isCntrl(ch);
}
// PropList.txt:Noncharacter_Code_Point
public static boolean isNoncharacterCodePoint(int ch) {
return (ch & 0xfffe) == 0xfffe || (ch >= 0xfdd0 && ch <= 0xfdef);
}
public static boolean isJoinControl(int ch) {
return (ch == 0x200C || ch == 0x200D);
}
// \p{alpha}
// \p{gc=Mark}
// \p{digit}
// \p{gc=Connector_Punctuation}
public static boolean isWord(int ch) {
return isAlpha(ch) ||
((((1 << Character.NON_SPACING_MARK) |
(1 << Character.ENCLOSING_MARK) |
(1 << Character.COMBINING_SPACING_MARK) |
(1 << Character.CONNECTOR_PUNCTUATION)) >> Character.getType(ch)) & 1)
!= 0 ||
isDigit(ch) ||
isJoinControl(ch);
}
}

View file

@ -0,0 +1,331 @@
/*
* Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8016846 8024341 8071479 8145006
* @summary Unit tests stream and lambda-based methods on Pattern and Matcher
* @library /lib/testlibrary/bootlib
* @build java.base/java.util.stream.OpTestCase
* @run testng/othervm PatternStreamTest
*/
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.LambdaTestHelpers;
import java.util.stream.OpTestCase;
import java.util.stream.Stream;
import java.util.stream.TestData;
import static org.testng.Assert.*;
@Test
public class PatternStreamTest extends OpTestCase {
@DataProvider(name = "Patterns")
public static Object[][] makeStreamTestData() {
// Each item must match the type signature of the consumer of this data
// String, String, Pattern
List<Object[]> data = new ArrayList<>();
String description = "All matches";
String input = "XXXXXX";
Pattern pattern = Pattern.compile("X");
data.add(new Object[]{description, input, pattern});
description = "Bounded every other match";
input = "XYXYXYYXYX";
pattern = Pattern.compile("X");
data.add(new Object[]{description, input, pattern});
description = "Every other match";
input = "YXYXYXYYXYXY";
pattern = Pattern.compile("X");
data.add(new Object[]{description, input, pattern});
description = "";
input = "awgqwefg1fefw4vssv1vvv1";
pattern = Pattern.compile("4");
data.add(new Object[]{description, input, pattern});
input = "afbfq\u00a3abgwgb\u00a3awngnwggw\u00a3a\u00a3ahjrnhneerh";
pattern = Pattern.compile("\u00a3a");
data.add(new Object[]{description, input, pattern});
input = "awgqwefg1fefw4vssv1vvv1";
pattern = Pattern.compile("1");
data.add(new Object[]{description, input, pattern});
input = "a\u4ebafg1fefw\u4eba4\u9f9cvssv\u9f9c1v\u672c\u672cvv";
pattern = Pattern.compile("1");
data.add(new Object[]{description, input, pattern});
input = "1\u56da23\u56da456\u56da7890";
pattern = Pattern.compile("\u56da");
data.add(new Object[]{description, input, pattern});
input = "1\u56da23\u9f9c\u672c\u672c\u56da456\u56da\u9f9c\u672c7890";
pattern = Pattern.compile("\u56da");
data.add(new Object[]{description, input, pattern});
description = "Empty input";
input = "";
pattern = Pattern.compile("\u56da");
data.add(new Object[]{description, input, pattern});
description = "Empty input with empty pattern";
input = "";
pattern = Pattern.compile("");
data.add(new Object[]{description, input, pattern});
description = "Multiple separators";
input = "This is,testing: with\tdifferent separators.";
pattern = Pattern.compile("[ \t,:.]");
data.add(new Object[]{description, input, pattern});
description = "Repeated separators within and at end";
input = "boo:and:foo";
pattern = Pattern.compile("o");
data.add(new Object[]{description, input, pattern});
description = "Many repeated separators within and at end";
input = "booooo:and:fooooo";
pattern = Pattern.compile("o");
data.add(new Object[]{description, input, pattern});
description = "Many repeated separators before last match";
input = "fooooo:";
pattern = Pattern.compile("o");
data.add(new Object[] {description, input, pattern});
return data.toArray(new Object[0][]);
}
@Test(dataProvider = "Patterns")
public void testPatternSplitAsStream(String description, String input, Pattern pattern) {
// Derive expected result from pattern.split
List<String> expected = Arrays.asList(pattern.split(input));
Supplier<Stream<String>> ss = () -> pattern.splitAsStream(input);
withData(TestData.Factory.ofSupplier(description, ss))
.stream(LambdaTestHelpers.identity())
.expectedResult(expected)
.exercise();
}
@Test(dataProvider = "Patterns")
public void testReplaceFirst(String description, String input, Pattern pattern) {
// Derive expected result from Matcher.replaceFirst(String )
String expected = pattern.matcher(input).replaceFirst("R");
String actual = pattern.matcher(input).replaceFirst(r -> "R");
assertEquals(actual, expected);
}
@Test(dataProvider = "Patterns")
public void testReplaceAll(String description, String input, Pattern pattern) {
// Derive expected result from Matcher.replaceAll(String )
String expected = pattern.matcher(input).replaceAll("R");
String actual = pattern.matcher(input).replaceAll(r -> "R");
assertEquals(actual, expected);
// Derive expected result from Matcher.find
Matcher m = pattern.matcher(input);
int expectedMatches = 0;
while (m.find()) {
expectedMatches++;
}
AtomicInteger actualMatches = new AtomicInteger();
pattern.matcher(input).replaceAll(r -> "R" + actualMatches.incrementAndGet());
assertEquals(expectedMatches, actualMatches.get());
}
@Test(dataProvider = "Patterns")
public void testMatchResults(String description, String input, Pattern pattern) {
// Derive expected result from Matcher.find
Matcher m = pattern.matcher(input);
List<MatchResultHolder> expected = new ArrayList<>();
while (m.find()) {
expected.add(new MatchResultHolder(m));
}
Supplier<Stream<MatchResult>> ss = () -> pattern.matcher(input).results();
withData(TestData.Factory.ofSupplier(description, ss))
.stream(s -> s.map(MatchResultHolder::new))
.expectedResult(expected)
.exercise();
}
@Test
public void testLateBinding() {
Pattern pattern = Pattern.compile(",");
StringBuilder sb = new StringBuilder("a,b,c,d,e");
Stream<String> stream = pattern.splitAsStream(sb);
sb.setLength(3);
assertEquals(Arrays.asList("a", "b"), stream.collect(Collectors.toList()));
stream = pattern.splitAsStream(sb);
sb.append(",f,g");
assertEquals(Arrays.asList("a", "b", "f", "g"), stream.collect(Collectors.toList()));
}
public void testFailfastMatchResults() {
Pattern p = Pattern.compile("X");
Matcher m = p.matcher("XX");
Stream<MatchResult> s = m.results();
m.find();
// Should start on the second match
assertEquals(s.count(), 1);
// Fail fast without short-circuit
// Exercises Iterator.forEachRemaining
m.reset();
try {
m.results().peek(mr -> m.reset()).count();
fail();
} catch (ConcurrentModificationException e) {
// Should reach here
}
m.reset();
try {
m.results().peek(mr -> m.find()).count();
fail();
} catch (ConcurrentModificationException e) {
// Should reach here
}
// Fail fast with short-circuit
// Exercises Iterator.hasNext/next
m.reset();
try {
m.results().peek(mr -> m.reset()).limit(2).count();
fail();
} catch (ConcurrentModificationException e) {
// Should reach here
}
m.reset();
try {
m.results().peek(mr -> m.find()).limit(2).count();
fail();
} catch (ConcurrentModificationException e) {
// Should reach here
}
}
public void testFailfastReplace() {
Pattern p = Pattern.compile("X");
Matcher m = p.matcher("XX");
// Fail fast without short-circuit
// Exercises Iterator.forEachRemaining
m.reset();
try {
m.replaceFirst(mr -> { m.reset(); return "Y"; });
fail();
} catch (ConcurrentModificationException e) {
// Should reach here
}
m.reset();
try {
m.replaceAll(mr -> { m.reset(); return "Y"; });
fail();
} catch (ConcurrentModificationException e) {
// Should reach here
}
}
// A holder of MatchResult that can compare
static class MatchResultHolder implements Comparable<MatchResultHolder> {
final MatchResult mr;
MatchResultHolder(Matcher m) {
this(m.toMatchResult());
}
MatchResultHolder(MatchResult mr) {
this.mr = mr;
}
@Override
public int compareTo(MatchResultHolder that) {
int c = that.mr.group().compareTo(this.mr.group());
if (c != 0)
return c;
c = Integer.compare(that.mr.start(), this.mr.start());
if (c != 0)
return c;
c = Integer.compare(that.mr.end(), this.mr.end());
if (c != 0)
return c;
c = Integer.compare(that.mr.groupCount(), this.mr.groupCount());
if (c != 0)
return c;
for (int g = 0; g < this.mr.groupCount(); g++) {
c = that.mr.group(g).compareTo(this.mr.group(g));
if (c != 0)
return c;
c = Integer.compare(that.mr.start(g), this.mr.start(g));
if (c != 0)
return c;
c = Integer.compare(that.mr.end(g), this.mr.end(g));
if (c != 0)
return c;
}
return 0;
}
@Override
public boolean equals(Object that) {
if (this == that) return true;
if (that == null || getClass() != that.getClass()) return false;
return this.compareTo((MatchResultHolder) that) == 0;
}
@Override
public int hashCode() {
return mr.group().hashCode();
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,122 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8305486
* @summary Tests to exercise the split functionality added in the issue.
* @run junit SplitWithDelimitersTest
*/
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.Arrays;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.params.provider.Arguments.arguments;
public class SplitWithDelimitersTest {
private static String[] dropOddIndexed(String[] a, int limit) {
String[] r = new String[(a.length + 1) / 2];
for (int i = 0; i < a.length; i += 2) {
r[i / 2] = a[i];
}
int len = r.length;
if (limit == 0) {
/* Also drop trailing empty strings */
for (; len > 0 && r[len - 1].isEmpty(); --len); // empty body
}
return len < r.length ? Arrays.copyOf(r, len) : r;
}
static Arguments[] testSplit() {
return new Arguments[] {
arguments(new String[] {"b", "o", "", "o", ":::and::f", "o", "", "o", ""},
"boo:::and::foo", "o", 5),
arguments(new String[] {"b", "o", "", "o", ":::and::f", "o", "o"},
"boo:::and::foo", "o", 4),
arguments(new String[] {"b", "o", "", "o", ":::and::foo"},
"boo:::and::foo", "o", 3),
arguments(new String[] {"b", "o", "o:::and::foo"},
"boo:::and::foo", "o", 2),
arguments(new String[] {"boo:::and::foo"},
"boo:::and::foo", "o", 1),
arguments(new String[] {"b", "o", "", "o", ":::and::f", "o", "", "o"},
"boo:::and::foo", "o", 0),
arguments(new String[] {"b", "o", "", "o", ":::and::f", "o", "", "o", ""},
"boo:::and::foo", "o", -1),
arguments(new String[] {"boo", ":::", "and", "::", "foo"},
"boo:::and::foo", ":+", 3),
arguments(new String[] {"boo", ":::", "and::foo"},
"boo:::and::foo", ":+", 2),
arguments(new String[] {"boo:::and::foo"},
"boo:::and::foo", ":+", 1),
arguments(new String[] {"boo", ":::", "and", "::", "foo"},
"boo:::and::foo", ":+", 0),
arguments(new String[] {"boo", ":::", "and", "::", "foo"},
"boo:::and::foo", ":+", -1),
arguments(new String[] {"b", "", "b", "", ""},
"bb", "a*|b*", 3),
arguments(new String[] {"b", "", "b"},
"bb", "a*|b*", 2),
arguments(new String[] {"bb"},
"bb", "a*|b*", 1),
arguments(new String[] {"b", "", "b"},
"bb", "a*|b*", 0),
arguments(new String[] {"b", "", "b", "", ""},
"bb", "a*|b*", -1),
arguments(new String[] {"", "bb", "", "", ""},
"bb", "b*|a*", 3),
arguments(new String[] {"", "bb", ""},
"bb", "b*|a*", 2),
arguments(new String[] {"bb"},
"bb", "b*|a*", 1),
arguments(new String[] {"", "bb"},
"bb", "b*|a*", 0),
arguments(new String[] {"", "bb", "", "", ""},
"bb", "b*|a*", -1),
};
}
@ParameterizedTest
@MethodSource
void testSplit(String[] expected, String target, String regex, int limit) {
String[] computedWith = target.splitWithDelimiters(regex, limit);
assertArrayEquals(expected, computedWith);
String[] patComputedWith = Pattern.compile(regex).splitWithDelimiters(target, limit);
assertArrayEquals(computedWith, patComputedWith);
String[] computedWithout = target.split(regex, limit);
assertArrayEquals(dropOddIndexed(expected, limit), computedWithout);
String[] patComputedWithout = Pattern.compile(regex).split(target, limit);
assertArrayEquals(computedWithout, patComputedWithout);
}
}

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,123 @@
/*
* Copyright (c) 2016, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 7071819 8221431 8239383 8273430 8291660
* @summary tests Unicode Extended Grapheme support
* @library /lib/testlibrary/java/lang
* @modules java.base/jdk.internal.util.regex:+open
* @run testng GraphemeTest
*/
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.nio.file.Files;
import java.nio.file.Path;
import jdk.internal.util.regex.Grapheme;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.fail;
import static org.testng.Assert.assertFalse;
public class GraphemeTest {
private static MethodHandles.Lookup lookup;
@BeforeClass
public static void setup() throws IllegalAccessException {
lookup = MethodHandles.privateLookupIn(Grapheme.class, MethodHandles.lookup());
}
@Test
public static void testGraphemeBreakProperty() throws Throwable {
testProps(UCDFiles.GRAPHEME_BREAK_PROPERTY);
}
@Test
public static void testEmojiData() throws Throwable {
testProps(UCDFiles.EMOJI_DATA);
}
@Test
public static void testExcludedSpacingMarks() throws Throwable {
var mh = lookup.findStatic(
Grapheme.class, "isExcludedSpacingMark", MethodType.methodType(boolean.class, int.class));
assertFalse((boolean)mh.invokeExact(0x1065));
assertFalse((boolean)mh.invokeExact(0x1066));
}
private static void testProps(Path path) throws Throwable {
var mh = lookup.findStatic(
Grapheme.class, "getType", MethodType.methodType(int.class, int.class));
Files.lines(path)
.map(ln -> ln.replaceFirst("#.*", ""))
.filter(ln -> ln.length() != 0)
.forEach(ln -> {
String[] strs = ln.split("\\s+");
int off = strs[0].indexOf("..");
int cp0, cp1;
String expected = strs[2];
if (off != -1) {
cp0 = Integer.parseInt(strs[0], 0, off, 16);
cp1 = Integer.parseInt(strs[0], off + 2, strs[0].length(), 16);
} else {
cp0 = cp1 = Integer.parseInt(strs[0], 16);
}
for (int cp = cp0; cp <= cp1; cp++) {
// Ignore Emoji* for now (only interested in Extended_Pictographic)
if (expected.startsWith("Emoji")) {
continue;
}
// NOTE:
// #tr29 "plus a few General_Category = Spacing_Mark needed for
// canonical equivalence."
// For "extended grapheme clusters" support, there is no
// need actually to diff "Extend" and "SpacingMark" given GB9, GB9a.
try {
if (!expected.equals(types[(int) mh.invokeExact(cp)])) {
if ("Extend".equals(expected) &&
"SpacingMark".equals(types[(int) mh.invokeExact(cp)]))
System.out.printf("[%x] [%s][%d] -> [%s]%n",
cp, expected, Character.getType(cp), types[(int) mh.invokeExact(cp)]);
else
fail(String.format(
"cp=[%x], expected:[%s] result:[%s]%n",
cp, expected, types[(int) mh.invokeExact(cp)]));
}
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
});
}
private static final String[] types = {
"Other", "CR", "LF", "Control", "Extend", "ZWJ", "Regional_Indicator",
"Prepend", "SpacingMark",
"L", "V", "T", "LV", "LVT",
"Extended_Pictographic"};
}