Update cxjs_substring() to match exp_fn_substring() - #122
Conversation
Greptile SummaryThis PR updates
Confidence Score: 5/5Safe to merge — the change is narrowly scoped to one JS helper function and correctly mirrors the C reference implementation. The new logic precisely replicates what exp_fn_substring() does in C: 1-based position adjusted to 0-based, both start and length clamped to valid range, and explicit type checks returning null for invalid input types. Edge cases (zero/negative position, length larger than remaining string, null third argument) were verified against the C implementation and all match. Replacing the deprecated substr() with slice() is a clean improvement with no behavioral difference for valid inputs. No files require special attention — only one function in one JS file changed.
|
| Filename | Overview |
|---|---|
| centrallix-os/sys/js/ht_render.js | cxjs_substring() updated to match server-side behavior: adds type guards, fixes negative/zero position clamping, replaces deprecated substr() with slice(), and clamps length correctly. |
Sequence Diagram
sequenceDiagram
participant Client as Browser cxjs_substring
participant Engine as Server exp_fn_substring
Note over Client,Engine: Before this PR
Client->>Client: "String(s).substr(p-1) — no clamping"
Engine->>Engine: "validate types, clamp start and length"
Note over Client,Engine: After this PR — aligned behavior
Client->>Client: "typeof s != string → return null"
Engine->>Engine: "DataType != DATA_T_STRING → return error"
Client->>Client: "i = p-1, clamp to 0 or s.length"
Engine->>Engine: "i = i1-1, clamp to 0 or n"
Client->>Client: "len = l ?? rest.length, clamp to rest.length"
Engine->>Engine: "i = i2->Integer or strlen(ptr), clamp"
Client->>Client: "return rest.slice(0, len)"
Engine->>Engine: "memcpy into tree->String"
Reviews (1): Last reviewed commit: "Update cxjs_substring() to be more simil..." | Re-trigger Greptile
|
This PR is ready for human review. |
cxjs_substring() to match exp_fn_substring().cxjs_substring() to match exp_fn_substring()
gbeeley
left a comment
There was a problem hiding this comment.
Change needed for string object compatibility.
I caused a bug in the
gift_associations.appbecause the client-side version ofsubstring()functioned differently from the server side version, as documented in #121. This PR will close #121 by solving this inconsistency issue.