aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorJacob Pratt <[email protected]>2021-01-10 02:36:38 +0000
committerJacob Pratt <[email protected]>2021-01-10 02:36:38 +0000
commitbd8a9035481ff2c349a36471c8c7c3533c3385c3 (patch)
tree1f553b31f0e6c9505f6ef864a8c2baad10024b1a /crates
parentbcb9ae18de1fa405eb2add56409ae264267607bb (diff)
Remove unnecessary allocation
The case-insensitive prefix/suffix check can be performed character-by-character. This allows the check to be done without having to allocate a new string. As a side effect, it's also no longer necessary to convert the entire string to lowercase, as it's done as needed. As the only case equality we're handling is ASCII, this operation can be further optimized by using byte equality, rather than character equality.
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/inlay_hints.rs19
1 files changed, 12 insertions, 7 deletions
diff --git a/crates/ide/src/inlay_hints.rs b/crates/ide/src/inlay_hints.rs
index a74829cd0..ddab9a168 100644
--- a/crates/ide/src/inlay_hints.rs
+++ b/crates/ide/src/inlay_hints.rs
@@ -353,13 +353,18 @@ fn is_argument_similar_to_param_name(
353 } 353 }
354 match get_string_representation(argument) { 354 match get_string_representation(argument) {
355 None => false, 355 None => false,
356 Some(mut repr) => { 356 Some(argument_string) => {
357 let param_name = param_name.to_ascii_lowercase(); 357 let mut arg_bytes = argument_string.bytes().skip_while(|&c| c == b'_');
358 let argument_string = { 358 let starts_with_pattern = param_name.bytes().all(
359 repr.make_ascii_lowercase(); 359 |expected| matches!(arg_bytes.next(), Some(actual) if expected.eq_ignore_ascii_case(&actual)),
360 repr.trim_start_matches('_') 360 );
361 }; 361
362 argument_string.starts_with(&param_name) || argument_string.ends_with(&param_name) 362 let mut arg_bytes = argument_string.bytes();
363 let ends_with_pattern = param_name.bytes().rev().all(
364 |expected| matches!(arg_bytes.next_back(), Some(actual) if expected.eq_ignore_ascii_case(&actual)),
365 );
366
367 starts_with_pattern || ends_with_pattern
363 } 368 }
364 } 369 }
365} 370}