Skip to content

Make indent preserve existing newlines in the input string#279

Merged
mgeisler merged 5 commits intomgeisler:masterfrom
jRimbault:fix/indent-custom-iter
Jan 21, 2021
Merged

Make indent preserve existing newlines in the input string#279
mgeisler merged 5 commits intomgeisler:masterfrom
jRimbault:fix/indent-custom-iter

Conversation

@jRimbault
Copy link
Copy Markdown
Contributor

@jRimbault jRimbault commented Jan 6, 2021

I ported a custom iterator to get the inclusive split feature specifically with a char (though it could be even be just a custom iterator with \n as a const value).

This fixes #207 on stable rust but I'd feel safer once we're able to use the much more generic split_inclusive, hopefully.

It uses unsafe code. Which is forbidden in this crate. It's up to you @mgeisler if you want to make an exception or not.

Before, indent("foo", "") would give "foo\n".
It now preserves any trailing newline character present in the
input string. This makes indent behave consistently with dedent.
New tests ere added to ensure this on a number of corner cases.

closes mgeisler#207
I guess this causes bound checking, but I think I prefer that.
@mgeisler
Copy link
Copy Markdown
Owner

It uses unsafe code. Which is forbidden in this crate. It's up to you @mgeisler if you want to make an exception or not.

Hey @jRimbault, thanks for removing the unsafe code again! Until someone shows up with a performance problem (with benchmarks...), there's no need for unsafe code here 😄

Comment thread .github/workflows/build.yml Outdated
Comment thread src/indentation.rs Outdated
Comment thread tests/indent.rs
Comment thread tests/indent.rs
Comment thread tests/indent.rs
@mgeisler mgeisler self-requested a review January 16, 2021 13:36
@jRimbault
Copy link
Copy Markdown
Contributor Author

I'll get on to it soon :)

@mgeisler
Copy link
Copy Markdown
Owner

Hi @jRimbault, no stress! That's the beauty of open source 😄

@mgeisler
Copy link
Copy Markdown
Owner

... and thanks!

@jRimbault
Copy link
Copy Markdown
Contributor Author

jRimbault commented Jan 20, 2021

I tried making some fuzz work but I don't understand quite what it's testing for ? Panics ? The examples here don't seem to actually test things ?

In any case I think your solution to simply add an enumerating index is really quite good since it doesn't need bumping the MSRV (split_inclusive will be part of 1.51). I also think, rather than fuzzing, taking tests from tried and proved libraries like, this crate inspiration, python's textwrap should be a better faster option (CPU time, and reliability wise).

@mgeisler
Copy link
Copy Markdown
Owner

I tried making some fuzz work but I don't understand quite what it's testing for ? Panics ? The examples here don't seem to actually test things ?

Yes, you raise a good point and you guessed correctly: they simply test that the code doesn't panic 😄 I should really document this...

In any case I think your solution to simply add an enumerating index is really quite good since it doesn't need bumping the MSRV (split_inclusive will be part of 1.51). I also think, rather than fuzzing, taking tests from tried and proved libraries like, this crate inspiration, python's textwrap should be a better faster option (CPU time, and reliability wise).

Fair enough, I like it a lot that you ported those tests. They give us a nice foundation and we can then always think about turning them into property-based tests later. You're definitely right that it's much cheaper to have some solid unit tests for this.

Comment thread tests/indent.rs
@jRimbault
Copy link
Copy Markdown
Contributor Author

Happy to help 😄

@mgeisler mgeisler changed the title Make indent preserve the of \n in the input string (stop adding a trailing \n) [stable rustc] Make indent preserve existing newlines in the input string Jan 30, 2021
@github-actions github-actions Bot mentioned this pull request Feb 20, 2021
ma2bd added a commit to ma2bd/libra that referenced this pull request Feb 22, 2021
bors-libra pushed a commit to diem/diem that referenced this pull request Feb 22, 2021
zgfzgf pushed a commit to zgfzgf/diem that referenced this pull request Jun 8, 2021
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

Successfully merging this pull request may close these issues.

The indent function always adds a final \n, even if the input string has no \n

2 participants