Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove multi-line flag from regexes #903

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions stdlib-candidate/std-rfc/str/dedent/mod.nu
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,27 @@ 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'
})
}

# 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"
Expand Down Expand Up @@ -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'
}
16 changes: 16 additions & 0 deletions stdlib-candidate/tests/str_dedent.nu
Original file line number Diff line number Diff line change
Expand Up @@ -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 {||
Expand All @@ -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 {||
Expand Down