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 ->