Fix str.trim(regex,at:end) when the whole string is matched (#3730)

This commit is contained in:
Leedehai 2024-04-01 17:01:26 -04:00 committed by Laurenz
parent b4885930d3
commit 8529ffe701
2 changed files with 31 additions and 11 deletions

View File

@ -491,11 +491,11 @@ impl Str {
#[func] #[func]
pub fn trim( pub fn trim(
&self, &self,
/// The pattern to search for. /// The pattern to search for. If `{none}`, trims white spaces.
#[default] #[default]
pattern: Option<StrPattern>, pattern: Option<StrPattern>,
/// Can be `start` or `end` to only trim the start or end of the string. /// Can be `{start}` or `{end}` to only trim the start or end of the
/// If omitted, both sides are trimmed. /// string. If omitted, both sides are trimmed.
#[named] #[named]
at: Option<StrSide>, at: Option<StrSide>,
/// Whether to repeatedly removes matches of the pattern or just once. /// Whether to repeatedly removes matches of the pattern or just once.
@ -535,16 +535,16 @@ impl Str {
} }
Some(StrPattern::Regex(re)) => { Some(StrPattern::Regex(re)) => {
let s = self.as_str(); let s = self.as_str();
let mut last = 0; let mut last = None;
let mut range = 0..s.len(); let mut range = 0..s.len();
for m in re.find_iter(s) { for m in re.find_iter(s) {
// Does this match follow directly after the last one? // Does this match follow directly after the last one?
let consecutive = last == m.start(); let consecutive = last == Some(m.start());
// As long as we're consecutive and still trimming at the // As long as we're at the beginning or in a consecutive run
// start, trim. // of matches, and we're still trimming at the start, trim.
start &= consecutive; start &= m.start() == 0 || consecutive;
if start { if start {
range.start = m.end(); range.start = m.end();
start &= repeat; start &= repeat;
@ -556,11 +556,11 @@ impl Str {
range.end = m.start(); range.end = m.start();
} }
last = m.end(); last = Some(m.end());
} }
// Is the last match directly at the end? // Is the last match directly at the end?
if last < s.len() { if last.is_some_and(|last| last < s.len()) {
range.end = s.len(); range.end = s.len();
} }

View File

@ -180,17 +180,31 @@
#"123".replace("123", (1, 2, 3)) #"123".replace("123", (1, 2, 3))
--- ---
// Test the `trim` method. // Test the `trim` method; the pattern is not provided.
#let str = "Typst, LaTeX, Word, InDesign" #let str = "Typst, LaTeX, Word, InDesign"
#let array = ("Typst", "LaTeX", "Word", "InDesign") #let array = ("Typst", "LaTeX", "Word", "InDesign")
#test(str.split(",").map(s => s.trim()), array) #test(str.split(",").map(s => s.trim()), array)
#test("".trim(), "") #test("".trim(), "")
#test(" ".trim(), "")
#test("\t".trim(), "")
#test("\n".trim(), "")
#test("\t \n".trim(), "")
#test(" abc ".trim(at: start), "abc ") #test(" abc ".trim(at: start), "abc ")
#test("\tabc ".trim(at: start), "abc ")
#test("abc\n".trim(at: end), "abc")
#test(" abc ".trim(at: end, repeat: true), " abc") #test(" abc ".trim(at: end, repeat: true), " abc")
#test(" abc".trim(at: start, repeat: false), "abc") #test(" abc".trim(at: start, repeat: false), "abc")
---
// Test the `trim` method; the pattern is a string.
#test("aabcaa".trim("a", repeat: false), "abca") #test("aabcaa".trim("a", repeat: false), "abca")
#test("aabca".trim("a", at: start), "bca") #test("aabca".trim("a", at: start), "bca")
#test("aabcaa".trim("a", at: end, repeat: false), "aabca") #test("aabcaa".trim("a", at: end, repeat: false), "aabca")
#test(" abc\n".trim("\n"), " abc")
#test("whole".trim("whole", at: start), "")
---
// Test the `trim` method; the pattern is a regex.
#test("".trim(regex(".")), "") #test("".trim(regex(".")), "")
#test("123abc456".trim(regex("\d")), "abc") #test("123abc456".trim(regex("\d")), "abc")
#test("123abc456".trim(regex("\d"), repeat: false), "23abc45") #test("123abc456".trim(regex("\d"), repeat: false), "23abc45")
@ -201,6 +215,12 @@
#test("123abc456".trim(regex("\d+"), at: end, repeat: false), "123abc") #test("123abc456".trim(regex("\d+"), at: end, repeat: false), "123abc")
#test("123abc456".trim(regex("\d{1,2}$"), repeat: false), "123abc4") #test("123abc456".trim(regex("\d{1,2}$"), repeat: false), "123abc4")
#test("hello world".trim(regex(".")), "") #test("hello world".trim(regex(".")), "")
#test("12306".trim(regex("\d"), at: start), "")
#test("12306abc".trim(regex("\d"), at: start), "abc")
#test("whole".trim(regex("whole"), at: start), "")
#test("12306".trim(regex("\d"), at: end), "")
#test("abc12306".trim(regex("\d"), at: end), "abc")
#test("whole".trim(regex("whole"), at: end), "")
--- ---
// Error: 17-21 expected either `start` or `end` // Error: 17-21 expected either `start` or `end`