Skip to content

feat(craft): add text, number, regex and date functions - #58

Open
innkuika wants to merge 1 commit into
craft/building-blocksfrom
craft/stdlib
Open

innkuika wants to merge 1 commit into
craft/building-blocksfrom
craft/stdlib

Conversation

@innkuika

@innkuika innkuika commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Description

Adds the standard functions templates were missing for real forms: text, number parsing, regular expressions and dates. Stacked on #57 (stack #51).

  • Text: split, join, literal replace; length now also counts list elements.
  • Numbers: toInt (strict [+-]?digits, exact 64-bit range) and toNumber (strict decimals, exactly rounded, ties to even, negative zero kept); absent on failure.
  • Regular expressions: regex (is there a match), regexFind, regexCapture, regexReplace. Names follow A2UI's catalog functions where they overlap; the blueprint lists the differences. Craft uses its own linear-time engine (an RE2-style Pike VM), never the platform RegExp, so results are identical on VM, JS and Wasm and no pattern can blow up. The dialect is an ECMAScript (u) subset without lookaround or backreferences; pattern size and length are capped.
  • Dates: dateToDays, daysToDate, daysBetween for calendar-valid YYYY-MM-DD (years 0000–9999).
  • Limits: every function whose work grows with its input charges steps by input and output length (including compiling a pattern), so an agent-supplied template can't stall the UI within the limits. The evaluator gains a stepsExceeded error.
  • Tests: 50 new functions cases and 12 limits cases, with controls, plus unit tests. The conformance harness now compares doubles including their sign. Passes on VM and with dart test -p node; two integer-vs-double cases are JS-only skips.

An independent review checked: 225k regex pattern/value pairs against ECMAScript (matches, captures, replace); a reimplementation from the blueprint alone that agrees on every case; linear time; 180k toNumber inputs bit-identical to a reference; every date in range; identical results on VM, JS and Wasm.

Pre-launch Checklist

  • I signed the CLA.
  • I read the Contributors Guide.
  • I updated/added relevant documentation.
  • My code changes (if any) have tests.
  • License checks pass (python3 ./scripts/fix_licenses.py --check).
  • Format checks pass (./scripts/fix_format.sh --check).

@innkuika
innkuika requested a review from ditman December 2, 2026 22:49
@innkuika
innkuika added this pull request to stack #51 December 2, 2026 22:50

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements requirements 14 to 18 of the Craft functions specification, introducing standard functions for splitting, joining, replacing, regular expressions (via a custom Pike VM for linear-time matching), parsing numbers from strings, and date calculations. It also adds metered functions that count execution steps against the runtime's budget to prevent CPU exhaustion. A critical bug was identified in regex.dart where Int32List.sublistView is incorrectly called with element indices instead of byte offsets, which would cause runtime errors; using sublist is recommended instead.

Comment on lines +68 to +69
Int32List.sublistView(points, 0, count),
Int32List.sublistView(offsets, 0, count + 1),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Using Int32List.sublistView with element indices count and count + 1 as the end parameter is a critical bug. In Dart, TypedData.sublistView expects byte offsets rather than element indices. Since Int32List has 4 bytes per element, this will throw an ArgumentError unless the index happens to be a multiple of 4, and even then, it will return a truncated view.

Using sublist(0, count) is much safer, correct, and allows the garbage collector to reclaim the unused portion of the pre-allocated lists.

      points.sublist(0, count),
      offsets.sublist(0, count + 1),

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request implements standard functions for text manipulation (splitting, joining, replacing), regex operations (using a custom Pike VM), number parsing, and date calculations, along with step-metering to prevent denial-of-service attacks. A critical issue was identified in daysToDate where passing a non-finite double (like NaN or Infinity) can cause a crash because truncateToDouble() throws an UnsupportedError on non-finite values. A fix was suggested to check !days.isFinite first.

Comment on lines +624 to +626
String? _date(num days) {
if (days is double && days != days.truncateToDouble()) return null;
if (days < _firstDay || days > _lastDay) return null;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Calling truncateToDouble() on a non-finite double (such as double.nan or double.infinity) throws an UnsupportedError in Dart. This can cause the evaluation to crash unexpectedly when an untrusted template passes a non-finite number to daysToDate. Checking !days.isFinite first will safely prevent this crash.

Suggested change
String? _date(num days) {
if (days is double && days != days.truncateToDouble()) return null;
if (days < _firstDay || days > _lastDay) return null;
String? _date(num days) {
if (!days.isFinite) return null;
if (days is double && days != days.truncateToDouble()) return null;
if (days < _firstDay || days > _lastDay) return null;

…dates

Adds the standard functions that form templates needed and hosts had to
register themselves (dogfooding: regex, split, toInt, ISO dates, list
length):

- split, join, replace (literal, linear-time search), which with toInt and
  toNumber count steps for the code units of their arguments and results;
- regex, regexFind, regexCapture, regexReplace in a regex dialect: a subset
  of ECMAScript patterns (u flag, no lookaround, backreferences or
  quantified atoms that can match the empty string) run by a Pike VM in
  time linear in the value and identically on the VM, dart2js and
  dart2wasm; results are ECMAScript's (leftmost-first priority, captures
  cleared at each iteration). Patterns are limited to 256 code points and
  size 1,000, and compiling a pattern and each search count steps against
  maxSteps;
- toInt and toNumber (strict decimals, exact 64-bit range check, exactly
  rounded doubles);
- dateToDays, daysToDate and daysBetween (calendar-valid YYYY-MM-DD);
- length of a list.

craft_functions gains requirements 14 to 18 and the stepsExceeded
evaluation error; craft_limits counts the steps of these functions in an
expansion and a firing. The conformance harness now compares doubles
with their sign, so -0.0 is not 0.0. Names follow A2UI's basic and MCP catalogs where
they overlap (regex, split, regexCapture, regexReplace); the blueprint
lists the differences.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant