markdown_utils/
codespan.rs1pub fn n_backticks_to_wrap_codespan(
22 character: char,
23 text: &str,
24) -> usize {
25 let mut in_a_rows: Vec<usize> = vec![];
26 let mut current_in_a_row = 0;
27
28 for c in text.chars() {
29 if c == character {
30 current_in_a_row += 1;
31 } else if current_in_a_row > 0 {
32 if !in_a_rows.contains(¤t_in_a_row) {
33 in_a_rows.push(current_in_a_row)
34 }
35 current_in_a_row = 0;
36 }
37 }
38 if current_in_a_row > 0 && !in_a_rows.contains(¤t_in_a_row) {
39 in_a_rows.push(current_in_a_row)
40 }
41
42 let mut result = 0;
43 if in_a_rows.len() > 0 {
44 let max = in_a_rows.iter().max().unwrap() + 2;
45 for n in 1..max {
46 if !in_a_rows.contains(&n) {
47 result = n;
48 break;
49 }
50 }
51 }
52 result
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58 use rstest::rstest;
59
60 #[rstest]
61 #[case('c', &"c tt ccc cccc", 2)]
62 #[case('c', &"c cc ttt cccc", 3)]
63 #[case('c', &"t cc ttt cccc", 1)]
64 #[case('c', &"c cc ccc tttt", 4)]
65 fn n_backticks_to_wrap_codespan_test(
66 #[case] character: char,
67 #[case] text: &str,
68 #[case] expected: usize,
69 ) {
70 assert_eq!(
71 n_backticks_to_wrap_codespan(character, text),
72 expected,
73 );
74 }
75}