Thread overview
[Issue 19982] padLeft usability issues
Jun 19, 2019
shove
Jun 19, 2019
Stephen Grammer
Jun 20, 2019
shove
Jun 20, 2019
shove
Jun 20, 2019
Stephen Grammer
Jul 06, 2019
shove
June 19, 2019
https://issues.dlang.org/show_bug.cgi?id=19982

shove <shove@163.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |shove@163.com

--- Comment #1 from shove <shove@163.com> ---
I think the function you want is:
std.string.leftJustify(), std.string. rightJustify().

--------------------------------

std.range.padLeft() is normal, and it returns a range. The assert in the source file and the four lines I added are as follows:

--------------------------------

    import std.algorithm.comparison : equal;

    assert([1, 2, 3, 4].padLeft(0, 6).equal([0, 0, 1, 2, 3, 4]));
    assert([1, 2, 3, 4].padLeft(0, 3).equal([1, 2, 3, 4]));

    assert("abc".padLeft('_', 6).equal("___abc"));

    assert("a string".padLeft('0', 10).equal("00a string"));
    assert("a string".padLeft('0', 10).equal([48, 48, 97, 32, 115, 116, 114,
105, 110, 103]));

    assert("a string".padLeft(dchar('0'), 10).equal("00a string"));
    assert("a string".padLeft(dchar('0'), 10).equal([48, 48, 97, 32, 115, 116,
114, 105, 110, 103]));

--
June 19, 2019
https://issues.dlang.org/show_bug.cgi?id=19982

--- Comment #2 from Stephen Grammer <sgrammer@economicmodeling.com> ---
Thanks! I missed leftJustify. That is exactly what I need. If you are planning to add more asserts to document the behavior of padLeft, maybe consider adding:
-------------------------------------------------------
// implicitly converts to a range of uint when padding dchar[] with char
static assert(is(ElementType!(typeof("string".padLeft('0', 10))) == uint));
static assert(is(ElementType!(typeof("string".padLeft(dchar('0'), 10))) ==
dchar));

--
June 20, 2019
https://issues.dlang.org/show_bug.cgi?id=19982

--- Comment #3 from shove <shove@163.com> ---
Thanks, I see. There's something unreasonable here.

--
June 20, 2019
https://issues.dlang.org/show_bug.cgi?id=19982

--- Comment #4 from shove <shove@163.com> ---
The final step in the internal implementation of padLeft is chain() , and
chain() converts multiple different types into a single type that can be
accommodated via CommonType.

alias X = CommonType!(int, long, short);
assert(is(X == long));
alias Y = CommonType!(int, char[], short);
assert(is(Y == void));

--------------------------

static assert(is(ElementType!(typeof("string".padLeft('0', 10))) == uint));
static assert(is(ElementType!(typeof("string".padLeft(dchar('0'), 10))) ==
dchar));

The corresponding types of these two sentences are transformed as follows:

alias Z1 = CommonType!(char, dchar);
static assert(is(Z1 == uint));
alias Z2 = CommonType!(dchar, dchar);
static assert(is(Z2 == dchar));


So there is no problem with the design here, we can use it flexibly after mastering this feature.

--
June 20, 2019
https://issues.dlang.org/show_bug.cgi?id=19982

--- Comment #5 from Stephen Grammer <sgrammer@economicmodeling.com> ---
I didn't realize chain was so presumptuous. This makes me want to go check all of my code that uses chain and make sure I'm using it correctly. It IS documented though, which is comforting: "Due to safe type promotion in D, chaining together different character ranges results in a uint range."

Should we resolve this issue, or use it to improve documentation of some of these behaviors?

--
July 06, 2019
https://issues.dlang.org/show_bug.cgi?id=19982

shove <shove@163.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--