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

strutils.splitLines with keepEol=true splits too much #12335

Open
SoftwareApe opened this issue Oct 2, 2019 · 1 comment
Open

strutils.splitLines with keepEol=true splits too much #12335

SoftwareApe opened this issue Oct 2, 2019 · 1 comment

Comments

@SoftwareApe
Copy link

SoftwareApe commented Oct 2, 2019

When using strutils.splitLines with the keepEol option I would expect that since the EOL is part of the string output, that we wouldn't have another empty line after the last EOL.

Example

    test "keepEol splits too much":
        let expected =
            [
                @[],                     # Fails, actual result @[""]
                @["Some"],
                @["Some\n"],             # Fails, actual result @["Some\n", ""]
                @["Some\n", "String"],
                @["Some\n", "String\n"], # Fails, actual result @["Some\n", "String\n", ""]
            ]
        let testStrings =
            [
                "",
                "Some",
                "Some\n",
                "Some\nString",
                "Some\nString\n",
            ]
        for (e, ts) in zip(expected, testStrings):
            check(e == strutils.splitLines(ts, keepEol=true))

Current Output

See above

Expected Output

See above

Reasoning:

  1. Now the EOL is counted as a split and still in the string. So in a way it's counted twice.
  2. Python 3.6.6 behaves more as expected:

"a\nb\nc".splitlines(True)
['a\n', 'b\n', 'c']
"a\nb\nc\n".splitlines(True)
['a\n', 'b\n', 'c\n']

Possible Solution

Additional Information

  • Your Nim version (output of nim -v).
    1.0.0
  • Was it working in the previous Nim releases?
    Not sure
  • A link to a related issue or discussion.
    Didn't find one.
  • A project reference where (and how) the issue causes problems.
    None available.
@ringabout
Copy link
Member

I don't know whether it should be fixed, just record some behaviours in other languages:

Python:

"a\nb\nc\n".splitlines(True)

Output: ['a\n', 'b\n', 'c\n']

Dlang:

string s = "a\nb\nc\n";
writeln(splitLines(s, KeepTerminator.yes));

Output: ["a\n", "b\n", "c\n"]

Rustlang:

let result: Vec<_> = "a\nb\nc\n".lines().collect();
println!("{:?}", result);

Output: ["a", "b", "c"]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants