1pub mod anchor_styles;
6pub mod ast_utils;
7pub mod code_block_utils;
8pub mod early_returns;
11pub mod element_cache;
12pub mod emphasis_utils;
13pub mod fix_utils;
14pub mod header_id_utils;
15pub mod jinja_utils;
16pub mod kramdown_utils;
17pub mod line_ending;
18pub mod markdown_elements;
19pub mod mkdocs_admonitions;
20pub mod mkdocs_common;
21pub mod mkdocs_critic;
22pub mod mkdocs_footnotes;
23pub mod mkdocs_patterns;
24pub mod mkdocs_snippets;
25pub mod mkdocs_tabs;
26pub mod mkdocs_test_utils;
27pub mod mkdocstrings_refs;
28pub mod range_utils;
29pub mod regex_cache;
30pub mod skip_context;
31pub mod string_interner;
32pub mod table_utils;
33pub mod text_reflow;
34
35pub use ast_utils::AstCache;
36pub use code_block_utils::CodeBlockUtils;
37pub use line_ending::{
39 LineEnding, detect_line_ending, detect_line_ending_enum, ensure_consistent_line_endings, get_line_ending_str,
40 normalize_line_ending,
41};
42pub use markdown_elements::{ElementQuality, ElementType, MarkdownElement, MarkdownElements};
43pub use range_utils::LineIndex;
44
45pub trait StrExt {
47 fn replace_trailing_spaces(&self, replacement: &str) -> String;
49
50 fn has_trailing_spaces(&self) -> bool;
52
53 fn trailing_spaces(&self) -> usize;
55}
56
57impl StrExt for str {
58 fn replace_trailing_spaces(&self, replacement: &str) -> String {
59 let (content, ends_with_newline) = if let Some(stripped) = self.strip_suffix('\n') {
63 (stripped, true)
64 } else {
65 (self, false)
66 };
67
68 let mut non_space_len = content.len();
70 for c in content.chars().rev() {
71 if c == ' ' {
72 non_space_len -= 1;
73 } else {
74 break;
75 }
76 }
77
78 let mut result =
80 String::with_capacity(non_space_len + replacement.len() + if ends_with_newline { 1 } else { 0 });
81 result.push_str(&content[..non_space_len]);
82 result.push_str(replacement);
83 if ends_with_newline {
84 result.push('\n');
85 }
86
87 result
88 }
89
90 fn has_trailing_spaces(&self) -> bool {
91 self.trailing_spaces() > 0
92 }
93
94 fn trailing_spaces(&self) -> usize {
95 let content = self.strip_suffix('\n').unwrap_or(self);
99
100 let mut space_count = 0;
102 for c in content.chars().rev() {
103 if c == ' ' {
104 space_count += 1;
105 } else {
106 break;
107 }
108 }
109
110 space_count
111 }
112}
113
114use std::collections::hash_map::DefaultHasher;
115use std::hash::{Hash, Hasher};
116
117pub fn fast_hash(content: &str) -> u64 {
130 let mut hasher = DefaultHasher::new();
131 content.hash(&mut hasher);
132 hasher.finish()
133}
134
135#[cfg(test)]
136mod tests {
137 use super::*;
138
139 #[test]
140 fn test_detect_line_ending_pure_lf() {
141 let content = "First line\nSecond line\nThird line\n";
143 assert_eq!(detect_line_ending(content), "\n");
144 }
145
146 #[test]
147 fn test_detect_line_ending_pure_crlf() {
148 let content = "First line\r\nSecond line\r\nThird line\r\n";
150 assert_eq!(detect_line_ending(content), "\r\n");
151 }
152
153 #[test]
154 fn test_detect_line_ending_mixed_more_lf() {
155 let content = "First line\nSecond line\r\nThird line\nFourth line\n";
157 assert_eq!(detect_line_ending(content), "\n");
158 }
159
160 #[test]
161 fn test_detect_line_ending_mixed_more_crlf() {
162 let content = "First line\r\nSecond line\r\nThird line\nFourth line\r\n";
164 assert_eq!(detect_line_ending(content), "\r\n");
165 }
166
167 #[test]
168 fn test_detect_line_ending_empty_string() {
169 let content = "";
171 assert_eq!(detect_line_ending(content), "\n");
172 }
173
174 #[test]
175 fn test_detect_line_ending_single_line_no_ending() {
176 let content = "This is a single line with no line ending";
178 assert_eq!(detect_line_ending(content), "\n");
179 }
180
181 #[test]
182 fn test_detect_line_ending_equal_lf_and_crlf() {
183 let content = "Line 1\r\nLine 2\nLine 3\r\nLine 4\n";
187 assert_eq!(detect_line_ending(content), "\n");
188 }
189
190 #[test]
191 fn test_detect_line_ending_single_lf() {
192 let content = "Line 1\n";
194 assert_eq!(detect_line_ending(content), "\n");
195 }
196
197 #[test]
198 fn test_detect_line_ending_single_crlf() {
199 let content = "Line 1\r\n";
201 assert_eq!(detect_line_ending(content), "\r\n");
202 }
203
204 #[test]
205 fn test_detect_line_ending_embedded_cr() {
206 let content = "Line 1\rLine 2\nLine 3\r\nLine 4\n";
209 assert_eq!(detect_line_ending(content), "\n");
211 }
212}