question about String.stripIndent method

Chris T tech.meshter at gmail.com
Sun Jan 12 04:26:26 UTC 2020


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