Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
504 changes: 172 additions & 332 deletions docs/functions.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Helpers/StringEscaping.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public static function unescapeString(string $str): string {
*/
public static function escapeString(
string $str,
string $quoteChar = \null
?string $quoteChar = \null
): string {

foreach (self::ESCAPE_PAIRS as $out => $in) {
Expand Down
2 changes: 1 addition & 1 deletion src/Repl.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Repl {

public function __construct(
?string $replName = null,
ReplIoDriverInterface $driver = null
?ReplIoDriverInterface $driver = null
) {

self::$historyFilePath = getenv("HOME") . '/' . self::HISTORY_FILE;
Expand Down
2 changes: 1 addition & 1 deletion src/Stdlib/Modules/std/math.primi.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static function atan(NumberValue $n): NumberValue {
#[PrimiFunc]
public static function round(
NumberValue $n,
NumberValue $precision = \null
?NumberValue $precision = \null
): NumberValue {
return Interned::number((string) \round(
(float) $n->value,
Expand Down
18 changes: 9 additions & 9 deletions src/Stdlib/TypeExtensions/BoolTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function not(BoolValue $value): BoolValue {
* ```js
* true.and(false) == false
* true.and(true) == true
* bool_and(true, false) == false
* bool.and(true, false) == false
* ```
*/
#[PrimiFunc]
Expand All @@ -69,10 +69,10 @@ public static function and(BoolValue $a, BoolValue $b): BoolValue {
* false.or(true) == true
* false.or(false) == false
*
* bool_or(true, true) == true
* bool_or(true, false) == true
* bool_or(false, true) == true
* bool_or(false, false) == false
* bool.or(true, true) == true
* bool.or(true, false) == true
* bool.or(false, true) == true
* bool.or(false, false) == false
* ```
*/
#[PrimiFunc]
Expand All @@ -89,10 +89,10 @@ public static function or(BoolValue $a, BoolValue $b): BoolValue {
* false.xor(true) == true
* false.xor(false) == false
*
* bool_xor(true, true) == false
* bool_xor(true, false) == true
* bool_xor(false, true) == true
* bool_xor(false, false) == false
* bool.xor(true, true) == false
* bool.xor(true, false) == true
* bool.xor(false, true) == true
* bool.xor(false, false) == false
* ```
*/
#[PrimiFunc]
Expand Down
2 changes: 1 addition & 1 deletion src/Stdlib/TypeExtensions/ListTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public static function contains(
public static function get(
ListValue $list,
NumberValue $index,
AbstractValue $default = \null
?AbstractValue $default = \null
): AbstractValue {

// If the index is not found, this will return null.
Expand Down
41 changes: 37 additions & 4 deletions src/Stdlib/TypeExtensions/StringTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -482,10 +482,11 @@ public static function join(

/**
* Returns `true` if the string starts with specified string.
*
* Returns `false` otherwise.
*
* ```js
* "this is a sentence".starts_with("tence") == true
* "this is a sentence".starts_with("e") == true
* "this is a sentence".starts_with("this") == true
* "this is a sentence".starts_with("t") == true
* "this is a sentence".starts_with("x") == false
* ```
*/
Expand All @@ -499,7 +500,8 @@ public static function starts_with(

/**
* Returns `true` if the string ends with specified string suffix.
*
* Returns `false` otherwise.
*
* ```js
* "this is a sentence".ends_with("tence") == true
* "this is a sentence".ends_with("e") == true
Expand All @@ -514,4 +516,35 @@ public static function ends_with(
return Interned::bool(\str_ends_with($haystack->value, $needle->value));
}

/**
* Returns a substring taken from `string` starting at `offset` with length `length`.
* Access outside of bounds will be silently tolerated.
* Length can be omitted to reach up to the `string` end.
* Negative offset can be used.
*
* ```js
* "this is a sentence".substring(5, 4) == "is a"
* "this is a sentence".substring(5) == "is a sentence"
* "this is a sentence".substring(10, 100) == "sentence"
* "this is a sentence".substring(-8) == "sentence"
* "this is a sentence".substring(20, 15) == ""
* ```
*/
#[PrimiFunc]
public static function substring(
StringValue $string,
NumberValue $offset,
?NumberValue $length = \null,
): StringValue {
$input = $string->value;
$start = $offset->value ?? 0;
$len = $length?->value ?? \mb_strlen($input);

if ($start < 0) {
$start = \mb_strlen($input) + $start;
}

return new StringValue(\mb_substr($input, (int)$start, (int)$len));
}

}
15 changes: 12 additions & 3 deletions tools/docgen/docgen.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function err($text): void {

function get_relevant_methods(string $className): array {

$classRef = new \ReflectionClass("\Smuuf\Primi\Stdlib\\Extensions\\{$className}");
$classRef = new \ReflectionClass("\Smuuf\Primi\\{$className}");

// We want methods that are both public AND static AND non-PHP-magic.
return array_filter(
Expand Down Expand Up @@ -146,7 +146,7 @@ function extract_params(\ReflectionMethod $methodRef): array {
line(Colors::get("- File {cyan}$filepath{_}"));

$filename = basename($filepath);
$className = substr($filename, 0, strrpos($filename, '.'));
$className = str_replace("/", "\\", substr($filepath, 6, strrpos($filepath, '.') - 6));

$methods = get_relevant_methods($className);
$data[$className] = [];
Expand All @@ -168,7 +168,16 @@ function extract_params(\ReflectionMethod $methodRef): array {
$returnType = false;
if ($returnTypeRef = $methodRef->getReturnType()) {
try {
$returnType = ($returnTypeRef->getName())::TYPE;
$returnType = match (true) {
$returnTypeRef instanceof ReflectionUnionType => implode(
"|",
array_map(
fn(ReflectionNamedType $type) => $type->getName()::TYPE,
$returnTypeRef->getTypes(),
),
),
default => ($returnTypeRef->getName())::TYPE,
};
} catch (\Throwable $e) {
warn("Class '$className, method '$methodName', referencing non-existent Primi type having class " . $returnTypeRef->getName());
}
Expand Down