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

stringx indent and dedent functions add an extra "\n" at the end of the string #446

Open
Aire-One opened this issue Oct 23, 2022 · 3 comments
Milestone

Comments

@Aire-One
Copy link

Hello,

The indent and dedent functions both add a final "\n" sequence at the end of the string.

Code reference of the indent function:

Penlight/lua/pl/stringx.lua

Lines 500 to 514 in b101290

--- indent a multiline string.
-- @tparam string s the (multiline) string
-- @tparam integer n the size of the indent
-- @tparam[opt=' '] string ch the character to use when indenting
-- @return indented string
function stringx.indent (s,n,ch)
assert_arg(1,s,'string')
assert_arg(2,n,'number')
local lines = usplit(s ,'\n')
local prefix = string.rep(ch or ' ',n)
for i, line in ipairs(lines) do
lines[i] = prefix..line
end
return concat(lines,'\n')..'\n'
end

I think this extra "\n" sequence shouldn't be here. It's not part of the functions documentations and is (at least in my use case) unwanted.

@Tieske
Copy link
Member

Tieske commented Nov 8, 2022

I tend to agree that the extra newline is weird. But is has been in here for 13 years. So not easy to fix, since it is breaking.

Maybe we should label this as something to fix in a next major version.

@Tieske Tieske added this to the 2.0.0 milestone Nov 8, 2022
@Tieske
Copy link
Member

Tieske commented Nov 8, 2022

This equally applies to dedent, and looking at the code it makes me wonder whether this even has a clean solution.
What is the expected output for in input that has NO trailing newline? That's easy, last line gets indented, and no trailing newline. Now what if the input does have a trailing newline? should that newline now be indented as well?

@Aire-One
Copy link
Author

Aire-One commented Nov 8, 2022

I tend to agree that the extra newline is weird. But is has been in here for 13 years. So not easy to fix, since it is breaking.

This equally applies to dedent, and looking at the code it makes me wonder whether this even has a clean solution.

With a second look at the code, I can see that there are even more of the stringx functions returning an extra \n.

What is the expected output for in input that has NO trailing newline? That's easy, last line gets indented, and no trailing newline.

Agree. I think it should be indent("hello\nworld", 2) == " hello\n world".

Now what if the input does have a trailing newline? should that newline now be indented as well?

Hm, I think it should, yes. It would be indent("hello\nworld\n", 2) == " hello\n world\n ".

I would let the caller have the responsibility to trim the end of the string before calling indent/dedent (if they need to remove the extra line).

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

No branches or pull requests

2 participants