question about String.stripIndent method

Jim Laskey james.laskey at oracle.com
Mon Jan 13 01:30:13 UTC 2020


Chris,

String::stripIndent might not be the routine you are looking for. It is provided to duplicate the actions performed by the compiler when processing a Text Block. To use String::stripIndent as a method to strip indentation in the way you might expect you might have to massage the input string.

Note: using ····· to represent leading spaces 

To understand what is going on, reverse the process and image what the string looked like as a Text Block.

The first case goes like this.

String endsWithNewLine = """
\s\s\s\s\s<- this line starts with 5 blank spaces.
\s\s\s\s\s<- count again: 1, 2, 3, 4, 5
········""";

String endsWithNewLineAfterStripIndent = """
·····<- this line starts with 5 blank spaces.
·····<- count again: 1, 2, 3, 4, 5
""";

In this case the first two lines start with 5 spaces and the last line has zero spaces because we stripped trailing spaces.

So applying the rules, the last line dominates and the indentation doesn't change.

·····<- this line starts with 5 blank spaces.
·····<- count again: 1, 2, 3, 4, 5


The last line still has zero spaces.

The second case goes like this.

String endsWithAsterisk = """
·····\s\s\s\s\s<- this line starts with 5 blank spaces.
·····\s\s\s\s\s<- count again: 1, 2, 3, 4, 5
········*""";

String endsWithAsteriskAfterStripIndent = """
<- this line starts with 5 blank spaces.
<- count again: 1, 2, 3, 4, 5
···*""";

Since there was no stripping of all characters on the last line, first two lines dominate and we get

<- this line starts with 5 blank spaces.
<- count again: 1, 2, 3, 4, 5
···*

So it all comes down to the last line being blank or not blank. A blank line becomes an empty line due to stripping of trailing blanks.

Cheers,

-- Jim



> On Jan 12, 2020, at 12:26 AM, Chris T <tech.meshter at gmail.com> wrote:
> 
> Hi,
> 
> I am working on an educational material about OpenJDK 14. Downloaded the
> code, compiled it and while "playing" with the new features I found
> something that seems to be inconsistent (maybe a bug?). So here we go:
> 
> I defined this method:
>  public void questionToOpenJDKPossibleBugOnStripIdent() {
> 
>    String endsWithNewLine = """
>        \s\s\s\s\s<- this line starts with 5 blank spaces.
>        \s\s\s\s\s<- count again: 1, 2, 3, 4, 5
>                """;
>    System.out.println("The string ending with new lines looks like this:");
>    System.out.println(endsWithNewLine);
> 
>    System.out.println();
>    System.out.println("If we call stripIndent, the white spaces are not
> eliminated (maybe I misunderstand the concept of incidental white space):");
>    System.out.println(endsWithNewLine.stripIndent());
> 
>    String endsWithAsterisk = """
>        \s\s\s\s\s<- this line starts with 5 blank spaces.
>        \s\s\s\s\s<- count again: 1, 2, 3, 4, 5
>                *""";
>    System.out.println("The string ending with asterisk looks like this:");
>    System.out.println(endsWithAsterisk);
> 
>    System.out.println();
>    System.out.println("If we call stripIndent, the white spaces ARE
> eliminated (this seems to be inconsistent with the previous behaviour):");
>    System.out.println(endsWithAsterisk.stripIndent());
>  }
> 
> If I run the code above, I get this output:
> The string ending with new lines looks like this:
>     <- this line starts with 5 blank spaces.
>     <- count again: 1, 2, 3, 4, 5
> 
> 
> If we call stripIndent, the white spaces are not eliminated (maybe I
> misunderstand the concept of incidental white space):
>     <- this line starts with 5 blank spaces.
>     <- count again: 1, 2, 3, 4, 5
> 
> The string ending with asterisk looks like this:
>     <- this line starts with 5 blank spaces.
>     <- count again: 1, 2, 3, 4, 5
>        *
> 
> If we call stripIndent, the white spaces ARE eliminated (this seems to be
> inconsistent with the previous behaviour):
> <- this line starts with 5 blank spaces.
> <- count again: 1, 2, 3, 4, 5
>   *
> 
> I looked over the stripIdent code and found this line (which, after
> debugging, turns out that "blocks" the entire indentation process):
> boolean optOut = lastChar == '\n' || lastChar == '\r';
> 
> Can somebody explain me what am I missing? Maybe there is an area din the
> JEP documentation that clarifies this and I didn't see nor actively paid
> attention to?
> 
> Thanks!
>  Chris T



More information about the core-libs-dev mailing list