From 5a7ad0e7d65559feedd4a04ba09af3527387353e Mon Sep 17 00:00:00 2001 From: Ayla Croft Date: Sat, 5 Dec 2026 19:44:35 -0500 Subject: [PATCH] Write a class holding a property as one class Under the default reading a character class holding a property, and a property on its own, were written out as a group: \p{L}* became (?:\p{L})*. A repeated group keeps a frame for each repetition, so ^\p{L}*$ refused a string of 100,000 letters ("the memory a check may hold"), where 1.0.2 and pattern_dialect: :pcre serve it. Such a class is now one class of the library's, with \p{...} inside it, negated where the class is: [\x{30}-\x{39}\p{L}], [^\p{N}]. It matches the same code points, and repeated it holds no frame per repetition. The differential against node, the property comparison and the uncharged canary give the same answers as before on OTP 28 and 29. Signed-off-by: Ayla Croft --- lib/beam_mcp/schema/ecma.ex | 39 +++++++++++--------------- test/beam_mcp/pattern_dialect_test.exs | 23 +++++++++++++++ 2 files changed, 39 insertions(+), 23 deletions(-) diff --git a/lib/beam_mcp/schema/ecma.ex b/lib/beam_mcp/schema/ecma.ex index cf96e30..2943f40 100644 --- a/lib/beam_mcp/schema/ecma.ex +++ b/lib/beam_mcp/schema/ecma.ex @@ -961,36 +961,29 @@ defmodule BeamMCP.Schema.ECMA do defp emit({:set, :any_of, ranges, []}), do: class_text(ranges) defp emit({:set, :none_of, ranges, []}), do: class_text(complement(ranges)) - defp emit({:set, :any_of, ranges, props}) do - parts = - if(ranges == [], do: [], else: [class_text(ranges)]) ++ Enum.map(props, &property_text/1) + # A class holding a property is one class of the library's, `\\p{...}` inside it as outside, + # so it matches one code point as the class does and, repeated, holds no memory for each + # repetition (a group does: `(?:\\p{L})*` met the frame bound where `\\p{L}*` does not). A + # negated one matches any code point in neither: under UTF the library's subject holds no + # surrogate, so the complement is ECMA-262's. + defp emit({:set, :any_of, ranges, props}), + do: ["[", range_text(ranges), Enum.map(props, &property_text/1), "]"] - ["(?:", Enum.intersperse(parts, "|"), ")"] - end - - defp emit({:set, :none_of, ranges, props}) do - [ - "(?:", - Enum.map(props, &["(?!", property_text(&1), ")"]), - class_text(complement(ranges)), - ")" - ] - end + defp emit({:set, :none_of, ranges, props}), + do: ["[^", range_text(ranges), Enum.map(props, &property_text/1), "]"] defp property_text({true, name}), do: ["\\p{", name, "}"] defp property_text({false, name}), do: ["\\P{", name, "}"] defp class_text([]), do: "(?:(?!))" - defp class_text(ranges) do - [ - "[", - Enum.map(ranges, fn - {a, a} -> code_point(a) - {a, b} -> [code_point(a), "-", code_point(b)] - end), - "]" - ] + defp class_text(ranges), do: ["[", range_text(ranges), "]"] + + defp range_text(ranges) do + Enum.map(ranges, fn + {a, a} -> code_point(a) + {a, b} -> [code_point(a), "-", code_point(b)] + end) end defp code_point(c), do: ["\\x{", Integer.to_string(c, 16), "}"] diff --git a/test/beam_mcp/pattern_dialect_test.exs b/test/beam_mcp/pattern_dialect_test.exs index c665a8e..fc184f2 100644 --- a/test/beam_mcp/pattern_dialect_test.exs +++ b/test/beam_mcp/pattern_dialect_test.exs @@ -150,6 +150,29 @@ defmodule BeamMCP.PatternDialectTest do end end + # A class holding a property is written as one class of the library's, as a plain class is, + # not as a group: a repeated group holds memory for each repetition, and 1.0.2 served these + # at any length the body allows. + test "a repeated property or class holding one is served at any length, as a plain class is" do + letters = String.duplicate("a", 200_000) + marks = String.duplicate("!", 200_000) + + for {pattern, string} <- [ + {"^\\p{L}*$", letters}, + {"^\\P{L}*$", marks}, + {"^[\\p{L}\\d]*$", letters}, + {"^[^\\p{N}]*$", letters}, + {"^[^\\p{L}a-z]+$", marks}, + {"^\\p{Script=Latin}{1,65535}$", String.duplicate("a", 65_535)} + ] do + assert matches?(pattern, string), pattern + assert matches?(pattern, string, pattern_dialect: :pcre), pattern + end + + refute matches?("^[^\\p{L}a-z]+$", "!a") + refute matches?("^[\\p{L}\\d]*$", "a!") + end + test "the dialect is :ecma or :pcre, and anything else raises" do for opts <- [[pattern_dialect: :ecma262], [pattern_dialect: "pcre"], [pattern_dialect: nil]] do assert_raise ArgumentError, ~r/pattern_dialect must be :ecma or :pcre/, fn ->