aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormat <git@matdoes.dev>2026-03-27 14:13:54 -0845
committermat <git@matdoes.dev>2026-03-27 14:13:54 -0845
commit0d0e253f8b5aa7669a64e8345680118c340bebab (patch)
tree611f289171560c0626184204675ee9675a5e32e1
parentf09d07ce5cc6f8dfa336e214cda95b157bb80ae4 (diff)
downloadazalea-drasl-0d0e253f8b5aa7669a64e8345680118c340bebab.tar.xz
optimize TranslatableComponent::read
-rw-r--r--azalea-chat/src/translatable_component.rs100
1 files changed, 49 insertions, 51 deletions
diff --git a/azalea-chat/src/translatable_component.rs b/azalea-chat/src/translatable_component.rs
index ed8f1d76..a846b550 100644
--- a/azalea-chat/src/translatable_component.rs
+++ b/azalea-chat/src/translatable_component.rs
@@ -106,7 +106,6 @@ impl TranslatableComponent {
});
// decode the % things
- let mut i = 0;
let mut matched = 0;
// every time we get a char we add it to built_text, and we push it to
@@ -114,60 +113,59 @@ impl TranslatableComponent {
let mut built_text = String::new();
let mut components = Vec::new();
- while i < template.chars().count() {
- if template.chars().nth(i).unwrap() == '%' {
- let Some(char_after) = template.chars().nth(i + 1) else {
+ let mut chars = template.chars();
+ while let Some(char) = chars.next() {
+ if char != '%' {
+ built_text.push(char);
+ continue;
+ }
+
+ let mut chars_preview = chars.clone();
+ let Some(char_after) = chars_preview.next() else {
+ built_text.push('%');
+ break;
+ };
+ match char_after {
+ '%' => {
+ chars.next();
+
built_text.push('%');
- break;
- };
- i += 1;
- match char_after {
- '%' => {
- built_text.push('%');
- }
- 's' => {
- let arg_component = self
- .args
- .get(matched)
- .cloned()
- .unwrap_or_else(|| PrimitiveOrComponent::String("".to_owned()));
+ }
+ 's' => {
+ chars.next();
+
+ let arg_component = self
+ .args
+ .get(matched)
+ .cloned()
+ .unwrap_or_else(|| PrimitiveOrComponent::String("".to_owned()));
- components.push(TextComponent::new(built_text.clone()));
- built_text.clear();
- components.push(TextComponent::from(arg_component));
- matched += 1;
- }
- _ => {
- // check if the char is a number
- if let Some(d) = char_after.to_digit(10) {
- // make sure the next two chars are $s
- if let Some('$') = template.chars().nth(i + 1) {
- if let Some('s') = template.chars().nth(i + 2) {
- i += 2;
- built_text.push_str(
- &self
- .args
- .get((d - 1) as usize)
- .unwrap_or(&PrimitiveOrComponent::String("".to_owned()))
- .to_string(),
- );
- } else {
- return Err(fmt::Error);
- }
- } else {
- return Err(fmt::Error);
- }
- } else {
- i -= 1;
- built_text.push('%');
- }
- }
+ components.push(TextComponent::new(built_text.clone()));
+ built_text.clear();
+ components.push(TextComponent::from(arg_component));
+ matched += 1;
+ }
+ '0'..='9' if let Some(d) = char_after.to_digit(10) => {
+ chars.next();
+ // make sure the next two chars are $s
+ let Some('$') = chars.next() else {
+ return Err(fmt::Error);
+ };
+ let Some('s') = chars.next() else {
+ return Err(fmt::Error);
+ };
+ built_text.push_str(
+ &self
+ .args
+ .get((d - 1) as usize)
+ .unwrap_or(&PrimitiveOrComponent::String("".to_owned()))
+ .to_string(),
+ );
+ }
+ _ => {
+ built_text.push('%');
}
- } else {
- built_text.push(template.chars().nth(i).unwrap());
}
-
- i += 1;
}
if components.is_empty() {