Skip to content

Commit 233e184

Browse files
committed
fix String methods lacking in Java 11
1 parent 7a81ad9 commit 233e184

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

src/main/scala/00-zero-dep-utils.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ object StringExtensions:
88
def spaceSplit: Array[String] = s.split(" ").map(_.trim).filter(_.nonEmpty)
99
def words: Array[String] = s.split("([^a-zA-Z']+)'*\\1*").filter(_.nonEmpty)
1010

11+
/**
12+
* Indent and trim each line with spaces and add trailing newline if missing.
13+
* From Java 17 there is indent on String; this is here to run on Java 8, 11.
14+
*/
15+
def trimIndent(nbrSpaces: Int): String =
16+
s.trim.toLines.map(line => (" " * nbrSpaces) + line.trim).mkString("", "\n", "\n")
17+
18+
/**
19+
* Strip leading whitespace.
20+
* From Java 17 there is stripLeading on String; this is here to run on Java 8, 11.
21+
*/
22+
def stripLeadingWhitespace: String = s.dropWhile(_.isWhitespace)
23+
1124
def firstWord: String = s.takeWhile(_.isLetter)
1225

1326
def deCapitalize: String = s.take(1).toLowerCase ++ s.drop(1)
@@ -57,9 +70,6 @@ object StringExtensions:
5770

5871
def hasNewline: Boolean = s.contains("\n")
5972

60-
def trimIndent(nbrSpaces: Int): String =
61-
s.trim.toLines.map(line => line.trim).mkString("\n").indent(nbrSpaces)
62-
6373
def wrapLongLineAtWords(n: Int): String =
6474
val words = s.split(" ").iterator
6575
val sb = StringBuilder()

src/main/scala/05-MarkdownParser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ object MarkdownParser:
106106

107107
val afterNumOnThisLine =
108108
restOfLine
109-
.stripLeading
109+
.stripLeadingWhitespace
110110
.drop(if numOpt.isDefined && thirdOpt.isDefined then thirdOpt.get.length else 0)
111111
.trim
112112

0 commit comments

Comments
 (0)