From 07a58afaff73f025bd4a027b78e9a8215716e8c8 Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Fri, 21 Mar 2025 14:15:12 +0100 Subject: [PATCH] improve flip-bool --- modules/shell/helix.nix | 2 +- programs/flip-bool/src/main.rs | 27 ++++++++++++--------------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/modules/shell/helix.nix b/modules/shell/helix.nix index d24aa16..adfda34 100644 --- a/modules/shell/helix.nix +++ b/modules/shell/helix.nix @@ -102,7 +102,7 @@ d = "@vgd"; f = "@vgy"; h = ":toggle-option lsp.display-inlay-hints"; - t = "@|flip-bool"; + t = ":pipe flip-bool"; }; }; insert = { diff --git a/programs/flip-bool/src/main.rs b/programs/flip-bool/src/main.rs index 299685f..0b99ab8 100644 --- a/programs/flip-bool/src/main.rs +++ b/programs/flip-bool/src/main.rs @@ -4,7 +4,6 @@ #![feature(pattern)] use std::{ - hint::black_box, io::{Read, Write}, str::pattern::Pattern, }; @@ -86,29 +85,27 @@ fn find_bools(input: &str) -> [[Vec; 2]; BOOL_COUNT] { input .match_indices(it) .filter_map(|it| { - let char_guard = |c: char| !(c.is_alphanumeric() || c.is_contained_in("-_")); - let mut allow = true; + fn char_guard(c: char) -> bool { + !(c.is_alphanumeric() || c.is_contained_in("-_")) + } - if it.0 > 0 { - allow &= char_guard( + let last_idx = it.0 + it.1.len(); + + (it.0 > 0 + && last_idx < input.len() + && char_guard( input[it.1.floor_char_boundary(it.0 - 1)..it.0] .chars() .last() .unwrap(), - ); - } - - let last_idx = it.0 + it.1.len(); - if last_idx < input.len() { - allow &= char_guard( + ) + && char_guard( input[(last_idx)..(input.ceil_char_boundary(last_idx + 1))] .chars() .last() .unwrap(), - ); - } - - allow.then_some(it.0) + )) + .then_some(it.0) }) .collect() })