| 1 |
use std::borrow::Cow; |
| 2 |
use std::ptr; |
| 3 |
use std::str; |
| 4 |
|
| 5 |
use crate::ctype::{ispunct, isspace, isspace_char}; |
| 6 |
use crate::entity; |
| 7 |
use crate::parser::AutolinkType; |
| 8 |
#[cfg(feature = "phoenix_heex")] |
| 9 |
use crate::scanners; |
| 10 |
|
| 11 |
#[derive(PartialEq, Eq)] |
| 12 |
pub enum Case { |
| 13 |
Preserve, |
| 14 |
Fold, |
| 15 |
} |
| 16 |
|
| 17 |
pub fn unescape(s: &mut String) { |
| 18 |
// SAFETY: we only shift over backslash characters, and always truncate any |
| 19 |
// continuation characters duplicated as a result of the shifting. |
| 20 |
let b = unsafe { s.as_bytes_mut() }; |
| 21 |
let mut r = 0; |
| 22 |
let mut prev = None; |
| 23 |
let mut found = 0; |
| 24 |
|
| 25 |
while r < b.len() { |
| 26 |
if b[r] == b'\\' && r + 1 < b.len() && ispunct(b[r + 1]) { |
| 27 |
if b[r + 1] == b'\\' { |
| 28 |
r += 1; |
| 29 |
} |
| 30 |
|
| 31 |
if let Some(prev) = prev { |
| 32 |
let window = &mut b[(prev + 1 - found)..r]; |
| 33 |
shift_buf_left(window, found); |
| 34 |
} |
| 35 |
prev = Some(r); |
| 36 |
found += 1; |
| 37 |
} |
| 38 |
r += 1; |
| 39 |
} |
| 40 |
|
| 41 |
if let Some(prev) = prev { |
| 42 |
let window = &mut b[(prev + 1 - found)..r]; |
| 43 |
shift_buf_left(window, found); |
| 44 |
} |
| 45 |
|
| 46 |
if found > 0 { |
| 47 |
let new_size = b.len() - found; |
| 48 |
// HACK: see shift_buf_left. |
| 49 |
b[new_size] = b'\0'; |
| 50 |
s.truncate(new_size); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
pub fn clean_autolink(mut url: &str, kind: AutolinkType) -> Cow<'_, str> { |
| 55 |
url = trim_slice(url); |
| 56 |
|
| 57 |
if url.is_empty() { |
| 58 |
return "".into(); |
| 59 |
} |
| 60 |
|
| 61 |
if kind == AutolinkType::Email { |
| 62 |
let mut buf = String::with_capacity(url.len() + "mailto:".len()); |
| 63 |
buf.push_str("mailto:"); |
| 64 |
buf.push_str(&entity::unescape_html(url)); |
| 65 |
buf.into() |
| 66 |
} else { |
| 67 |
entity::unescape_html(url) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
pub fn normalize_code(v: &str) -> Cow<'_, str> { |
| 72 |
let b = v.as_bytes(); |
| 73 |
let mut r = String::new(); |
| 74 |
let mut offset = 0; |
| 75 |
let mut i = 0; |
| 76 |
let mut contains_nonspace = false; |
| 77 |
|
| 78 |
while i < b.len() { |
| 79 |
match b[i] { |
| 80 |
b'\r' => { |
| 81 |
if i + 1 == v.len() || b[i + 1] != b'\n' { |
| 82 |
r.push_str(&v[offset..i]); |
| 83 |
r.push(' '); |
| 84 |
offset = i + 1; |
| 85 |
} |
| 86 |
} |
| 87 |
b'\n' => { |
| 88 |
r.push_str(&v[offset..i]); |
| 89 |
r.push(' '); |
| 90 |
offset = i + 1; |
| 91 |
} |
| 92 |
b' ' => {} |
| 93 |
_ => contains_nonspace = true, |
| 94 |
} |
| 95 |
|
| 96 |
i += 1 |
| 97 |
} |
| 98 |
|
| 99 |
if offset == 0 { |
| 100 |
if contains_nonspace && b[0] == b' ' && b[i - 1] == b' ' { |
| 101 |
return v[1..i - 1].into(); |
| 102 |
} else { |
| 103 |
return v.into(); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
r.push_str(&v[offset..i]); |
| 108 |
|
| 109 |
// SAFETY: we only shift over a space, and we are guaranteed to duplicate a |
| 110 |
// space in the last byte of the buffer before truncating. |
| 111 |
let bytes = unsafe { r.as_bytes_mut() }; |
| 112 |
let len = bytes.len(); |
| 113 |
if contains_nonspace && bytes[0] == b' ' && bytes[len - 1] == b' ' { |
| 114 |
shift_buf_left(bytes, 1); |
| 115 |
r.truncate(len - 2); |
| 116 |
} |
| 117 |
|
| 118 |
r.into() |
| 119 |
} |
| 120 |
|
| 121 |
pub fn remove_trailing_blank_lines(line: &mut String) { |
| 122 |
line.truncate(remove_trailing_blank_lines_ix(line)); |
| 123 |
} |
| 124 |
|
| 125 |
pub fn remove_trailing_blank_lines_slice(line: &str) -> &str { |
| 126 |
&line[..remove_trailing_blank_lines_ix(line)] |
| 127 |
} |
| 128 |
|
| 129 |
fn remove_trailing_blank_lines_ix(line: &str) -> usize { |
| 130 |
let line_bytes = line.as_bytes(); |
| 131 |
if line.is_empty() { |
| 132 |
return 0; |
| 133 |
} |
| 134 |
let mut i = line.len() - 1; |
| 135 |
loop { |
| 136 |
let c = line_bytes[i]; |
| 137 |
|
| 138 |
if c != b' ' && c != b'\t' && !is_line_end_char(c) { |
| 139 |
break; |
| 140 |
} |
| 141 |
|
| 142 |
if i == 0 { |
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
i -= 1; |
| 147 |
} |
| 148 |
|
| 149 |
for (i, c) in line_bytes.iter().enumerate().take(line.len()).skip(i) { |
| 150 |
if !is_line_end_char(*c) { |
| 151 |
continue; |
| 152 |
} |
| 153 |
|
| 154 |
return i; |
| 155 |
} |
| 156 |
|
| 157 |
line.len() |
| 158 |
} |
| 159 |
|
| 160 |
pub fn is_line_end_char(ch: u8) -> bool { |
| 161 |
matches!(ch, 10 | 13) |
| 162 |
} |
| 163 |
|
| 164 |
pub fn is_space_or_tab(ch: u8) -> bool { |
| 165 |
matches!(ch, 9 | 32) |
| 166 |
} |
| 167 |
|
| 168 |
/// Chop any trailing sequence of `#` characters from an ATX heading line. |
| 169 |
/// |
| 170 |
/// Returns the possibly-chopped line and a boolean indicating whether |
| 171 |
/// trailing hashes were removed (i.e. the heading had a closing sequence). |
| 172 |
pub fn chop_trailing_hashes(mut line: &str) -> (&str, bool) { |
| 173 |
line = rtrim_slice(line); |
| 174 |
|
| 175 |
let orig_n = line.len() - 1; |
| 176 |
let mut n = orig_n; |
| 177 |
|
| 178 |
let bytes = line.as_bytes(); |
| 179 |
while bytes[n] == b'#' { |
| 180 |
if n == 0 { |
| 181 |
return (line, false); |
| 182 |
} |
| 183 |
n -= 1; |
| 184 |
} |
| 185 |
|
| 186 |
if n != orig_n && is_space_or_tab(bytes[n]) { |
| 187 |
(rtrim_slice(&line[..n]), true) |
| 188 |
} else { |
| 189 |
(line, false) |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
pub fn rtrim(line: &mut String) -> usize { |
| 194 |
let spaces = line |
| 195 |
.as_bytes() |
| 196 |
.iter() |
| 197 |
.rev() |
| 198 |
.take_while(|&&b| isspace(b)) |
| 199 |
.count(); |
| 200 |
let new_len = line.len() - spaces; |
| 201 |
line.truncate(new_len); |
| 202 |
spaces |
| 203 |
} |
| 204 |
|
| 205 |
pub fn ltrim(line: &mut String) -> usize { |
| 206 |
let spaces = ltrim_count(line); |
| 207 |
remove_from_start(line, spaces); |
| 208 |
spaces |
| 209 |
} |
| 210 |
|
| 211 |
#[inline] |
| 212 |
pub fn ltrim_count(line: &str) -> usize { |
| 213 |
line.as_bytes().iter().take_while(|&&c| isspace(c)).count() |
| 214 |
} |
| 215 |
|
| 216 |
pub fn remove_from_start(s: &mut String, n: usize) { |
| 217 |
if n == 0 { |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
if !s.is_char_boundary(n) { |
| 222 |
panic!("remove_from_start results in non UTF-8 string"); |
| 223 |
} |
| 224 |
|
| 225 |
// SAFETY: we've asserted s[n] is a valid UTF-8 boundary, and we truncate |
| 226 |
// any duplicated continuation characters. |
| 227 |
let bytes = unsafe { s.as_bytes_mut() }; |
| 228 |
shift_buf_left(bytes, n); |
| 229 |
let new_len = bytes.len() - n; |
| 230 |
// HACK: see shift_buf_left. |
| 231 |
bytes[new_len] = b'\0'; |
| 232 |
s.truncate(new_len); |
| 233 |
} |
| 234 |
|
| 235 |
pub fn trim(line: &mut String) { |
| 236 |
ltrim(line); |
| 237 |
rtrim(line); |
| 238 |
} |
| 239 |
|
| 240 |
pub fn ltrim_slice(i: &str) -> &str { |
| 241 |
// Compared to upstream, this additionally trims U+000C FORM FEED. |
| 242 |
i.trim_start_matches(isspace_char) |
| 243 |
} |
| 244 |
|
| 245 |
pub fn rtrim_slice(i: &str) -> &str { |
| 246 |
i.trim_end_matches(isspace_char) |
| 247 |
} |
| 248 |
|
| 249 |
pub fn rtrim_cow(s: &mut Cow<str>) { |
| 250 |
match s { |
| 251 |
Cow::Borrowed(str) => *str = rtrim_slice(str), |
| 252 |
Cow::Owned(string) => { |
| 253 |
rtrim(string); |
| 254 |
} |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
pub fn trim_slice(i: &str) -> &str { |
| 259 |
rtrim_slice(ltrim_slice(i)) |
| 260 |
} |
| 261 |
|
| 262 |
pub fn trim_cow(s: &mut Cow<str>) { |
| 263 |
match s { |
| 264 |
Cow::Borrowed(str) => *str = trim_slice(str), |
| 265 |
Cow::Owned(string) => trim(string), |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
// HACK: Using this function safely on a buffer obtained from |
| 270 |
// String::as_bytes_mut() requires care when truncating it. |
| 271 |
// |
| 272 |
// Say the string ends in a multibyte character, i.e. the last byte is a UTF-8 |
| 273 |
// continuation byte. That byte will be repeated at the end of the buffer when |
| 274 |
// it's shifted left by one at a time; therefore the truncation point won't be |
| 275 |
// a valid UTF-8 character boundary, and String::truncate will panic. In such |
| 276 |
// cases, set the byte immediately after the retained portion to b'\0' (or any |
| 277 |
// non-continuation byte!). |
| 278 |
fn shift_buf_left(buf: &mut [u8], n: usize) { |
| 279 |
if n == 0 { |
| 280 |
return; |
| 281 |
} |
| 282 |
assert!(n <= buf.len()); |
| 283 |
let keep = buf.len() - n; |
| 284 |
// SAFETY: we can copy `keep` bytes from `dst+n` to `dst`, as the full size |
| 285 |
// of the `dst` buffer is `keep+n`. |
| 286 |
unsafe { |
| 287 |
let dst = buf.as_mut_ptr(); |
| 288 |
let src = dst.add(n); |
| 289 |
ptr::copy(src, dst, keep); |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
pub fn clean_url(url: &str) -> Cow<'static, str> { |
| 294 |
let url = trim_slice(url); |
| 295 |
|
| 296 |
if url.is_empty() { |
| 297 |
return "".into(); |
| 298 |
} |
| 299 |
|
| 300 |
let mut b = entity::unescape_html(url).into_owned(); |
| 301 |
unescape(&mut b); |
| 302 |
b.into() |
| 303 |
} |
| 304 |
|
| 305 |
pub fn clean_title(title: &str) -> Cow<'static, str> { |
| 306 |
let title_len = title.len(); |
| 307 |
if title_len == 0 { |
| 308 |
return "".into(); |
| 309 |
} |
| 310 |
|
| 311 |
let bytes = title.as_bytes(); |
| 312 |
let first = bytes[0]; |
| 313 |
let last = bytes[title_len - 1]; |
| 314 |
|
| 315 |
let mut b = if (first == b'\'' && last == b'\'') |
| 316 |
|| (first == b'(' && last == b')') |
| 317 |
|| (first == b'"' && last == b'"') |
| 318 |
{ |
| 319 |
entity::unescape_html(&title[1..title_len - 1]) |
| 320 |
} else { |
| 321 |
entity::unescape_html(title) |
| 322 |
} |
| 323 |
.into_owned(); |
| 324 |
|
| 325 |
unescape(&mut b); |
| 326 |
b.into() |
| 327 |
} |
| 328 |
|
| 329 |
pub fn is_blank(s: &str) -> bool { |
| 330 |
for c in s.as_bytes() { |
| 331 |
match c { |
| 332 |
10 | 13 => return true, |
| 333 |
32 | 9 => (), |
| 334 |
_ => return false, |
| 335 |
} |
| 336 |
} |
| 337 |
true |
| 338 |
} |
| 339 |
|
| 340 |
pub fn normalize_label(i: &str, casing: Case) -> String { |
| 341 |
let i = trim_slice(i); |
| 342 |
|
| 343 |
let mut v = String::with_capacity(i.len()); |
| 344 |
let mut last_was_whitespace = false; |
| 345 |
for c in i.chars() { |
| 346 |
if c.is_whitespace() { |
| 347 |
if !last_was_whitespace { |
| 348 |
last_was_whitespace = true; |
| 349 |
v.push(' '); |
| 350 |
} |
| 351 |
} else { |
| 352 |
last_was_whitespace = false; |
| 353 |
v.push(c); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
if casing == Case::Fold { |
| 358 |
caseless::default_case_fold_str(&v) |
| 359 |
} else { |
| 360 |
v |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
#[test] |
| 365 |
fn normalize_label_fold_test() { |
| 366 |
assert_eq!(normalize_label("Abc \t\ndef", Case::Preserve), "Abc def"); |
| 367 |
assert_eq!(normalize_label("Abc \t\ndef", Case::Fold), "abc def"); |
| 368 |
assert_eq!(normalize_label("Straẞe", Case::Preserve), "Straẞe"); |
| 369 |
assert_eq!(normalize_label("Straẞe", Case::Fold), "strasse"); |
| 370 |
} |
| 371 |
|
| 372 |
pub fn split_off_front_matter<'s>(mut s: &'s str, delimiter: &str) -> Option<(&'s str, &'s str)> { |
| 373 |
s = trim_start_match(s, "\u{feff}"); |
| 374 |
|
| 375 |
if !s.starts_with(delimiter) { |
| 376 |
return None; |
| 377 |
} |
| 378 |
let mut start = delimiter.len(); |
| 379 |
if s[start..].starts_with('\n') { |
| 380 |
start += 1; |
| 381 |
} else if s[start..].starts_with("\r\n") { |
| 382 |
start += 2; |
| 383 |
} else { |
| 384 |
return None; |
| 385 |
} |
| 386 |
|
| 387 |
start += match s[start..] |
| 388 |
.find(&("\n".to_string() + delimiter + "\r\n")) |
| 389 |
.or_else(|| s[start..].find(&("\n".to_string() + delimiter + "\n"))) |
| 390 |
.or_else(|| s[start..].find(&("\n".to_string() + delimiter))) // delimiter followed by EOF |
| 391 |
{ |
| 392 |
Some(n) => n + 1 + delimiter.len(), |
| 393 |
None => return None, |
| 394 |
}; |
| 395 |
|
| 396 |
if start == s.len() { |
| 397 |
return Some((s, "")); |
| 398 |
} |
| 399 |
|
| 400 |
start += if s[start..].starts_with('\n') { |
| 401 |
1 |
| 402 |
} else if s[start..].starts_with("\r\n") { |
| 403 |
2 |
| 404 |
} else { |
| 405 |
return None; |
| 406 |
}; |
| 407 |
|
| 408 |
start += if s[start..].starts_with('\n') { |
| 409 |
1 |
| 410 |
} else if s[start..].starts_with("\r\n") { |
| 411 |
2 |
| 412 |
} else { |
| 413 |
0 |
| 414 |
}; |
| 415 |
|
| 416 |
Some((&s[..start], &s[start..])) |
| 417 |
} |
| 418 |
|
| 419 |
pub fn trim_start_match<'s>(s: &'s str, pat: &str) -> &'s str { |
| 420 |
s.strip_prefix(pat).unwrap_or(s) |
| 421 |
} |
| 422 |
|
| 423 |
pub fn count_newlines(input: &str) -> (usize, usize) { |
| 424 |
let bytes = input.as_bytes(); |
| 425 |
let mut num_lines = 0; |
| 426 |
let mut last_line_start = 0; |
| 427 |
let mut i = 0; |
| 428 |
while i < input.len() { |
| 429 |
match bytes[i] { |
| 430 |
b'\r' if i + 1 < input.len() && bytes[i + 1] == b'\n' => { |
| 431 |
i += 1; |
| 432 |
num_lines += 1; |
| 433 |
last_line_start = i + 1; |
| 434 |
} |
| 435 |
b'\r' | b'\n' => { |
| 436 |
num_lines += 1; |
| 437 |
last_line_start = i + 1; |
| 438 |
} |
| 439 |
_ => {} |
| 440 |
} |
| 441 |
i += 1; |
| 442 |
} |
| 443 |
let last_line_len = input.len() - last_line_start; |
| 444 |
(num_lines, last_line_len) |
| 445 |
} |
| 446 |
|
| 447 |
pub fn newlines_of(s: &str) -> usize { |
| 448 |
if s.ends_with("\r\n") { |
| 449 |
2 |
| 450 |
} else if s.ends_with('\r') || s.ends_with('\n') { |
| 451 |
1 |
| 452 |
} else { |
| 453 |
0 |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
#[cfg(feature = "phoenix_heex")] |
| 458 |
pub fn skip_quoted_string(s: &str, mut cursor: usize, quote_byte: u8) -> usize { |
| 459 |
let bytes = s.as_bytes(); |
| 460 |
let len = s.len(); |
| 461 |
let mut escaped = false; |
| 462 |
|
| 463 |
while cursor < len { |
| 464 |
let byte = bytes[cursor]; |
| 465 |
if escaped { |
| 466 |
escaped = false; |
| 467 |
} else if byte == b'\\' { |
| 468 |
escaped = true; |
| 469 |
} else if byte == quote_byte { |
| 470 |
return cursor + 1; |
| 471 |
} |
| 472 |
cursor += 1; |
| 473 |
} |
| 474 |
|
| 475 |
cursor |
| 476 |
} |
| 477 |
|
| 478 |
#[cfg(feature = "phoenix_heex")] |
| 479 |
pub fn find_matching_brace(s: &str, start: usize) -> Option<usize> { |
| 480 |
let bytes = s.as_bytes(); |
| 481 |
let mut cursor = start; |
| 482 |
let mut depth = 1; |
| 483 |
let len = s.len(); |
| 484 |
|
| 485 |
while cursor < len { |
| 486 |
match bytes[cursor] { |
| 487 |
b'"' | b'\'' => { |
| 488 |
cursor = skip_quoted_string(s, cursor + 1, bytes[cursor]); |
| 489 |
} |
| 490 |
b'{' => { |
| 491 |
depth += 1; |
| 492 |
cursor += 1; |
| 493 |
} |
| 494 |
b'}' => { |
| 495 |
depth -= 1; |
| 496 |
if depth == 0 { |
| 497 |
return Some(cursor + 1); |
| 498 |
} |
| 499 |
cursor += 1; |
| 500 |
} |
| 501 |
_ => cursor += 1, |
| 502 |
} |
| 503 |
} |
| 504 |
None |
| 505 |
} |
| 506 |
|
| 507 |
#[cfg(feature = "phoenix_heex")] |
| 508 |
pub fn phoenix_inline_tag(s: &str) -> Option<usize> { |
| 509 |
let tag_name_len = scanners::phoenix_opening_tag(s)?; |
| 510 |
let bytes = s.as_bytes(); |
| 511 |
let mut cursor = 1 + tag_name_len; |
| 512 |
let len = s.len(); |
| 513 |
|
| 514 |
while cursor < len { |
| 515 |
match bytes[cursor] { |
| 516 |
b'>' => return Some(cursor + 1), |
| 517 |
b'/' if cursor + 1 < len && bytes[cursor + 1] == b'>' => return Some(cursor + 2), |
| 518 |
b'"' | b'\'' => { |
| 519 |
cursor = skip_quoted_string(s, cursor + 1, bytes[cursor]); |
| 520 |
} |
| 521 |
b'{' => { |
| 522 |
cursor = find_matching_brace(s, cursor + 1)?; |
| 523 |
} |
| 524 |
_ => cursor += 1, |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
None |
| 529 |
} |
| 530 |
|
| 531 |
#[cfg(feature = "phoenix_heex")] |
| 532 |
pub fn phoenix_inline_expression(s: &str) -> Option<usize> { |
| 533 |
if !s.starts_with('{') { |
| 534 |
return None; |
| 535 |
} |
| 536 |
find_matching_brace(s, 1) |
| 537 |
} |
| 538 |
|
| 539 |
#[cfg(test)] |
| 540 |
pub mod tests { |
| 541 |
use super::{ |
| 542 |
count_newlines, ltrim, normalize_code, normalize_label, shift_buf_left, |
| 543 |
split_off_front_matter, |
| 544 |
}; |
| 545 |
use crate::strings::Case; |
| 546 |
|
| 547 |
#[test] |
| 548 |
fn normalize_code_handles_lone_newline() { |
| 549 |
assert_eq!(normalize_code("\n"), " "); |
| 550 |
} |
| 551 |
|
| 552 |
#[test] |
| 553 |
fn normalize_code_handles_lone_space() { |
| 554 |
assert_eq!(normalize_code(" "), " "); |
| 555 |
} |
| 556 |
|
| 557 |
#[test] |
| 558 |
fn front_matter() { |
| 559 |
assert_eq!( |
| 560 |
split_off_front_matter("---\nfoo: bar\n---\nHiiii", "---"), |
| 561 |
Some(("---\nfoo: bar\n---\n", "Hiiii")) |
| 562 |
); |
| 563 |
assert_eq!( |
| 564 |
split_off_front_matter( |
| 565 |
"\u{feff}!@#\r\n\r\nfoo: !@# \r\nquux\n!@#\r\n\n\nYes!\n", |
| 566 |
"!@#" |
| 567 |
), |
| 568 |
Some(("!@#\r\n\r\nfoo: !@# \r\nquux\n!@#\r\n\n", "\nYes!\n")) |
| 569 |
); |
| 570 |
assert_eq!( |
| 571 |
split_off_front_matter( |
| 572 |
"\u{feff}!@#\r\n\r\nfoo: \n!@# \r\nquux\n!@#\r\n\n\nYes!\n", |
| 573 |
"!@#" |
| 574 |
), |
| 575 |
Some(("!@#\r\n\r\nfoo: \n!@# \r\nquux\n!@#\r\n\n", "\nYes!\n")) |
| 576 |
); |
| 577 |
} |
| 578 |
|
| 579 |
#[test] |
| 580 |
fn normalize_label_lowercase() { |
| 581 |
assert_eq!(normalize_label(" Foo\u{A0}BAR ", Case::Fold), "foo bar"); |
| 582 |
assert_eq!(normalize_label(" FooİBAR ", Case::Fold), "fooi\u{307}bar"); |
| 583 |
} |
| 584 |
|
| 585 |
#[test] |
| 586 |
fn normalize_label_preserve() { |
| 587 |
assert_eq!( |
| 588 |
normalize_label(" Foo\u{A0}BAR ", Case::Preserve), |
| 589 |
"Foo BAR" |
| 590 |
); |
| 591 |
assert_eq!(normalize_label(" FooİBAR ", Case::Preserve), "FooİBAR"); |
| 592 |
} |
| 593 |
|
| 594 |
#[test] |
| 595 |
fn shift_buf_left_ok() { |
| 596 |
let mut b: Vec<u8>; |
| 597 |
|
| 598 |
b = vec![1, 2, 3, 4, 5, 6]; |
| 599 |
shift_buf_left(&mut b, 1); |
| 600 |
assert_eq!(b, vec![2, 3, 4, 5, 6, 6]); |
| 601 |
|
| 602 |
shift_buf_left(&mut b, 2); |
| 603 |
assert_eq!(b, vec![4, 5, 6, 6, 6, 6]); |
| 604 |
} |
| 605 |
|
| 606 |
#[test] |
| 607 |
fn ltrim_ok() { |
| 608 |
let mut s: String; |
| 609 |
|
| 610 |
s = "okay".to_string(); |
| 611 |
ltrim(&mut s); |
| 612 |
assert_eq!(s, "okay"); |
| 613 |
|
| 614 |
s = " okay".to_string(); |
| 615 |
ltrim(&mut s); |
| 616 |
assert_eq!(s, "okay"); |
| 617 |
} |
| 618 |
|
| 619 |
#[test] |
| 620 |
fn count_newlines_ok() { |
| 621 |
assert_eq!((0, 7), count_newlines("abcdefg")); |
| 622 |
assert_eq!((2, 0), count_newlines("abc\ndefg\n")); |
| 623 |
assert_eq!((3, 2), count_newlines("abc\rde\nfg\nhi")); |
| 624 |
} |
| 625 |
} |