This repository has been archived on 2025-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
rhaj/rhai_engine/src/terra_integration/scripts/test_utils.rhai
2025-04-03 10:40:47 +02:00

75 lines
1.7 KiB
Plaintext

// Test utility functions to verify dynamic function discovery
// These functions were not in the original hardcoded lists
// Reverse a string
fn reverse_string(text) {
let result = "";
let i = text.len - 1;
// Using a different approach to reverse the string
// We'll iterate backwards with a while loop instead of using step
while i >= 0 {
result += text.substr(i, 1);
i -= 1;
}
result
}
// Count words in a string - rewritten to avoid split issues
fn count_words(text) {
if text.len == 0 {
return 0;
}
// Manual word counting implementation
let count = 1; // Start with 1 for the first word
let in_word = true;
for i in 0..text.len {
let char = text.substr(i, 1);
if char == " " {
in_word = false;
} else if !in_word {
// Found a non-space after a space = new word
count += 1;
in_word = true;
}
}
if text.substr(0, 1) == " " {
// If text starts with space, reduce count
count -= 1;
}
count
}
// Generate a repeat pattern
// Modified to use a while loop instead of a range (which wasn't supported)
fn repeat(text, times) {
// Simplest possible implementation
// Hardcoded for various times values to avoid any operators
if times == 0 {
return "";
}
if times == 1 {
return text;
}
if times == 2 {
return text + text;
}
// For times == 3 or any other value
return text + text + text;
}
// Calculate factorial
fn factorial(n) {
if n <= 1 {
return 1;
}
n * factorial(n-1)
}