Skip to content

Implement GString concatenation operator #1117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
4 changes: 3 additions & 1 deletion godot-core/src/global/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@

mod print;

pub use crate::{godot_error, godot_print, godot_print_rich, godot_script_error, godot_warn};
pub use crate::{
godot_error, godot_print, godot_print_rich, godot_script_error, godot_str, godot_warn,
};

// Some enums are directly re-exported from crate::builtin.
pub use crate::gen::central::global_enums::*;
Expand Down
12 changes: 12 additions & 0 deletions godot-core/src/global/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ macro_rules! godot_print_rich {
])
};
}

/// Concatenates format-style into a `GString`.
#[macro_export]
macro_rules! godot_str {
($fmt:literal $(, $args:expr)* $(,)?) => {
$crate::global::str(&[
$crate::builtin::Variant::from(
format!($fmt $(, $args)*)
)
])
};
}
13 changes: 9 additions & 4 deletions itest/rust/src/engine_tests/utilities_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@ fn utilities_sign() {

#[itest]
fn utilities_str() {
let a = 12;
let b = " is a ";
let c = true;
let d = " number";
let concat = str(&[
Variant::from(12),
Variant::from(" is a "),
Variant::from(true),
Variant::from(" number"),
Variant::from(a),
Variant::from(b),
Variant::from(c),
Variant::from(d),
]);

let empty = str(&[]);

// TODO: implement GString==&str operator. Then look for "...".into() patterns and replace them.
assert_eq!(concat, "12 is a true number".into());
assert_eq!(concat, godot_str!("{a}{b}{c}{d}"));
assert_eq!(empty, GString::new());
}

Expand Down
Loading