Strip trailing PHP visibility/modifier keywords from a string. Given a string like `" /** ... */\n public static"`, returns `" /** ... */"` (after stripping `static` and `public`). Recognised modifiers: `public`, `protected`, `private`, `static`, `abstract`, `final`, `readonly`.
(s: &str)
| 490 | /// Recognised modifiers: `public`, `protected`, `private`, `static`, |
| 491 | /// `abstract`, `final`, `readonly`. |
| 492 | pub(crate) fn strip_trailing_modifiers(s: &str) -> &str { |
| 493 | const MODIFIERS: &[&str] = &[ |
| 494 | "public", |
| 495 | "protected", |
| 496 | "private", |
| 497 | "static", |
| 498 | "abstract", |
| 499 | "final", |
| 500 | "readonly", |
| 501 | ]; |
| 502 | |
| 503 | let mut result = s; |
| 504 | loop { |
| 505 | let trimmed = result.trim_end(); |
| 506 | let mut found = false; |
| 507 | for &kw in MODIFIERS { |
| 508 | if let Some(prefix) = trimmed.strip_suffix(kw) { |
| 509 | // Make sure the keyword isn't part of a larger identifier. |
| 510 | if prefix.is_empty() |
| 511 | || prefix |
| 512 | .as_bytes() |
| 513 | .last() |
| 514 | .is_some_and(|&b| !b.is_ascii_alphanumeric() && b != b'_') |
| 515 | { |
| 516 | result = prefix; |
| 517 | found = true; |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | if !found { |
| 523 | break; |
| 524 | } |
| 525 | } |
| 526 | result.trim_end() |
| 527 | } |
| 528 | |
| 529 | /// Find the first `;` in `s` that is not nested inside `()`, `[]`, |
| 530 | /// `{}`, or string literals. |
no test coverage detected