func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte { ms := MentionPattern.FindAll(rawBytes, -1) for _, m := range ms { rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(`<a href="/user/%s">%s</a>`, m[1:], m)), -1) } ms = commitPattern.FindAll(rawBytes, -1) for _, m := range ms { m = bytes.TrimSpace(m) i := strings.Index(string(m), "commit/") j := strings.Index(string(m), "#") if j == -1 { j = len(m) } rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1) } ms = issueFullPattern.FindAll(rawBytes, -1) for _, m := range ms { m = bytes.TrimSpace(m) i := strings.Index(string(m), "issues/") j := strings.Index(string(m), "#") if j == -1 { j = len(m) } rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1) } ms = issueIndexPattern.FindAll(rawBytes, -1) for _, m := range ms { rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf( `<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1) } return rawBytes }
func create() error { pemBytes, err := ioutil.ReadFile(*pem) if err != nil { return fmt.Errorf("reading -pem key failed: %s", err) } pubBytes, err := ioutil.ReadFile(*pub) if err != nil { return fmt.Errorf("reading -pub key failed: %s", err) } if *id == "" { *id = uuid.NewV4().String() } k := &keycreator.Key{ KontrolURL: *kontrolURL, KontrolPrivateKey: string(bytes.TrimSpace(pemBytes)), KontrolPublicKey: string(bytes.TrimSpace(pubBytes)), } kiteKey, err := k.Create(*username, *id) if err != nil { return fmt.Errorf("signing failed: %s", err) } if *file == "" { fmt.Println(kiteKey) return nil } return ioutil.WriteFile(*file, bytes.TrimSpace([]byte(kiteKey)), 0644) }
func (options *xml2) CommentHtml(out *bytes.Buffer, text []byte) { i := bytes.Index(text, []byte("-->")) if i > 0 { text = text[:i] } // strip, <!-- text = text[4:] var source []byte l := len(text) if l > 20 { l = 20 } for i := 0; i < l; i++ { if text[i] == '-' && text[i+1] == '-' { source = text[:i] text = text[i+2:] break } } // don't output a cref if it is not name -- remark if len(source) != 0 { source = bytes.TrimSpace(source) text = bytes.TrimSpace(text) out.WriteString("<t><cref source=\"") out.Write(source) out.WriteString("\">") out.Write(text) out.WriteString("</cref></t>\n") } return }
func FileGetConfig(filenameOrURL string, timeout ...time.Duration) (map[string]string, error) { data, err := FileGetBytes(filenameOrURL, timeout...) if err != nil { return nil, err } lines := bytes.Split(data, []byte("\n")) config := make(map[string]string, len(lines)) for _, line := range lines { kv := bytes.SplitN(line, []byte("="), 2) if len(kv) < 2 { continue } key := string(bytes.TrimSpace(kv[0])) if len(key) == 0 || key[0] == '#' { continue } value := string(bytes.TrimSpace(kv[1])) if len(value) >= 2 && value[0] == '"' && value[len(value)-1] == '"' { value = value[1 : len(value)-1] } config[key] = value } return config, nil }
func (r *CustomRender) AutoLink(out *bytes.Buffer, link []byte, kind int) { if kind != 1 { r.Renderer.AutoLink(out, link, kind) return } // This method could only possibly serve one link at a time, no need to find all. m := commitPattern.Find(link) if m != nil { m = bytes.TrimSpace(m) i := strings.Index(string(m), "commit/") j := strings.Index(string(m), "#") if j == -1 { j = len(m) } out.WriteString(fmt.Sprintf(` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))) return } m = issueFullPattern.Find(link) if m != nil { m = bytes.TrimSpace(m) i := strings.Index(string(m), "issues/") j := strings.Index(string(m), "#") if j == -1 { j = len(m) } out.WriteString(fmt.Sprintf(` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))) return } r.Renderer.AutoLink(out, link, kind) }
func fromLine(line []byte) Project { parts := bytes.SplitN(line, []byte{':'}, 2) return Project{ Name: string(bytes.TrimSpace(parts[0])), Path: string(bytes.TrimSpace(parts[1])), } }
func (options *xml) CommentHtml(out *bytes.Buffer, text []byte) { // nothing fancy any left of the first `:` will be used as the source="..." // if the syntax is different, don't output anything. i := bytes.Index(text, []byte("-->")) if i > 0 { text = text[:i] } // strip, <!-- text = text[4:] var source []byte l := len(text) if l > 20 { l = 20 } for i := 0; i < l; i++ { if text[i] == '-' && text[i+1] == '-' { source = text[:i] text = text[i+2:] break } } // don't output a cref if it is not name: remark if len(source) != 0 { // sanitize source here source = bytes.TrimSpace(source) text = bytes.TrimSpace(text) out.WriteString("<t><cref source=\"") out.Write(source) out.WriteString("\">") out.Write(text) out.WriteString("</cref></t>\n") } return }
func decodeChangesetState(data []byte) (State, error) { // example // --- // last_run: 2016-07-02 22:46:01.422137422 Z // sequence: 1912325 lines := bytes.Split(data, []byte("\n")) parts := bytes.Split(lines[1], []byte(":")) timeString := string(bytes.TrimSpace(bytes.Join(parts[1:], []byte(":")))) t, err := time.Parse( "2006-01-02 15:04:05.999999999 Z", timeString) if err != nil { return State{}, err } parts = bytes.Split(lines[2], []byte(":")) n, err := strconv.ParseUint(string(bytes.TrimSpace(parts[1])), 10, 64) if err != nil { return State{}, err } return State{ SeqNum: n, Timestamp: t, }, nil }
// An <xs:annotation> element may contain zero or more <xs:documentation> // children. The xsd package joins the content of these children, separated // with blank lines. func (doc *annotation) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { buf := make([][]byte, 1) var ( tok xml.Token err error ) Loop: for { tok, err = d.Token() if err != nil { break } switch tok := tok.(type) { case xml.EndElement: break Loop case xml.StartElement: if (tok.Name != xml.Name{schemaNS, "documentation"}) { if err := d.Skip(); err != nil { return err } } var frag []byte if err := d.DecodeElement(&frag, &tok); err != nil { return err } buf = append(buf, bytes.TrimSpace(frag)) } } *doc = annotation(bytes.TrimSpace(bytes.Join(buf, []byte("\n\n")))) return err }
func readLine(r *bufio.Reader) (fname string, types []string, data string, err error) { var line []byte for len(line) == 0 { var short bool line, short, err = r.ReadLine() if short { err = fmt.Errorf("line too long") return } if err != nil { return } line = bytes.TrimSpace(line) } b, err := r.ReadByte() if err == io.EOF || b != ' ' { r.UnreadByte() return parseLine(string(line)) } for b == ' ' && err != io.EOF { r.UnreadByte() stub, short, err := r.ReadLine() if !short { err = fmt.Errorf("line too long") return fname, types, data, err } line = append(line, bytes.TrimSpace(stub)...) b, err = r.ReadByte() } r.UnreadByte() return parseLine(string(line)) }
func parseFileHeader(header []byte) (*fileInfo, error) { if len(header) != 60 { panic("invalid file header") } if header[58] != filemagic[0] || header[59] != filemagic[1] { return nil, CorruptArchiveError("per file magic not found") } name := string(bytes.TrimSpace(header[0:16])) secs, err := strconv.ParseInt(string(bytes.TrimSpace(header[16:16+12])), 10, 64) if err != nil { return nil, CorruptArchiveError(err.Error()) } filemode, err := parseFileMode(string(bytes.TrimSpace(header[40 : 40+8]))) if err != nil { return nil, err } filesize, err := strconv.ParseInt(string(bytes.TrimSpace(header[48:48+10])), 10, 64) if err != nil { return nil, CorruptArchiveError(err.Error()) } fi := &fileInfo{ name: name, mtime: time.Unix(secs, 0), mode: filemode, size: filesize, } return fi, nil }
func fillArr(s *bufio.Scanner) (l []loc) { var err error for s.Scan() { if bytes.Count(s.Bytes(), []byte{','}) != 1 { if len(l) == 0 { ln, err := strconv.ParseInt(string(bytes.TrimSpace(s.Bytes())), 10, 0) check(err) l = make([]loc, 0, ln+10) } continue } t1 := make([]byte, len(s.Bytes())) copy(t1, s.Bytes()) tmploc := loc{t1, 0, 0} tmp := bytes.SplitN(bytes.Trim(tmploc.pts, "() "), []byte{','}, 3) tmploc.x, err = strconv.ParseFloat(string(bytes.TrimSpace(tmp[0])), 64) check(err) tmploc.y, err = strconv.ParseFloat(string(bytes.TrimSpace(tmp[1])), 64) check(err) l = append(l, tmploc) } if s.Err() != nil { log.Fatal(s.Err()) } sort.Sort(locA(l)) return }
// Parse func (c *Config) Parse(r *bufio.Reader) error { var section string = "defaults" for { l, err := r.ReadBytes('\n') if err == io.EOF { break } else if err != nil { return err } switch { case PatternEmpty.Match(l): continue case PatternComment.Match(l): continue case PatternSection.Match(l): m := PatternSection.FindSubmatch(l) section = string(bytes.TrimSpace(m[1])) case PatternOption.Match(l): m := PatternOption.FindSubmatch(l) c.Set(section, string(bytes.TrimSpace(m[1])), string(bytes.TrimSpace(m[2]))) default: return &ConfigSyntaxError{string(l)} } } return nil }
func ParseConfig(configpath string) (map[string]string, error) { file, err := os.Open(configpath) if err != nil { return nil, err } defer file.Close() M := make(map[string]string) buf := bufio.NewReader(file) var num int for { line, _, err := buf.ReadLine() if err != nil { if err.Error() == "EOF" { break } return nil, err } list := bytes.Split(line, []byte("=")) if len(list) != 2 { return nil, fmt.Errorf("第%d行 出现多次'='", num) } M[string(bytes.TrimSpace(list[0]))] = string(bytes.TrimSpace(list[1])) num++ } return M, nil }
func parsehdr(barr []byte, fn string) (int, error) { barr = bytes.TrimSpace(barr) // First character is a # if barr[0] != '#' { return 0, errors.New("Missing # to start header") } barr = barr[1:] // Second elt is the filename ndx1 := bytes.IndexAny(barr, ":") if ndx1 == -1 { return 0, errors.New("Malformed header, couldn't find filename") } if bytes.Compare(bytes.TrimSpace(barr[:ndx1]), []byte(fn)) != 0 { return 0, errors.New("Malformed header, unable to find filename in header") } // Find ranks barr = barr[ndx1+1:] ndx1 = bytes.Index(barr, []byte("rank")) if ndx1 == -1 { return 0, errors.New("Malformed header, could not find rank") } // Attempt to parse the rank rank64, err := strconv.ParseInt(string(bytes.TrimSpace(barr[:ndx1])), 10, 32) if err != nil { return 0, err } return int(rank64), nil }
// From a byte slice of a Go file find the tags. func findTags(co []byte) []string { p := co var tgs []string for len(p) > 0 { line := p if i := bytes.IndexByte(line, '\n'); i >= 0 { line, p = line[:i], p[i+1:] } else { p = p[len(p):] } line = bytes.TrimSpace(line) // Only look at comment lines that are well formed in the Go style if bytes.HasPrefix(line, []byte("//")) { line = bytes.TrimSpace(line[len([]byte("//")):]) if len(line) > 0 && line[0] == '+' { f := strings.Fields(string(line)) // We've found a +build tag line. if f[0] == "+build" { for _, tg := range f[1:] { tgs = append(tgs, tg) } } } } } return tgs }
// readEvent reads a single binlog event. It can be a single comment line, // or multiple lines terminated by the delimiter. func (bls *BinlogStreamer) readEvent(bufReader *bufio.Reader) (event []byte, err error) { for { fragment, err := bufReader.ReadSlice('\n') event = append(event, fragment...) if err == bufio.ErrBufferFull { continue } if err != nil { if err == io.EOF { return event, err } return event, fmt.Errorf("read error: %v", err) } // Use a different var than event, because you have to keep // the trailing \n if we continue trimmed := bytes.TrimSpace(event) if bytes.HasPrefix(trimmed, HASH_COMMENT) || bytes.HasPrefix(trimmed, SLASH_COMMENT) || bytes.HasPrefix(trimmed, DELIM_STMT) { return trimmed, nil } if bytes.HasSuffix(trimmed, bls.delim) { return bytes.TrimSpace(trimmed[:len(trimmed)-len(bls.delim)]), nil } } }
func ParseMatches(r io.Reader) ([]Match, error) { s := bufio.NewScanner(os.Stdin) s.Split(func(data []byte, atEOF bool) (advance int, token []byte, err error) { i := bytes.IndexAny(data, "\n \t\r") if len(data) == 0 { return 0, nil, nil } else if i == -1 && !atEOF { return 0, nil, nil } else if i == -1 && atEOF { return len(data), bytes.TrimSpace(data), nil } token = bytes.TrimSpace(data[:i]) if len(token) == 0 { token = nil } return i + 1, token, nil }) matches := []Match{} for { if !s.Scan() { break } winner := s.Text() if !s.Scan() { return nil, errors.New("odd number of players causes opponentless match") } loser := s.Text() matches = append(matches, Match{winner, loser}) } return matches, s.Err() }
func (d *Decoder) tryLine(indent, state int) bool { var line []byte var pos int if state == stateListElem { line, pos = d.peekLine() if len(bytes.TrimSpace(line)) != 0 { return true } d.off = pos } for { line, pos = d.peekLine() if d.off == pos { // at Eof return false } if len(bytes.TrimSpace(line)) != 0 { break } d.off = pos } if hasIndent(line, indent) { d.off += indent return true } return false }
//Main parse loop func (blp *Bls) parseBinlogEvents(sendReply proto.SendBinlogResponse, binlogReader io.Reader) { // read over the stream and buffer up the transactions var err error var line []byte bigLine := make([]byte, 0, BINLOG_BLOCK_SIZE) lineReader := bufio.NewReaderSize(binlogReader, BINLOG_BLOCK_SIZE) readAhead := false var event *blsEventBuffer var delimIndex int for { line = line[:0] bigLine = bigLine[:0] line, err = blp.readBlsLine(lineReader, bigLine) if err != nil { if err == io.EOF { //end of stream blp.globalState.blsStats.parseStats.Add("EOFErrors."+blp.keyrangeTag, 1) panic(newBinlogServerError(fmt.Sprintf("EOF"))) } panic(newBinlogServerError(fmt.Sprintf("ReadLine err: , %v", err))) } if len(line) == 0 { continue } if line[0] == '#' { //parse positional data line = bytes.TrimSpace(line) blp.currentLine = string(line) blp.parsePositionData(line) } else { //parse event data if readAhead { event.LogLine = append(event.LogLine, line...) } else { event = newBlsEventBuffer(blp.currentPosition, line) } delimIndex = bytes.LastIndex(event.LogLine, BINLOG_DELIMITER) if delimIndex != -1 { event.LogLine = event.LogLine[:delimIndex] readAhead = false } else { readAhead = true continue } event.LogLine = bytes.TrimSpace(event.LogLine) event.firstKw = string(bytes.ToLower(bytes.SplitN(event.LogLine, SPACE, 2)[0])) blp.currentLine = string(event.LogLine) //processes statements only for the dbname that it is subscribed to. blp.parseDbChange(event) blp.parseEventData(sendReply, event) } } }
// AutoLink defines how auto-detected links should be processed to produce corresponding HTML elements. // Reference for kind: https://github.com/russross/blackfriday/blob/master/markdown.go#L69-L76 func (r *Renderer) AutoLink(out *bytes.Buffer, link []byte, kind int) { if kind != blackfriday.LINK_TYPE_NORMAL { r.Renderer.AutoLink(out, link, kind) return } // Since this method could only possibly serve one link at a time, // we do not need to find all. m := CommitPattern.Find(link) if m != nil { m = bytes.TrimSpace(m) i := strings.Index(string(m), "commit/") j := strings.Index(string(m), "#") if j == -1 { j = len(m) } out.WriteString(fmt.Sprintf(` <code><a href="%s">%s</a></code>`, m, base.ShortSha(string(m[i+7:j])))) return } m = IssueFullPattern.Find(link) if m != nil { m = bytes.TrimSpace(m) i := strings.Index(string(m), "issues/") j := strings.Index(string(m), "#") if j == -1 { j = len(m) } out.WriteString(fmt.Sprintf(` <a href="%s">#%s</a>`, m, base.ShortSha(string(m[i+7:j])))) return } r.Renderer.AutoLink(out, link, kind) }
func ParseMessage(line []byte) *Message { line = bytes.TrimSpace(line) if len(line) <= 0 { return nil } m := new(Message) if line[0] == ':' { split := bytes.SplitN(line, []byte{' '}, 2) if len(split) <= 1 { return nil } m.Prefix = string(split[0][1:]) line = split[1] } split := bytes.SplitN(line, []byte{':'}, 2) args := bytes.Split(bytes.TrimSpace(split[0]), []byte{' '}) m.Command = string(bytes.ToUpper(args[0])) m.Args = make([]string, 0, len(args)) for _, arg := range args[1:] { m.Args = append(m.Args, string(arg)) } if len(split) > 1 { m.Args = append(m.Args, string(split[1])) } return m }
// promptConsolePass uses the given prefix to ask the user for a password. // The function will ask the user to confirm the passphrase and will repeat // the prompts until they enter a matching response. func promptConsolePass(prefix string, confirm bool) ([]byte, error) { // Prompt the user until they enter a passphrase. prompt := fmt.Sprintf("%s: ", prefix) for { fmt.Print(prompt) pass, err := terminal.ReadPassword(int(os.Stdin.Fd())) if err != nil { return nil, err } fmt.Print("\n") pass = bytes.TrimSpace(pass) if len(pass) == 0 { return nil, nil } if !confirm { return pass, nil } fmt.Print("Confirm passphrase: ") confirm, err := terminal.ReadPassword(int(os.Stdin.Fd())) if err != nil { return nil, err } fmt.Print("\n") confirm = bytes.TrimSpace(confirm) if !bytes.Equal(pass, confirm) { fmt.Println("The entered passphrases do not match.") continue } return pass, nil } }
func (rs *ranges) UnmarshalJSON(data []byte) (err error) { if data = bytes.Trim(data, `[]"`); len(data) == 0 { return nil } var rng [2]uint64 for _, r := range bytes.Split(data, []byte(",")) { ps := bytes.SplitN(r, []byte("-"), 2) if len(ps) != 2 { return fmt.Errorf("bad range: %s", r) } rng[0], err = strconv.ParseUint(string(bytes.TrimSpace(ps[0])), 10, 64) if err != nil { return err } rng[1], err = strconv.ParseUint(string(bytes.TrimSpace(ps[1])), 10, 64) if err != nil { return err } *rs = append(*rs, rng) } return nil }
func loadKey() { if *masterKeyFile != "" { b, err := ioutil.ReadFile(*masterKeyFile) if err != nil { log.Fatal(err) } masterKeyCache = bytes.TrimSpace(b) return } req, _ := http.NewRequest("GET", "http://metadata.google.internal/computeMetadata/v1/project/attributes/builder-master-key", nil) req.Header.Set("Metadata-Flavor", "Google") res, err := http.DefaultClient.Do(req) if err != nil { log.Fatal("No builder master key available") } defer res.Body.Close() if res.StatusCode != 200 { log.Fatalf("No builder-master-key project attribute available.") } slurp, err := ioutil.ReadAll(res.Body) if err != nil { log.Fatal(err) } masterKeyCache = bytes.TrimSpace(slurp) }
func ParseRoute(line []byte) (*Route, error) { route := &Route{} chunks := bytes.Split(line, []byte(",")) urlParams := bytes.Split(chunks[0], []byte(" ")) route.Method = strings.ToUpper(string(urlParams[0])) route.Path = string(urlParams[1]) for i, chunk := range chunks { if i != 0 { chunkParts := bytes.Split(chunk, []byte(":")) if len(chunkParts) != 2 { return nil, fmt.Errorf("unexpected route parameters: %v", string(line)) } name := string(bytes.TrimSpace(chunkParts[0])) value := string(bytes.TrimSpace(chunkParts[1])) if value[0] == '\'' && value[len(value)-1] == '\'' { value = value[1 : len(value)-1] } if name == "name" { route.Name = value } if name == "collection" && value == "true" { route.Collection = true } if name == "custom" && value == "true" { route.Custom = true } } } return route, nil }
func (options *xml2) BlockQuote(out *bytes.Buffer, text []byte, attribution []byte) { // Fake a list paragraph // TODO(miek): IAL, clear them for now options.Attr() out.WriteString("<t><list style=\"empty\">\n") out.Write(text) // check for "person -- URI" syntax use those if found // need to strip tags because these are attributes if len(attribution) != 0 { parts := bytes.Split(attribution, []byte("-- ")) if len(parts) > 0 { cite := bytes.TrimSpace(parts[0]) var quotedFrom []byte if len(parts) == 2 { quotedFrom = bytes.TrimSpace(parts[1]) } out.WriteString("<t>-- ") out.Write(cite) if len(parts) == 2 { if len(cite) > 0 { out.WriteString(", ") } // len(quotedFrom) == 0 check as well? out.Write(quotedFrom) } out.WriteString("</t>\n") } } out.WriteString("</list></t>\n") }
// PreProcessMarkdown renders full links of commits, issues and pulls to shorter version. func PreProcessMarkdown(rawHTML []byte, urlPrefix string) []byte { ms := commitPattern.FindAll(rawHTML, -1) for _, m := range ms { m = bytes.TrimSpace(m) i := strings.Index(string(m), "commit/") j := strings.Index(string(m), "#") if j == -1 { j = len(m) } rawHTML = bytes.Replace(rawHTML, m, []byte(fmt.Sprintf( ` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1) } ms = issueFullPattern.FindAll(rawHTML, -1) for _, m := range ms { m = bytes.TrimSpace(m) i := strings.Index(string(m), "issues/") j := strings.Index(string(m), "#") if j == -1 { j = len(m) } rawHTML = bytes.Replace(rawHTML, m, []byte(fmt.Sprintf( ` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1) } return rawHTML }
func (this *torConfig) LoadConfig(filename string) error { this.datas = make(map[string]string) file, err := os.Open(filename) if err != nil { return err } defer file.Close() buf := bufio.NewReader(file) for { line, _, err := buf.ReadLine() if err == io.EOF { break } if bytes.Equal(line, []byte{}) { continue } line = bytes.TrimSpace(line) if bytes.HasPrefix(line, []byte{'#'}) { continue } s := bytes.SplitN(line, []byte{'='}, 2) k := string(bytes.TrimSpace(s[0])) v := string(bytes.TrimSpace(s[1])) this.datas[k] = v } return nil }
// GenerateSSHKeyPair generates new key-pair for use with SSH. func GenerateSSHKeyPair() (*SSHKeyPair, error) { priv, err := rsa.GenerateKey(rand.Reader, 1024) if err != nil { return nil, err } var privBuf bytes.Buffer privPEM := &pem.Block{ Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv), } if err := pem.Encode(&privBuf, privPEM); err != nil { return nil, err } pub, err := ssh.NewPublicKey(&priv.PublicKey) if err != nil { return nil, err } key := &SSHKeyPair{ Private: bytes.TrimSpace(privBuf.Bytes()), Public: bytes.TrimSpace(ssh.MarshalAuthorizedKey(pub)), } sum := sha1.Sum(key.Private) key.Name = "koding-ssh-keypair-" + hex.EncodeToString(sum[:]) return key, nil }