func (t *Trie) forEach(f func([]byte, interface{}) bool, buf *bytes.Buffer) bool { if t.value == nil && t.children == nil { return true } pfx := buf.Len() buf.WriteString(t.suffix) if t.value != nil && !f(buf.Bytes(), t.value) { // this is the alloc (probably) return false } if t.children != nil { l := buf.Len() buf.WriteByte(t.base) for _, v := range t.children { if !v.forEach(f, buf) { return false } buf.Bytes()[l]++ } } buf.Truncate(pfx) return true }
func bulkEncode(metaBuilder MetaBuilder, body []interface{}) bytes.Buffer { var buf bytes.Buffer enc := json.NewEncoder(&buf) if metaBuilder == nil { for _, obj := range body { pos := buf.Len() if err := enc.Encode(obj); err != nil { debug("Failed to encode message: %s", err) buf.Truncate(pos) } } } else { for _, obj := range body { pos := buf.Len() meta := metaBuilder(obj) err := enc.Encode(meta) if err == nil { err = enc.Encode(obj) } if err != nil { debug("Failed to encode message: %s", err) buf.Truncate(pos) } } } return buf }
func saveDraw(w *draw.Window, statusCh chan string) { var buffer bytes.Buffer dir, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { panic(err) } buffer.WriteString(dir) statusCh <- fmt.Sprintf("Choose a Filename: %s", buffer.String()) for { ev := termbox.PollEvent() switch ev.Key { case termbox.KeyEnter: statusCh <- "Saved" return case termbox.KeyBackspace: if buffer.Len() > 0 { buffer.Truncate(buffer.Len() - 1) } default: buffer.WriteRune(ev.Ch) } statusCh <- fmt.Sprintf("Choose a Filename: %s", buffer.String()) } }
// String returns a fully parsable version of the store spec. func (ss StoreSpec) String() string { var buffer bytes.Buffer if len(ss.Path) != 0 { fmt.Fprintf(&buffer, "path=%s,", ss.Path) } if ss.InMemory { fmt.Fprint(&buffer, "type=mem,") } if ss.SizeInBytes > 0 { fmt.Fprintf(&buffer, "size=%s,", humanizeutil.IBytes(ss.SizeInBytes)) } if ss.SizePercent > 0 { fmt.Fprintf(&buffer, "size=%s%%,", humanize.Ftoa(ss.SizePercent)) } if len(ss.Attributes.Attrs) > 0 { fmt.Fprint(&buffer, "attrs=") for i, attr := range ss.Attributes.Attrs { if i != 0 { fmt.Fprint(&buffer, ":") } fmt.Fprintf(&buffer, attr) } fmt.Fprintf(&buffer, ",") } // Trim the extra comma from the end if it exists. if l := buffer.Len(); l > 0 { buffer.Truncate(l - 1) } return buffer.String() }
func summary(list []*CommentGroup) string { const maxLen = 40 var buf bytes.Buffer // collect comments text loop: for _, group := range list { // Note: CommentGroup.Text() does too much work for what we // need and would only replace this innermost loop. // Just do it explicitly. for _, comment := range group.List { if buf.Len() >= maxLen { break loop } buf.WriteString(comment.Text) } } // truncate if too long if buf.Len() > maxLen { buf.Truncate(maxLen - 3) buf.WriteString("...") } // replace any invisibles with blanks bytes := buf.Bytes() for i, b := range bytes { switch b { case '\t', '\n', '\r': bytes[i] = ' ' } } return string(bytes) }
func (options *xml) List(out *bytes.Buffer, text func() bool, flags, start int, group []byte) { marker := out.Len() ial := options.inlineAttr() if start > 1 { ial.GetOrDefaultAttr("start", strconv.Itoa(start)) } if group != nil { ial.GetOrDefaultAttr("group", string(group)) } s := ial.String() switch { case flags&_LIST_TYPE_ORDERED != 0: out.WriteString("<ol" + s + ">\n") case flags&_LIST_TYPE_DEFINITION != 0: out.WriteString("<dl" + s + ">\n") default: out.WriteString("<ul" + s + ">\n") } if !text() { out.Truncate(marker) return } switch { case flags&_LIST_TYPE_ORDERED != 0: out.WriteString("</ol>\n") case flags&_LIST_TYPE_DEFINITION != 0: out.WriteString("</dl>\n") default: out.WriteString("</ul>\n") } }
func helperScript(p *parser, out *bytes.Buffer, data []byte, c byte) int { i := 0 // write too much var raw bytes.Buffer for i < len(data) { if isspace(data[i]) { if i > 0 && data[i-1] == '\\' { // just written the '\', truncate to length-1 and write the space raw.Truncate(raw.Len() - 1) raw.WriteByte(data[i]) i++ continue } return 0 } if data[i] == c { var work bytes.Buffer p.inline(&work, raw.Bytes()) switch c { case '~': p.r.Subscript(out, work.Bytes()) return i + 1 // differences in how subscript is called ... case '^': p.r.Superscript(out, work.Bytes()) return i + 2 // ... compared to superscript } } raw.WriteByte(data[i]) i++ } return 0 }
func (d *DBO) Find(record_ptr interface{}, where string, params ...interface{}) error { switch t := reflect.TypeOf(record_ptr); { case t.Kind() != reflect.Ptr, t.Elem().Kind() != reflect.Struct: panic("only *Struct value allowed") } columns := Columns(record_ptr, EmptySkipList) if len(columns) == 0 { return NoColumnsTaggedError } var sql bytes.Buffer sql.WriteString("SELECT ") for _, col := range columns { sql.WriteString("`") sql.WriteString(col) sql.WriteString("`,") } // trim off last comma sql.Truncate(sql.Len() - 1) sql.WriteString(" FROM `") sql.WriteString(Table(record_ptr)) sql.WriteString("` ") sql.WriteString(where) return d.DB.QueryRow(sql.String(), params...).Scan(FieldPointers(record_ptr, EmptySkipList)...) }
// Reads a single word from a file, assuming space + tab + EOL to be word boundaries func ReadWord(fin *bufio.Reader) (word string, err error) { var a int = 0 var ch byte var buf bytes.Buffer for { ch, err = fin.ReadByte() if err == io.EOF { break } if ch == 13 { continue } if (ch == ' ') || (ch == '\t') || (ch == '\n') { if a > 0 { if ch == '\n' { fin.UnreadByte() } break } if ch == '\n' { word = "</s>" return } else { continue } } buf.WriteByte(ch) a++ } if a >= MAX_STRING { // Truncate too long words buf.Truncate(MAX_STRING) } word = buf.String() return }
// Compact appends to dst the JSON-encoded src with // insignificant space characters elided. func Compact(dst *bytes.Buffer, src []byte) os.Error { origLen := dst.Len() var scan scanner scan.reset() start := 0 for i, c := range src { v := scan.step(&scan, int(c)) if v >= scanSkipSpace { if v == scanError { break } if start < i { dst.Write(src[start:i]) } start = i + 1 } } if scan.eof() == scanError { dst.Truncate(origLen) return scan.err } if start < len(src) { dst.Write(src[start:]) } return nil }
func appendParamsList(list []paramItem, f Func, output bool) []paramItem { var tweaks = funcTweaks[f.GoName] var buf bytes.Buffer for _, param := range f.Param { tweak := tweaks.params[param.GoNameOrig] if tweak.omit || tweak.output != output { continue } item := paramItem{GoName: param.GoName} if tweak.retype != "" { item.GoType = param.GoType } else if param.Addr == 1 && param.Type == "GLvoid" { item.GoType = "interface{}" } else if tweak.single { item.GoType = param.GoType } else { buf.Truncate(0) for j := 0; j < param.Addr; j++ { buf.WriteString("[]") } if param.Array > 0 { buf.WriteByte('[') buf.WriteString(strconv.Itoa(param.Array)) buf.WriteByte(']') } buf.WriteString(param.GoType) item.GoType = buf.String() } list = append(list, item) } return list }
func (options *Latex) Header(out *bytes.Buffer, text func() bool, level int, id string) { marker := out.Len() switch level + options.PageLevel { case 1: out.WriteString("\n\\chapter{") case 2: out.WriteString("\n\\section{") case 3: out.WriteString("\n\\subsection{") case 4: out.WriteString("\n\\subsubsection{") case 5: out.WriteString("\n\\paragraph{") case 6: out.WriteString("\n\\subparagraph{") case 7: out.WriteString("\n\\textbf{") } if !text() { out.Truncate(marker) return } out.WriteString("}\n") }
func TestEOFWithinFrame(t *testing.T) { const bufSize = 64 for n := 0; ; n++ { var b bytes.Buffer wc := newConn(fakeNetConn{Reader: nil, Writer: &b}, false, 1024, 1024) rc := newConn(fakeNetConn{Reader: &b, Writer: nil}, true, 1024, 1024) w, _ := wc.NextWriter(BinaryMessage) w.Write(make([]byte, bufSize)) w.Close() if n >= b.Len() { break } b.Truncate(n) op, r, err := rc.NextReader() if err == errUnexpectedEOF { continue } if op != BinaryMessage || err != nil { t.Fatalf("%d: NextReader() returned %d, %v", n, op, err) } _, err = io.Copy(ioutil.Discard, r) if err != errUnexpectedEOF { t.Fatalf("%d: io.Copy() returned %v, want %v", n, err, errUnexpectedEOF) } _, _, err = rc.NextReader() if err != errUnexpectedEOF { t.Fatalf("%d: NextReader() returned %v, want %v", n, err, errUnexpectedEOF) } } }
func (rend *renderer) Paragraph(out *bytes.Buffer, text func() bool) { rend.ensureBlankLine(out) oPos := out.Len() if !text() { out.Truncate(oPos) } }
func formatQuoted(text []byte) []byte { if bytes.Equal(text, noOutputOld) { return noOutputNew } nonZero := bytes.HasSuffix(text, nonZeroOld) if nonZero { text = text[:len(text)-len(nonZeroOld)] } var buf bytes.Buffer buf.Grow(512) fmt.Fprintf(&buf, "%q", text) if buf.Len() > maxTextLen { truncateMaxLen(&buf, 1) buf.Truncate(len(bytes.TrimRight(buf.Bytes(), "\\"))) buf.WriteString(`" + `) buf.WriteString(more) } if nonZero { buf.WriteString(split) buf.WriteString(nonZeroNew) } return buf.Bytes() }
func (options *xml2) List(out *bytes.Buffer, text func() bool, flags, start int, group []byte) { marker := out.Len() // inside lists we must drop the paragraph if flags&_LIST_INSIDE_LIST == 0 { out.WriteString("<t>\n") } ial := options.inlineAttr() ial.KeepAttr([]string{"style", "counter"}) // start > 1 is not supported // for group, fake a numbered format (if not already given and put a // group -> current number in options switch { case flags&_LIST_TYPE_ORDERED != 0: switch { case flags&_LIST_TYPE_ORDERED_ALPHA_LOWER != 0: ial.GetOrDefaultAttr("style", "format %c") case flags&_LIST_TYPE_ORDERED_ALPHA_UPPER != 0: ial.GetOrDefaultAttr("style", "format %C") case flags&_LIST_TYPE_ORDERED_ROMAN_LOWER != 0: ial.GetOrDefaultAttr("style", "format %i") case flags&_LIST_TYPE_ORDERED_ROMAN_UPPER != 0: ial.GetOrDefaultAttr("style", "format %I") case flags&_LIST_TYPE_ORDERED_GROUP != 0: if group != nil { // don't think we need ++ this. options.group[string(group)]++ ial.GetOrDefaultAttr("counter", string(group)) ial.GetOrDefaultAttr("style", "format (%d)") } default: ial.GetOrDefaultAttr("style", "numbers") } case flags&_LIST_TYPE_DEFINITION != 0: ial.GetOrDefaultAttr("style", "hanging") default: ial.GetOrDefaultAttr("style", "symbols") } out.WriteString("<list" + ial.String() + ">\n") if !text() { out.Truncate(marker) return } switch { case flags&_LIST_TYPE_ORDERED != 0: out.WriteString("</list>\n") case flags&_LIST_TYPE_DEFINITION != 0: out.WriteString("</t>\n</list>\n") default: out.WriteString("</list>\n") } if flags&_LIST_INSIDE_LIST == 0 { out.WriteString("</t>\n") } }
func enc_struct(val *reflect.StructValue, typ *reflect.StructType, buffer *bytes.Buffer) os.Error { buffer.WriteString("{") first := true for i := 0; i < typ.NumField(); i++ { f := typ.Field(i) if f.Tag == "" { continue } tags := strings.Split(f.Tag[3:len(f.Tag)-1], ",", -1) l := buffer.Len() if first { buffer.WriteString(fmt.Sprintf("\"%v\":", tags[1])) } else { buffer.WriteString(fmt.Sprintf(",\"%v\":", tags[1])) } written, err := enc(val.Field(i), f.Type, buffer, tags[2] == "req") if err != nil { return err } if !written { buffer.Truncate(l) } else { first = false } } buffer.WriteString("}") return nil }
// AppLabelExpr returns the specification of the apparmor label describing // all the apps bound to a given slot. The result has one of three forms, // depending on how apps are bound to the slot: // // - "snap.$snap.$app" if there is exactly one app bound // - "snap.$snap.{$app1,...$appN}" if there are some, but not all, apps bound // - "snap.$snap.*" if all apps are bound to the slot func appLabelExpr(apps map[string]*snap.AppInfo, snap *snap.Info) []byte { var buf bytes.Buffer fmt.Fprintf(&buf, `"snap.%s.`, snap.Name()) if len(apps) == 1 { for appName := range apps { buf.WriteString(appName) } } else if len(apps) == len(snap.Apps) { buf.WriteByte('*') } else { appNames := make([]string, 0, len(apps)) for appName := range apps { appNames = append(appNames, appName) } sort.Strings(appNames) buf.WriteByte('{') for _, appName := range appNames { buf.WriteString(appName) buf.WriteByte(',') } buf.Truncate(buf.Len() - 1) buf.WriteByte('}') } buf.WriteByte('"') return buf.Bytes() }
func compact(dst *bytes.Buffer, src []byte, escape bool) error { origLen := dst.Len() var scan scanner scan.reset() start := 0 for i, c := range src { if escape && (c == '<' || c == '>' || c == '&') { if start < i { dst.Write(src[start:i]) } dst.WriteString(`\u00`) dst.WriteByte(hex[c>>4]) dst.WriteByte(hex[c&0xF]) start = i + 1 } v := scan.step(&scan, int(c)) if v >= scanSkipSpace { if v == scanError { break } if start < i { dst.Write(src[start:i]) } start = i + 1 } } if scan.eof() == scanError { dst.Truncate(origLen) return scan.err } if start < len(src) { dst.Write(src[start:]) } return nil }
func test(simpleMux *SimpleMux, n int) { sess, _ := simpleMux.NewSession() var buf bytes.Buffer binary.Write(&buf, binary.BigEndian, Header{Len: 4, ID: sess.ID()}) for i := 0; i != kSendTimes; i++ { buf.Truncate(12) binary.Write(&buf, binary.BigEndian, int32(i)) sess.Send(buf.Bytes()) packet, _ := sess.Recv() hdr := packet.Header.(*Header) if hdr.SessionID() != sess.ID() { out.Errorf("Session ID mismatch! %d %d", hdr.SessionID(), sess.ID()) } var nn int32 r := bytes.NewReader(packet.Body) binary.Read(r, binary.BigEndian, &nn) if int(nn) != i { out.Errorf("Out of order! %d %d", i, nn) } } sess.Close() wg.Done() }
func MultiLineToJson(multiLineInput string) (string, error) { var result bytes.Buffer // strip whitespace first multiLineInput = strings.TrimSpace(multiLineInput) // get the total amount of lines totalLines := strings.Count(multiLineInput, "\n") // assign a reader reader := bufio.NewReader(strings.NewReader(multiLineInput)) // start constructing the JSON string result.WriteString("{") for lineCount := 0; lineCount <= totalLines; lineCount++ { line, err := (reader.ReadString('\n')) if err != nil { break } else { kvPair := strings.Split(line, ": ") result.WriteString("\"" + kvPair[0] + "\" : \"" + strings.Trim(kvPair[1], "\n") + "\",") } } // loose the last "," and close with a "}" result.Truncate(int(len(result.String()) - 1)) result.WriteString("}") return result.String(), nil }
// Bytes returns a []byte representation of this message. // // As noted in rfc2812 section 2.3, messages should not exceed 512 characters // in length. This method forces that limit by discarding any characters // exceeding the length limit. func (m *Message) Bytes() []byte { buffer := new(bytes.Buffer) // Message prefix if m.Prefix != nil { buffer.WriteByte(prefix) m.Prefix.writeTo(buffer) buffer.WriteByte(space) } // Command is required buffer.WriteString(m.Command) // Space separated list of arguments if len(m.Params) > 0 { buffer.WriteByte(space) buffer.WriteString(strings.Join(m.Params, string(space))) } if len(m.Trailing) > 0 || m.EmptyTrailing { buffer.WriteByte(space) buffer.WriteByte(prefix) buffer.WriteString(m.Trailing) } // We need the limit the buffer length. if buffer.Len() > (maxLength) { buffer.Truncate(maxLength) } return buffer.Bytes() }
func (r SummaryRenderer) Paragraph(out *bytes.Buffer, text func() bool) { marker := out.Len() if !text() { out.Truncate(marker) } out.Write([]byte{' '}) }
func writeDiffHTML(out *bytes.Buffer, from, to, header string) { dmp := diffmatchpatch.New() diff := dmp.DiffMain(from, to, true) diff = dmp.DiffCleanupSemantic(diff) out.WriteString("<h1>" + html.EscapeString(header) + "</h1>\n<pre>") // write the diff for _, chunk := range diff { txt := html.EscapeString(chunk.Text) txt = strings.Replace(txt, "\n", "↩\n", -1) switch chunk.Type { case diffmatchpatch.DiffInsert: out.WriteString(`<ins style="background:#e6ffe6;">`) out.WriteString(txt) out.WriteString(`</ins>`) case diffmatchpatch.DiffDelete: out.WriteString(`<del style="background:#ffe6e6;">`) out.WriteString(txt) out.WriteString(`</del>`) case diffmatchpatch.DiffEqual: out.WriteString(`<span>`) out.WriteString(txt) out.WriteString(`</span>`) } } if out.Len() > MaxDetailsLen { out.Truncate(MaxDetailsLen) out.WriteString("\n\n[TRUNCATED]") } out.WriteString("</pre>") }
// newline preceded by two spaces becomes <br> // newline without two spaces works when EXTENSION_HARD_LINE_BREAK is enabled func lineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int { // remove trailing spaces from out outBytes := out.Bytes() end := len(outBytes) eol := end for eol > 0 && outBytes[eol-1] == ' ' { eol-- } out.Truncate(eol) precededByTwoSpaces := offset >= 2 && data[offset-2] == ' ' && data[offset-1] == ' ' precededByBackslash := offset >= 1 && data[offset-1] == '\\' // see http://spec.commonmark.org/0.18/#example-527 precededByBackslash = precededByBackslash && p.flags&EXTENSION_BACKSLASH_LINE_BREAK != 0 // should there be a hard line break here? if p.flags&EXTENSION_HARD_LINE_BREAK == 0 && !precededByTwoSpaces && !precededByBackslash { return 0 } if precededByBackslash && eol > 0 { out.Truncate(eol - 1) } p.r.LineBreak(out) return 1 }
func TestLdifWriter(t *testing.T) { fmt.Printf("TestLdifWriter: starting...\n") reader := strings.NewReader(simpleLDIF) lr, err := NewLDIFReader(reader) if err != nil { t.Errorf(err.Error()) } buf := new(bytes.Buffer) lw, lwerr := NewLDIFWriter(buf) if lwerr != nil { t.Errorf(err.Error()) } for { record, lerr := lr.ReadLDIFEntry() if lerr != nil { t.Error("Error reading LDIF: " + lerr.Error()) break } if record == nil { break } err = lw.WriteLDIFRecord(record) if err != nil { t.Errorf(err.Error()) } fmt.Print(buf.String()) buf.Truncate(0) } fmt.Printf("TestLdifWriter: ended.\n") }
func comma(s string) string { var buf bytes.Buffer parts := strings.Split(s, ".") pos := 0 if len(parts[0])%3 != 0 { pos += len(parts[0]) % 3 fmt.Println("ACK " + parts[0][:pos]) buf.WriteString(parts[0][:pos]) buf.Write([]byte{','}) } for ; pos < len(parts[0]); pos += 3 { buf.WriteString(parts[0][pos : pos+3]) buf.Write([]byte{','}) } buf.Truncate(buf.Len() - 1) if len(parts) > 1 { buf.Write([]byte{'.'}) buf.WriteString(parts[1]) } return buf.String() }
func TestPrintTableWithAndWithoutAutoWrap(t *testing.T) { var buf bytes.Buffer var multiline = `A multiline string with some lines being really long.` with := NewWriter(&buf) with.Append([]string{multiline}) with.Render() want := `+--------------------------------+ | A multiline string with some | | lines being really long. | +--------------------------------+ ` got := buf.String() if got != want { t.Errorf("multiline text rendering with wrapping failed\ngot:\n%s\nwant:\n%s\n", got, want) } buf.Truncate(0) without := NewWriter(&buf) without.SetAutoWrapText(false) without.Append([]string{multiline}) without.Render() want = `+-------------------------------------------+ | A multiline | | string with some lines being really long. | +-------------------------------------------+ ` got = buf.String() if got != want { t.Errorf("multiline text rendering without wrapping rendering failed\ngot:\n%s\nwant:\n%s\n", got, want) } }
func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id string) { marker := out.Len() doubleSpace(out) if id != "" { out.WriteString(fmt.Sprintf("<h%d id=\"%s\">", level, id)) } else if options.flags&HTML_TOC != 0 { // headerCount is incremented in htmlTocHeader out.WriteString(fmt.Sprintf("<h%d id=\"toc_%d\">", level, options.headerCount)) } else { out.WriteString(fmt.Sprintf("<h%d>", level)) } tocMarker := out.Len() if !text() { out.Truncate(marker) return } // are we building a table of contents? if options.flags&HTML_TOC != 0 { options.TocHeaderWithAnchor(out.Bytes()[tocMarker:], level, id) } out.WriteString(fmt.Sprintf("</h%d>\n", level)) }
func (v *vimDoc) Header(out *bytes.Buffer, text func() bool, level int, id string) { initPos := out.Len() if v.flags&flagNoRules == 0 { switch level { case 1: v.writeRule(out, "=") case 2: v.writeRule(out, "-") } } headingPos := out.Len() if !text() { out.Truncate(initPos) return } var temp []byte temp = append(temp, out.Bytes()[headingPos:]...) out.Truncate(headingPos) h := &heading{temp, level} v.headings = append(v.headings, h) tag := fmt.Sprintf("*%s*", v.buildHelpTag(h.text)) v.writeSplitText(out, bytes.ToUpper(h.text), []byte(tag), " ", 2) out.WriteString("\n") }