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
2 changes: 1 addition & 1 deletion COMPATIBILITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ likely not be implemented due to differences between piccolo and PUC-Lua.
| 🔵 | `char(args...)` | | |
| ⚫️️ | `dump(function[, strip])` | | |
| ⚫️️ | `find(s, pattern[, init, plain])` | | |
| ⚫️️ | `format(formatstring, args...)` | | |
| 🔵 | `format(formatstring, args...)` | | |
| ⚫️️ | `gmatch(s, pattern[, init])` | | |
| ⚫️️ | `gsub(s, pattern, repl[, n])` | | |
| 🔵 | `len(s)` | | |
Expand Down
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ repository.workspace = true
ahash.workspace = true
allocator-api2.workspace = true
anyhow.workspace = true
fhex = "1.0.0"
gc-arena.workspace = true
hashbrown.workspace = true
rand.workspace = true
Expand Down
23 changes: 22 additions & 1 deletion src/stdlib/string.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{Callback, CallbackReturn, Context, FromValue, String, Table, Value};
use crate::{Callback, CallbackReturn, Context, FromValue, IntoValue, String, Table, Value};

mod format;

pub fn load_string<'gc>(ctx: Context<'gc>) {
let string = Table::new(&ctx);
Expand Down Expand Up @@ -97,6 +99,25 @@ pub fn load_string<'gc>(ctx: Context<'gc>) {
}),
);

string.set_field(
ctx,
"format",
Callback::from_fn(&ctx, |ctx, _, mut stack| {
let formatstring = stack.consume::<String>(ctx)?;
let formatstring = formatstring.to_str()?;

let args: Vec<Value> = stack.into_iter().collect();

let formatted = format::format(&ctx, formatstring, &args).map_err(|err| {
let err = err.to_string();
err.into_value(ctx)
})?;

stack.replace(ctx, formatted);
Ok(CallbackReturn::Return)
}),
);

ctx.set_global("string", string);
}

Expand Down
Loading