Write writes buf to the writer b. The only errors returned are ones encountered while writing to the underlying output stream.
(buf []byte)
| 525 | // The only errors returned are ones encountered |
| 526 | // while writing to the underlying output stream. |
| 527 | func (b *Writer) Write(buf []byte) (n int, err error) { |
| 528 | defer b.handlePanic(&err, "Write") |
| 529 | |
| 530 | // split text into cells |
| 531 | n = 0 |
| 532 | for i, ch := range buf { |
| 533 | if b.endChar == 0 { |
| 534 | // outside escape |
| 535 | switch ch { |
| 536 | case '\t', '\v', '\n', '\f': |
| 537 | // end of cell |
| 538 | b.append(buf[n:i]) |
| 539 | b.updateWidth() |
| 540 | n = i + 1 // ch consumed |
| 541 | ncells := b.terminateCell(ch == '\t') |
| 542 | if ch == '\n' || ch == '\f' { |
| 543 | // terminate line |
| 544 | b.addLine(ch == '\f') |
| 545 | if ch == '\f' || ncells == 1 { |
| 546 | // A '\f' always forces a flush. Otherwise, if the previous |
| 547 | // line has only one cell which does not have an impact on |
| 548 | // the formatting of the following lines (the last cell per |
| 549 | // line is ignored by format()), thus we can flush the |
| 550 | // Writer contents. |
| 551 | b.flushNoDefers() |
| 552 | if ch == '\f' && b.flags&Debug != 0 { |
| 553 | // indicate section break |
| 554 | b.write0(hbar) |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | case Escape: |
| 560 | // start of escaped sequence |
| 561 | b.append(buf[n:i]) |
| 562 | b.updateWidth() |
| 563 | n = i |
| 564 | if b.flags&StripEscape != 0 { |
| 565 | n++ // strip Escape |
| 566 | } |
| 567 | b.startEscape(Escape) |
| 568 | |
| 569 | case '<', '&': |
| 570 | // possibly an html tag/entity |
| 571 | if b.flags&FilterHTML != 0 { |
| 572 | // begin of tag/entity |
| 573 | b.append(buf[n:i]) |
| 574 | b.updateWidth() |
| 575 | n = i |
| 576 | b.startEscape(ch) |
| 577 | } |
| 578 | } |
| 579 | } else if ch == b.endChar { |
| 580 | // inside escape |
| 581 | // end of tag/entity |
| 582 | j := i + 1 |
| 583 | if ch == Escape && b.flags&StripEscape != 0 { |
| 584 | j = i // strip Escape |