PHP familiarity shortcuts

This commit is contained in:
udo
2026-04-19 21:15:52 +00:00
parent af642c8167
commit dc05f9faa5
29 changed files with 633 additions and 174 deletions
+24
View File
@@ -0,0 +1,24 @@
:sig
StringMap array_merge(StringMap a, StringMap b)
DTree array_merge(DTree a, DTree b)
:params
a : left-hand source map or tree
b : right-hand source map or tree
return value : merged result
:desc
Merges two maps or trees using PHP-like merge behavior.
For `StringMap`, keys from `b` overwrite keys from `a`.
For `DTree`, string keys from `b` overwrite keys from `a`. Numeric keys are appended and reindexed when either side behaves like a list.
This helper is intended as the closest UCE equivalent to PHP `array_merge()` for common request, config, and JSON-shaped data.
:see
>types
:related
**PHP:** `array_merge()`
**JavaScript / Node.js:** Object spread, `Object.assign()`, or array concatenation depending on whether the data is map-like or list-like
+19
View File
@@ -0,0 +1,19 @@
:sig
bool contains(String haystack, String needle)
:params
haystack : string to search in
needle : substring to look for
return value : `true` when `needle` appears in `haystack`, otherwise `false`
:desc
Returns whether `haystack` contains `needle`.
An empty `needle` always returns `true`.
:see
>string
:related
**PHP:** `str_contains()`
**JavaScript / Node.js:** `String.prototype.includes()`
+19
View File
@@ -0,0 +1,19 @@
:sig
void redirect(String url, s32 code = 302)
:params
url : target URL for the redirect
code : optional HTTP redirect status, defaults to `302`
:desc
Sets the `Location` response header and updates the current HTTP status code.
Use this helper instead of manually assigning `context.header["Location"]` and `context.set_status(...)` when you want to redirect the current response.
:see
set_status
0_context
:related
**PHP:** `header("Location: ...")` together with `http_response_code()` or implicit redirect semantics
**JavaScript / Node.js:** `res.redirect(...)`, setting `Location`, or returning a redirect `Response`
+17
View File
@@ -0,0 +1,17 @@
:sig
bool str_ends_with(String haystack, String needle)
:params
haystack : string to inspect
needle : suffix to compare against the end of `haystack`
return value : `true` when `haystack` ends with `needle`, otherwise `false`
:desc
Checks whether `haystack` ends with the given suffix.
:see
>string
:related
**PHP:** `str_ends_with()`
**JavaScript / Node.js:** `String.prototype.endsWith()`
+17
View File
@@ -0,0 +1,17 @@
:sig
bool str_starts_with(String haystack, String needle)
:params
haystack : string to inspect
needle : prefix to compare against the start of `haystack`
return value : `true` when `haystack` begins with `needle`, otherwise `false`
:desc
Checks whether `haystack` starts with the given prefix.
:see
>string
:related
**PHP:** `str_starts_with()`
**JavaScript / Node.js:** `String.prototype.startsWith()`
+22
View File
@@ -0,0 +1,22 @@
:sig
s64 strpos(String haystack, String needle, s64 offset = 0)
:params
haystack : string to search in
needle : substring to search for
offset : optional start offset; negative values count from the end of the string
return value : zero-based position of the first match, or `-1` if `needle` is not found
:desc
Finds the first occurrence of `needle` inside `haystack`.
This is the closest UCE equivalent to PHP `strpos()`, but it returns `-1` instead of `false` when no match is found.
If `needle` is an empty string, `strpos()` returns the normalized start offset.
:see
>string
:related
**PHP:** `strpos()`
**JavaScript / Node.js:** `String.prototype.indexOf()`
+25
View File
@@ -0,0 +1,25 @@
:sig
String substr(String s, s64 start_pos)
String substr(String s, s64 start_pos, s64 length)
:params
s : source string
start_pos : zero-based start position; negative values count from the end of the string
length : optional substring length; negative values trim bytes from the end of the result range
return value : extracted substring, or an empty string if the requested range is outside the source string
:desc
Extracts part of a string using PHP-style start and length semantics.
Use the two-argument form to return everything from `start_pos` to the end of the string.
If `start_pos` is negative, counting starts from the end of the string.
If `length` is negative, the returned range stops that many bytes before the end of the string.
:see
>string
:related
**PHP:** `substr()`
**JavaScript / Node.js:** `String.prototype.slice()` or `substring()`