From 8bf899c6c2162b151dfa359b3e3d4c920f814569 Mon Sep 17 00:00:00 2001 From: NotTheDr01ds <32344964+NotTheDr01ds@users.noreply.github.com> Date: Fri, 19 Jul 2024 17:15:58 -0400 Subject: [PATCH] Remove multi-line flag from regexes (#903) Minor fix on `str dedent` - I erroneously used the regex multi-line flag `(?m)` in several patterns where it shouldn't have been. For the most part, this was harmless, but it did surface an error when I was working on updating the themes. Fixed these and added two new tests that would have caught the issue. --- stdlib-candidate/std-rfc/str/dedent/mod.nu | 10 +++++----- stdlib-candidate/tests/str_dedent.nu | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/stdlib-candidate/std-rfc/str/dedent/mod.nu b/stdlib-candidate/std-rfc/str/dedent/mod.nu index fabc50f..69cba44 100644 --- a/stdlib-candidate/std-rfc/str/dedent/mod.nu +++ b/stdlib-candidate/std-rfc/str/dedent/mod.nu @@ -35,13 +35,13 @@ export def main []: string -> string { } } - if ($string !~ '(?ms)^\s*\n') { + if ($string !~ '^\s*\n') { return (error make { msg: 'First line must be empty' }) } - if ($string !~ '(?ms)\n\s*$') { + if ($string !~ '\n\s*$') { return (error make { msg: 'Last line must contain only whitespace indicating the dedent' }) @@ -49,13 +49,13 @@ export def main []: string -> string { # Get number of spaces on the last line let indent = $string - | str replace -r '(?ms).*\n( *)$' '$1' + | str replace -r '(?s).*\n( *)$' '$1' | str length # Skip the first and last lines let lines = ( $string - | str replace -r '(?ms)^[^\n]*\n(.*)\n[^\n]*$' '$1' + | str replace -r '(?s)^[^\n]*\n(.*)\n[^\n]*$' '$1' # Use `split` instead of `lines`, since `lines` will # drop legitimate trailing empty lines | split row "\n" @@ -89,5 +89,5 @@ export def main []: string -> string { | to text # Remove the trailing newline which indicated # indent level - | str replace -r '(?ms)(.*)\n$' '$1' + | str replace -r '(?s)(.*)\n$' '$1' } \ No newline at end of file diff --git a/stdlib-candidate/tests/str_dedent.nu b/stdlib-candidate/tests/str_dedent.nu index 0f6c8ef..7fedd34 100644 --- a/stdlib-candidate/tests/str_dedent.nu +++ b/stdlib-candidate/tests/str_dedent.nu @@ -80,6 +80,13 @@ export def "test str dedent" [] { $s | str dedent } + # Test 6.1: + # Error - Does not contain an empty first line + assert error {|| + let s = "Error\n \nTesting\n" + $s | str dedent + } + # Test 7: # Error - Does not contain an empty last line assert error {|| @@ -88,6 +95,15 @@ export def "test str dedent" [] { $s | str dedent } + # Test 7.1: + # Error - Does not contain an empty last line + assert error {|| + let s = " + + Error" + $s | str dedent + } + # Test 8: # Error - Line 1 does not have enough indentation assert error {||