MCPcopy Index your code
hub / github.com/coder/coder / truncateLines

Function truncateLines

agent/agentproc/headtail.go:261–303  ·  view source on GitHub ↗

truncateLines scans the input line by line and truncates any line longer than MaxLineLength.

(s string)

Source from the content-addressed store, hash-verified

259// truncateLines scans the input line by line and truncates
260// any line longer than MaxLineLength.
261func truncateLines(s string) string {
262 if len(s) <= MaxLineLength {
263 // Fast path: if the entire string is shorter than
264 // the max line length, no line can exceed it.
265 return s
266 }
267
268 var b strings.Builder
269 b.Grow(len(s))
270
271 for len(s) > 0 {
272 idx := strings.IndexByte(s, '\n')
273 var line string
274 if idx == -1 {
275 line = s
276 s = ""
277 } else {
278 line = s[:idx]
279 s = s[idx+1:]
280 }
281
282 if len(line) > MaxLineLength {
283 // Truncate preserving the suffix length so the
284 // total does not exceed a reasonable size.
285 cut := MaxLineLength - len(lineTruncationSuffix)
286 if cut < 0 {
287 cut = 0
288 }
289 _, _ = b.WriteString(line[:cut])
290 _, _ = b.WriteString(lineTruncationSuffix)
291 } else {
292 _, _ = b.WriteString(line)
293 }
294
295 // Re-add the newline unless this was the final
296 // segment without a trailing newline.
297 if idx != -1 {
298 _ = b.WriteByte('\n')
299 }
300 }
301
302 return b.String()
303}
304
305// Close marks the buffer as closed and wakes any waiters.
306// This is called when the process exits.

Callers 1

OutputMethod · 0.85

Calls 2

WriteStringMethod · 0.80
StringMethod · 0.45

Tested by

no test coverage detected