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

Add "use_length" argument to str_pad() #190

Merged
merged 11 commits into from Apr 19, 2021

Conversation

yutannihilation
Copy link
Member

Fixes #188

As I commented on #188, stri_pad_*() gains use_length (gagolews/stringi#149) to control whether to use the total code point width or the number of code points as "width" of a string. But, the corresponding argument is not added on stringr's side so far. This PR simply adds it to str_pad().

As base R's nchar() has similar argument type which takes either "bytes", "chars" (default), or "width", I guess there must be some people who wants to control this behaviour, so exposing this argument is reasonable.

@hadley
Copy link
Member

hadley commented Nov 14, 2017

I think this is a good idea, but the docs are confusing me. Does this change the current default behaviour? (It probably should since you're usually padding to make strings aligned for output)

@@ -18,3 +18,8 @@ test_that("directions work for simple case", {
expect_equal(pad("left"), " had")
expect_equal(pad("both"), " had ")
})

test_that("padding based of length works", {
expect_identical(str_pad("\u4e2d", width = 6, side = "both"), " \u4e2d ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs comment describing what "\u4e2d" is

@yutannihilation
Copy link
Member Author

Does this change the current default behaviour?

No, but I think you don't need to be afraid.

The default behaviour already had been changed on the stringi's side after version 0.5-2, which pads strings based on the total character widths so that strings are properly aligned for output. Users who want the old behaviour can still choose the old behaviour by use_length = TRUE ("length" means "the number of characters" here, which may be a bit confusing...).

Does my explanation make sense? I'm sorry maybe my English is not good enough to document this nicely :(

@hadley
Copy link
Member

hadley commented Nov 15, 2017

In that case, this doesn't seem super useful to me given that the point of str_pad() is to align things visually.

@yutannihilation
Copy link
Member Author

Agreed. This is not super useful. Yet, it seems some people like #188 need the behaviour consistent with the default of base nchar() (I don't know the exact use cases, though).

Maybe you can close this PR for now and reopen if needed :)

@hadley
Copy link
Member

hadley commented Nov 15, 2017

Yeah, let me dig into that issue a bit more. Thanks!

@hadley hadley merged commit c6a07ec into tidyverse:master Apr 19, 2021
@yutannihilation
Copy link
Member Author

Thanks for merging!

@yutannihilation yutannihilation deleted the use-length branch April 19, 2021 14:39
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.

str_pad count 1 chinese char as 2
2 participants