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

FormatWriter: define string slice append method #4778

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -563,11 +563,11 @@ class FormatWriter(formatOps: FormatOps) {
val matcher = RegexCompat.getStripMarginPattern(pipe).matcher(text)
var pos = 0
while (matcher.find()) {
sb.append(CharBuffer.wrap(text, pos, matcher.start())).append(eol)
sb.add(text, pos, matcher.start()).append(eol)
if (matcher.start(1) >= 0) sb.append(spaces).append(pipe)
pos = matcher.end()
}
sb.append(CharBuffer.wrap(text, pos, text.length))
sb.add(text, pos, text.length)
case _ => sb.append(text)
}
}
Expand Down Expand Up @@ -681,10 +681,6 @@ class FormatWriter(formatOps: FormatOps) {
if (lines == 0 && style.comments.wrapSingleLineMlcAsSlc) sb
.setCharAt(begpos - 1, '/')
else sb.append(" */")

protected def append(csq: CharSequence, beg: Int, end: Int) = sb
.append(CharBuffer.wrap(csq, beg, end))

}

private class FormatSlc(text: String)(implicit sb: StringBuilder)
Expand All @@ -704,10 +700,8 @@ class FormatWriter(formatOps: FormatOps) {
(if (hasSpace) 0 else 1)
if (column > maxColumn && canRewrite) reFormat(trimmed)
else if (hasSpace) sb.append(trimmed)
else {
append(trimmed, 0, nonSlash).append(' ')
append(trimmed, nonSlash, trimmed.length)
}
else sb.add(trimmed, 0, nonSlash).append(' ')
.add(trimmed, nonSlash, trimmed.length)
}
}
private def reFormat(text: String): Unit = {
Expand Down Expand Up @@ -781,15 +775,15 @@ class FormatWriter(formatOps: FormatOps) {
val matcher = RegexCompat.leadingAsteriskSpace.matcher(text)
var pos = 0
while (matcher.find()) {
sb.append(CharBuffer.wrap(text, pos, matcher.start())).append(eol)
sb.add(text, pos, matcher.start()).append(eol)
val end = matcher.end()
val endMargin = matcher.end(1)
if (endMargin == end) // no asterisk
pos = if (end < text.length) matcher.start(1) else text.length
else { sb.append(spaces); pos = endMargin }
}
val lastLength = State.getLineLength(text, pos, text.length)
sb.append(CharBuffer.wrap(text, pos, pos + lastLength))
sb.add(text, pos, pos + lastLength)
}

private def appendLineBreak(): Unit = startNewLine(spaces).append('*')
Expand Down Expand Up @@ -1025,7 +1019,7 @@ class FormatWriter(formatOps: FormatOps) {
offsetOpt match {
case Some((offset, lineStart)) =>
sb.append(offset)
append(x, lineStart, x.length)
sb.add(x, lineStart, x.length)
case _ => sb.append(x)
}
}
Expand Down Expand Up @@ -1123,7 +1117,7 @@ class FormatWriter(formatOps: FormatOps) {
val extraMargin = matcher.end(1) - matcher.start(1) -
margin.length
if (extraMargin > 0) sb.append(getIndentation(extraMargin))
append(trimmed, contentBeg, contentEnd)
sb.add(trimmed, contentBeg, contentEnd)
iter(false)
}
}
Expand Down Expand Up @@ -1982,4 +1976,10 @@ object FormatWriter {
def isEmptyDocstring(text: String): Boolean = emptyDocstring.matcher(text)
.matches()

private implicit class ImplicitStringBuilder(private val sb: StringBuilder)
extends AnyVal {
def add(csq: CharSequence, beg: Int, end: Int): StringBuilder = sb
.append(CharBuffer.wrap(csq, beg, end))
}

}