1use super::*;
2
3#[inline]
13pub fn normalize_line_endings(input: &str) -> String {
14 let bytes: &[u8] = input.as_bytes();
15 let Some(first_cr) = bytes.iter().position(|&b: &u8| b == b'\r') else {
16 return input.to_string();
17 };
18 let mut out: String = String::with_capacity(input.len());
19 out.push_str(&input[..first_cr]);
20 let mut idx: usize = first_cr;
21 let len: usize = bytes.len();
22 while idx < len {
23 if bytes[idx] == b'\r' {
24 out.push(TEXT_LF);
25 if idx + 1 < len && bytes[idx + 1] == b'\n' {
26 idx += 2;
27 } else {
28 idx += 1;
29 }
30 let start: usize = idx;
31 while idx < len && bytes[idx] != b'\r' {
32 idx += 1;
33 }
34 if idx > start {
35 out.push_str(&input[start..idx]);
36 }
37 } else {
38 idx += 1;
39 }
40 }
41 out
42}
43
44pub fn format_plain_text(input: &str, preserve_tabs: bool) -> String {
55 if input.is_empty() {
56 return String::new();
57 }
58 let bytes: &[u8] = input.as_bytes();
59 let len: usize = bytes.len();
60 let mut out: String = String::with_capacity(len + 1);
61 let mut last_non_empty_end: usize = 0;
62 let mut start: usize = 0;
63 let mut idx: usize = 0;
64 let mut line_idx: usize = 0;
65 loop {
66 while idx < len && bytes[idx] != b'\n' && bytes[idx] != b'\r' {
67 idx += 1;
68 }
69 let raw_line: &str = &input[start..idx];
70 let trimmed: &str = raw_line.trim_end_matches([TEXT_SPACE, TEXT_TAB]);
71 if line_idx > 0 {
72 out.push(TEXT_LF);
73 }
74 if !trimmed.is_empty() {
75 if preserve_tabs || !trimmed.starts_with(TEXT_TAB) {
76 out.push_str(trimmed);
77 } else {
78 let tab_count: usize = trimmed
79 .as_bytes()
80 .iter()
81 .take_while(|&&b: &&u8| b == b'\t')
82 .count();
83 push_spaces(&mut out, tab_count * PRETTIER_TAB_WIDTH);
84 out.push_str(&trimmed[tab_count..]);
85 }
86 last_non_empty_end = out.len();
87 }
88 if idx >= len {
89 break;
90 }
91 if bytes[idx] == b'\r' && idx + 1 < len && bytes[idx + 1] == b'\n' {
92 idx += 2;
93 } else {
94 idx += 1;
95 }
96 start = idx;
97 line_idx += 1;
98 }
99 if last_non_empty_end == 0 {
100 return String::new();
101 }
102 out.truncate(last_non_empty_end);
103 out.push(TEXT_LF);
104 out
105}
106
107#[inline(always)]
117pub fn char_display_width(ch: char) -> usize {
118 let code: u32 = ch as u32;
119 if (0x20..0x7F).contains(&code) {
120 return 1;
121 }
122 if code < 0x20 || (0x7F..0xA0).contains(&code) {
123 return 0;
124 }
125 if (0x0300..=0x036F).contains(&code)
126 || (0x1AB0..=0x1AFF).contains(&code)
127 || (0x1DC0..=0x1DFF).contains(&code)
128 || (0x20D0..=0x20FF).contains(&code)
129 || (0xFE00..=0xFE0F).contains(&code)
130 || (0xFE20..=0xFE2F).contains(&code)
131 || code == 0x200D
132 {
133 return 0;
134 }
135 if is_wide_codepoint(code) {
136 return 2;
137 }
138 1
139}
140
141#[inline]
151pub fn str_display_width(text: &str) -> usize {
152 let bytes: &[u8] = text.as_bytes();
153 if text.is_ascii() {
154 let mut width: usize = 0;
155 for &b in bytes {
156 width += usize::from((0x20..0x7F).contains(&b));
157 }
158 return width;
159 }
160 let len: usize = bytes.len();
161 let mut width: usize = 0;
162 let mut idx: usize = 0;
163 while idx < len {
164 let b: u8 = bytes[idx];
165 if b < 0x80 {
166 width += usize::from((0x20..0x7F).contains(&b));
167 idx += 1;
168 continue;
169 }
170 let rest: &str = &text[idx..];
171 if let Some(ch) = rest.chars().next() {
172 width += char_display_width(ch);
173 idx += ch.len_utf8();
174 } else {
175 break;
176 }
177 }
178 width
179}
180
181#[inline(always)]
191fn is_wide_codepoint(code: u32) -> bool {
192 (0x1100..=0x115F).contains(&code)
193 || (0x2329..=0x232A).contains(&code)
194 || (0x2E80..=0x303E).contains(&code)
195 || (0x3040..=0xA4CF).contains(&code)
196 || (0xAC00..=0xD7A3).contains(&code)
197 || (0xF900..=0xFAFF).contains(&code)
198 || (0xFE10..=0xFE19).contains(&code)
199 || (0xFE30..=0xFE6F).contains(&code)
200 || (0xFF00..=0xFF60).contains(&code)
201 || (0xFFE0..=0xFFE6).contains(&code)
202 || (0x1F300..=0x1FAFF).contains(&code)
203 || (0x20000..=0x2FFFD).contains(&code)
204 || (0x30000..=0x3FFFD).contains(&code)
205}
206
207#[inline(always)]
214pub(crate) fn push_spaces(out: &mut String, count: usize) {
215 let mut remaining: usize = count;
216 while remaining >= TEXT_SPACES_CHUNK.len() {
217 out.push_str(TEXT_SPACES_CHUNK);
218 remaining -= TEXT_SPACES_CHUNK.len();
219 }
220 if remaining > 0 {
221 out.push_str(&TEXT_SPACES_CHUNK[..remaining]);
222 }
223}