Skip to content

Commit

Permalink
Move allocation outside of wrap application
Browse files Browse the repository at this point in the history
  • Loading branch information
cdesaintguilhem committed Oct 13, 2024
1 parent 76d9ef0 commit 9bc0a3b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lazy_static = "1.5.0"
log = "0.4.22"
regex = "1.11.0"
similar = "2.6.0"
unicode-width = "0.2.0"

[profile.release]
codegen-units = 1
Expand Down
11 changes: 8 additions & 3 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ pub fn format_file(
if needs_wrap(&line, &temp_state, args) {
let wrapped_lines =
apply_wrap(&line, &temp_state, file, args, logs);
if let Some((this_line, next_line)) = wrapped_lines {
queue.push((linum_old, next_line));
queue.push((linum_old, this_line));
if let Some([this_line, next_line_start, next_line]) =
wrapped_lines
{
queue.push((
linum_old,
[next_line_start, next_line].concat(),
));
queue.push((linum_old, this_line.to_string()));
continue;
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,14 @@ fn read_files_from_dir(dir: &str) -> Vec<String> {
#[test]
fn test_source() {
let source_files = read_files_from_dir("./tests/source/");
let mut fail = false;
for file in source_files {
if !test_file(
&format!("tests/source/{file}"),
&format!("tests/target/{file}"),
) {
fail = true;
panic!("Failed in {file}");
}
}
assert!(!fail, "Some tests failed");
}

#[test]
Expand Down
37 changes: 26 additions & 11 deletions src/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ use crate::format::*;
use crate::logging::*;
use crate::parse::*;
use log::Level::{Trace, Warn};
use unicode_width::UnicodeWidthChar;

// String slice to start wrapped text lines
pub const TEXT_LINE_START: &str = "";
// String slice to start wrapped comment lines
pub const COMMENT_LINE_START: &str = "% ";

/// Check if a line needs wrapping
pub fn needs_wrap(line: &str, state: &State, args: &Cli) -> bool {
Expand All @@ -19,8 +25,13 @@ fn find_wrap_point(line: &str, args: &Cli) -> Option<usize> {
let mut wrap_point: Option<usize> = None;
let mut after_char = false;
let mut prev_char: Option<char> = None;
for (i, c) in line.chars().enumerate() {
if i >= args.wrap_min.into() && wrap_point.is_some() {

let mut line_width = 0;

// Return *byte* index rather than *char* index.
for (i, c) in line.char_indices() {
line_width += c.width().expect("No control characters in text.");
if line_width > args.wrap_min.into() && wrap_point.is_some() {
break;
}
if c == ' ' && prev_char != Some('\\') {
Expand All @@ -36,13 +47,13 @@ fn find_wrap_point(line: &str, args: &Cli) -> Option<usize> {
}

/// Wrap a long line into a short prefix and a suffix
pub fn apply_wrap(
line: &str,
pub fn apply_wrap<'a>(
line: &'a str,
state: &State,
file: &str,
args: &Cli,
logs: &mut Vec<Log>,
) -> Option<(String, String)> {
) -> Option<[&'a str; 3]> {
if args.trace {
record_line_log(
logs,
Expand Down Expand Up @@ -73,11 +84,15 @@ pub fn apply_wrap(
};

wrap_point.map(|p| {
let line_start =
comment_index.map_or("", |c| if p > c { "%" } else { "" });
let line_1: String = line.chars().take(p).collect();
let mut line_2: String = line.chars().skip(p).collect();
line_2.insert_str(0, line_start);
(line_1, line_2)
let this_line = &line[0..p];
let next_line_start = comment_index.map_or("", |c| {
if p > c {
COMMENT_LINE_START
} else {
TEXT_LINE_START
}
});
let next_line = &line[p + 1..];
[this_line, next_line_start, next_line]
})
}

0 comments on commit 9bc0a3b

Please sign in to comment.