whitespace.rs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: Apache-2.0 OR MIT
  2. pub(crate) fn skip(mut s: &str) -> &str {
  3. 'skip: while !s.is_empty() {
  4. let byte = s.as_bytes()[0];
  5. if byte == b'/' {
  6. if s.starts_with("//")
  7. && (!s.starts_with("///") || s.starts_with("////"))
  8. && !s.starts_with("//!")
  9. {
  10. if let Some(i) = s.find('\n') {
  11. s = &s[i + 1..];
  12. continue;
  13. } else {
  14. return "";
  15. }
  16. } else if s.starts_with("/**/") {
  17. s = &s[4..];
  18. continue;
  19. } else if s.starts_with("/*")
  20. && (!s.starts_with("/**") || s.starts_with("/***"))
  21. && !s.starts_with("/*!")
  22. {
  23. let mut depth = 0;
  24. let bytes = s.as_bytes();
  25. let mut i = 0;
  26. let upper = bytes.len() - 1;
  27. while i < upper {
  28. if bytes[i] == b'/' && bytes[i + 1] == b'*' {
  29. depth += 1;
  30. i += 1; // eat '*'
  31. } else if bytes[i] == b'*' && bytes[i + 1] == b'/' {
  32. depth -= 1;
  33. if depth == 0 {
  34. s = &s[i + 2..];
  35. continue 'skip;
  36. }
  37. i += 1; // eat '/'
  38. }
  39. i += 1;
  40. }
  41. return s;
  42. }
  43. }
  44. match byte {
  45. b' ' | 0x09..=0x0D => {
  46. s = &s[1..];
  47. continue;
  48. }
  49. b if b <= 0x7F => {}
  50. _ => {
  51. let ch = s.chars().next().unwrap();
  52. if is_whitespace(ch) {
  53. s = &s[ch.len_utf8()..];
  54. continue;
  55. }
  56. }
  57. }
  58. return s;
  59. }
  60. s
  61. }
  62. fn is_whitespace(ch: char) -> bool {
  63. // Rust treats left-to-right mark and right-to-left mark as whitespace
  64. ch.is_whitespace() || ch == '\u{200e}' || ch == '\u{200f}'
  65. }