Skip to content

Commit 6fc9f0f

Browse files
committed
Remove unnecessay allocations and add support for \n in patterns
1 parent 1f819fd commit 6fc9f0f

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

node-graph/gcore/src/logic.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ fn string_length(_: impl Ctx, string: String) -> f64 {
6969
string.chars().count() as f64
7070
}
7171

72+
// Get an indexed part of string whitch separated a specified delimeter ("1;2;3" e.t.c.)
73+
#[node_macro::node(category("Text"))]
74+
fn substring_by_index(_: impl Ctx, string: String, #[default("\\n")] delimeter: String, index: u32) -> String {
75+
let delimeter = delimeter.replace("\\n", "\n");
76+
string.split(&delimeter).nth(index as usize).unwrap_or("").to_owned()
77+
}
78+
79+
// Get amount substrings like ";" in string (useful for check max index in substring_by_index)
80+
#[node_macro::node(category("Text"))]
81+
fn count_substring(_: impl Ctx, string: String, #[default("\\n")] substring: String) -> f64 {
82+
let substring = substring.replace("\\n", "\n");
83+
string.matches(&substring).count() as f64
84+
}
85+
7286
/// Evaluates either the "If True" or "If False" input branch based on whether the input condition is true or false.
7387
#[node_macro::node(category("Math: Logic"))]
7488
async fn switch<T, C: Send + 'n + Clone>(
@@ -120,20 +134,3 @@ async fn switch<T, C: Send + 'n + Clone>(
120134
if_false.eval(ctx).await
121135
}
122136
}
123-
124-
// Get an indexed part of string whitch separated a specified delimeter ("1;2;3" e.t.c.)
125-
#[node_macro::node(category("Text"))]
126-
fn substring_by_index(_: impl Ctx, #[implementations(String)] string: String, delimeter: String, index: u32) -> String {
127-
let idx = index as usize;
128-
let parts: Vec<&str> = string.split(&delimeter).collect();
129-
if idx < parts.len() { parts[idx].to_string() } else { String::new() }
130-
}
131-
132-
// Get amount substrings like ";" in string (useful for check max index in substring_by_index)
133-
#[node_macro::node(category("Text"))]
134-
fn count_substring(_: impl Ctx, #[implementations(String)] string: String, substring: String) -> f64 {
135-
if substring.is_empty() {
136-
return 0.;
137-
}
138-
string.matches(&substring).count() as f64
139-
}

node-graph/node-macro/src/codegen.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ pub(crate) fn generate_node_code(crate_ident: &CrateIdent, parsed: &ParsedNodeFn
8888
.iter()
8989
.map(|field| match &field.ty {
9090
ParsedFieldType::Regular(RegularParsedField { value_source, .. }) => match value_source {
91-
ParsedValueSource::Default(data) => quote!(RegistryValueSource::Default(stringify!(#data))),
91+
ParsedValueSource::Default(data) => {
92+
// Check if the data is a string literal by parsing the token stream
93+
let data_str = data.to_string();
94+
if data_str.starts_with('"') && data_str.ends_with('"') && data_str.len() >= 2 {
95+
quote!(RegistryValueSource::Default(#data))
96+
} else {
97+
quote!(RegistryValueSource::Default(stringify!(#data)))
98+
}
99+
}
92100
ParsedValueSource::Scope(data) => quote!(RegistryValueSource::Scope(#data)),
93101
_ => quote!(RegistryValueSource::None),
94102
},

0 commit comments

Comments
 (0)