writeString writes the string s to p.output and updates p.pos, p.out, and p.last. If isLit is set, s is escaped w/ tabwriter.Escape characters to protect s from being interpreted by the tabwriter. Note: writeString is only used to write Go tokens, literals, and comments, all of which must be writte
(pos token.Position, s string, isLit bool)
| 290 | // avoids processing extra escape characters and reduces run time of the |
| 291 | // printer benchmark by up to 10%. |
| 292 | func (p *printer) writeString(pos token.Position, s string, isLit bool) { |
| 293 | if p.out.Column == 1 { |
| 294 | if p.Config.Mode&SourcePos != 0 { |
| 295 | p.writeLineDirective(pos) |
| 296 | } |
| 297 | p.writeIndent() |
| 298 | } |
| 299 | |
| 300 | if pos.IsValid() { |
| 301 | // update p.pos (if pos is invalid, continue with existing p.pos) |
| 302 | // Note: Must do this after handling line beginnings because |
| 303 | // writeIndent updates p.pos if there's indentation, but p.pos |
| 304 | // is the position of s. |
| 305 | p.pos = pos |
| 306 | } |
| 307 | |
| 308 | if isLit { |
| 309 | // Protect s such that is passes through the tabwriter |
| 310 | // unchanged. Note that valid Go programs cannot contain |
| 311 | // tabwriter.Escape bytes since they do not appear in legal |
| 312 | // UTF-8 sequences. |
| 313 | p.output = append(p.output, tabwriter.Escape) |
| 314 | } |
| 315 | |
| 316 | if debug { |
| 317 | p.output = append(p.output, fmt.Sprintf("/*%s*/", pos)...) // do not update p.pos! |
| 318 | } |
| 319 | p.output = append(p.output, s...) |
| 320 | |
| 321 | // update positions |
| 322 | nlines := 0 |
| 323 | var li int // index of last newline; valid if nlines > 0 |
| 324 | for i := 0; i < len(s); i++ { |
| 325 | // Raw string literals may contain any character except back quote (`). |
| 326 | if ch := s[i]; ch == '\n' || ch == '\f' { |
| 327 | // account for line break |
| 328 | nlines++ |
| 329 | li = i |
| 330 | // A line break inside a literal will break whatever column |
| 331 | // formatting is in place; ignore any further alignment through |
| 332 | // the end of the line. |
| 333 | p.endAlignment = true |
| 334 | } |
| 335 | } |
| 336 | p.pos.Offset += len(s) |
| 337 | if nlines > 0 { |
| 338 | p.pos.Line += nlines |
| 339 | p.out.Line += nlines |
| 340 | c := len(s) - li |
| 341 | p.pos.Column = c |
| 342 | p.out.Column = c |
| 343 | } else { |
| 344 | p.pos.Column += len(s) |
| 345 | p.out.Column += len(s) |
| 346 | } |
| 347 | |
| 348 | if isLit { |
| 349 | p.output = append(p.output, tabwriter.Escape) |
no test coverage detected