作者:ad
项目:d
// lineDiff returns b with all lines added or changed from a highlighted.
// It discards spaces within lines when comparing lines, so subtle
// gofmt-induced alignment changes are not flagged as changes.
// It also handles single-line diffs specially, highlighting only the
// changes within those lines.
func lineDiff(a, b []byte) []byte {
l := byteLines{bytes.Split(a, []byte("\n")), bytes.Split(b, []byte("\n"))}
cs := diff.Diff(len(l.a), len(l.b), diff.Data(l))
var buf bytes.Buffer
n := 0
for _, c := range cs {
for _, b := range l.b[n:c.B] {
buf.Write(b)
buf.WriteByte('\n')
}
if c.Ins > 0 {
if c.Ins == 1 && c.Del == 1 {
buf.Write(byteDiff(l.a[c.A], l.b[c.B]))
buf.WriteByte('\n')
} else {
for _, b := range l.b[c.B : c.B+c.Ins] {
buf.Write(colorize(b))
buf.WriteByte('\n')
}
}
}
n = c.B + c.Ins
}
for i, b := range l.b[n:] {
if i > 0 {
buf.WriteByte('\n')
}
buf.Write(b)
}
return buf.Bytes()
}
作者:wjdi
项目:go-price-fetche
func runDatCase(c []byte) int {
var counter int
defer func() {
if e := recover(); e != nil {
fmt.Println("ERROR while running test case:", e)
counter++
}
}()
parts := bytes.Split(c, []byte("#"))
if len(parts) != 4 {
counter++
}
if len(parts) != 4 && *verbose {
fmt.Printf("Malformed test case: %d, %q\n", len(parts), string(c))
return counter
}
fmt.Println("Running test case:", string(c))
testData := make(map[string]string)
for _, p := range parts[1:] {
t := bytes.Split(p, []byte("\n"))
testData[string(t[0])] = string(t[1])
}
p := h5.NewParserFromString(string(testData["data"]))
err := p.Parse()
if err != nil {
fmt.Println("Test case:", string(c))
fmt.Println("ERROR parsing: ", err)
counter++
} else {
if *verbose {
fmt.Println("SUCCESS!!!")
}
}
return counter
}
作者:Freeaqingm
项目:SshReverseProx
func LoadMap() error {
contents, err := ioutil.ReadFile(fileMapPath)
if err != nil {
return fmt.Errorf("Could not read file %s: %s", fileMapPath, err.Error())
}
lines := bytes.Split(contents, []byte("\n"))
newMap := make(map[string]string)
for i, line := range lines {
lineParts := bytes.Split(bytes.TrimSpace(line), []byte(" "))
if len(lineParts[0]) == 0 {
continue
}
user := string(lineParts[:1][0])
host := string(lineParts[len(lineParts)-1:][0])
if _, alreadyExists := newMap[user]; alreadyExists {
return fmt.Errorf("User %s was defined more than once on line %d", user, i)
}
newMap[user] = host
}
if len(newMap) < minEntries {
return fmt.Errorf("New Map only contains %d entries, which is less than the set minimum %d",
len(newMap), minEntries)
}
fileMapLock.Lock()
defer fileMapLock.Unlock()
fileMap = newMap
return nil
}
作者:fczuard
项目:pinbo
func (fl *FriendsList) Parse(buf []byte) (f map[string]string, err error) {
f = make(map[string]string)
for _, l := range bytes.Split(buf, []byte("\n")) {
if len(l) < 3 {
continue
}
parts := bytes.Split(l, []byte(" "))
if len(parts) != 2 {
return f, fmt.Errorf("format error. too many parts. %s", parts)
}
user := string(parts[0])
if len(user) < 1 {
return f, fmt.Errorf("invalid user: %s", user)
}
perm := string(parts[1])
if !validPerm(perm) {
return f, fmt.Errorf("invalid perm: %s", perm)
}
f[user] = perm
}
// ok everything seems good
return f, nil
}
作者:wcspromotea
项目:perm
func loadPerms(path string) (mperms PermissionsList) {
file, e := ioutil.ReadFile(path)
if e != nil {
fmt.Println("Could not get group permissions for", path, ":", e)
return
}
lines := bytes.Split(file, []byte("\n"), -1)
mperms = make(PermissionsList, len(lines))
for i, line := range lines {
parts := bytes.Split(line, []byte(" "), 2)
perms := mperms[i]
for _, perm := range parts[0] {
switch perm {
case 'r':
perms.Read = true
case 'w':
perms.Write = true
default:
fmt.Println("WARNING: Unrecognized permission", perm)
}
perms.Path = string(parts[1])
mperms[i] = perms
}
}
sort.Sort(mperms)
if !sort.IsSorted(mperms) {
fmt.Println("Failed to sort!")
}
return
}
作者:thought-machin
项目:pleas
func parseGcovCoverageResults(target *core.BuildTarget, coverage *core.TestCoverage, data []byte) error {
// The data we have is a sequence of .gcov files smashed together.
lines := bytes.Split(data, []byte{'\n'})
if len(lines) == 0 {
return fmt.Errorf("Empty coverage file")
}
currentFilename := ""
for lineno, line := range lines {
fields := bytes.Split(line, []byte{':'})
if len(fields) < 3 {
continue
}
if bytes.Equal(fields[2], []byte("Source")) {
if len(fields) < 4 {
return fmt.Errorf("Bad source on line %d: %s", lineno, string(line))
}
currentFilename = string(fields[3])
continue
}
covLine, err := strconv.Atoi(strings.TrimSpace(string(fields[1])))
if err != nil {
return fmt.Errorf("Bad line number on line %d: %s", lineno, string(line))
} else if covLine > 0 {
coverage.Files[currentFilename] = append(coverage.Files[currentFilename], translateGcovCount(bytes.TrimSpace(fields[0])))
}
}
return nil
}
作者:postfi
项目:qap-projec
// Reads a .dat file where the entries are matricies of numbers
// separated by empty new lines and individually aligned with
// whitespace between row entries and newlines between rows
func Read(file []byte) []matrix.Matrix {
data := make([]matrix.Matrix, 0)
for _, mat := range bytes.Split(file, []byte("\n\n")) {
temp := make(matrix.Matrix, 0)
didParse := true
Element:
for _, row := range bytes.Split(mat, []byte("\n")) {
floatsAsStrings := strings.Fields(string(row))
elms := make([]matrix.Element, 0)
for _, s := range floatsAsStrings {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
didParse = false
break Element
}
elms = append(elms, matrix.Element(f))
}
temp = append(temp, elms)
}
if didParse && len(temp[0]) > 0 {
data = append(data, temp)
}
}
return data
}
作者:aaronjareck
项目:concept-gam
func getWikiList() []wikiListItem {
if len(WikiList) == 0 {
body, err := ioutil.ReadFile("wiki5000.csv")
if err != nil {
log.Printf("Error loading wiki5000.csv: %s\n", err)
}
currentItem := new(wikiListItem)
lines := bytes.Split(body, []byte{'\n'})
for _, line := range lines {
parts := bytes.Split(line, []byte{','})
for i, p := range parts {
switch i {
case 0:
currentItem.Rank, _ = strconv.Atoi(string(p))
case 1:
currentItem.Link = string(p)
case 2:
currentItem.Title = string(p)
case 3:
currentItem.Views, _ = strconv.Atoi(string(p))
}
}
WikiList = append(WikiList, *currentItem)
currentItem = new(wikiListItem)
}
return WikiList
} else {
return WikiList
}
}
作者:rosatole
项目:coyi
func (v otrV3) parseFragmentPrefix(c *Conversation, data []byte) (rest []byte, ignore bool, ok bool) {
if len(data) < 23 {
return data, false, false
}
header := data[:23]
headerPart := bytes.Split(header, fragmentSeparator)[0]
itagParts := bytes.Split(headerPart, fragmentItagsSeparator)
if len(itagParts) < 3 {
return data, false, false
}
senderInstanceTag, err1 := parseItag(itagParts[1])
if err1 != nil {
return data, false, false
}
receiverInstanceTag, err2 := parseItag(itagParts[2])
if err2 != nil {
return data, false, false
}
if err := v.verifyInstanceTags(c, senderInstanceTag, receiverInstanceTag); err != nil {
switch err {
case errInvalidOTRMessage:
return data, false, false
case errReceivedMessageForOtherInstance:
return data, true, true
}
}
return data[23:], false, true
}
作者:shruti
项目:vites
func (blp *Blp) parseRotateEvent(line []byte) {
blp.RotateEventCount++
rem := bytes.Split(line, BINLOG_ROTATE_TO)
rem2 := bytes.Split(rem[1], POS)
rotateFilename := strings.TrimSpace(string(rem2[0]))
rotatePos, err := strconv.ParseUint(string(rem2[1]), 10, 64)
if err != nil {
panic(NewBinlogParseError(CODE_ERROR, fmt.Sprintf("Error in extracting rotate pos %v from line %s", err, string(line))))
}
if !blp.globalState.usingRelayLogs {
//If the file being parsed is a binlog,
//then the rotate events only correspond to itself.
blp.currentPosition.Position.MasterFilename = rotateFilename
blp.currentPosition.Position.MasterPosition = rotatePos
} else {
//For relay logs, the rotate events could be that of relay log or the binlog,
//the prefix of rotateFilename is used to test which case is it.
logsDir, relayFile := path.Split(blp.currentPosition.Position.RelayFilename)
currentPrefix := strings.Split(relayFile, ".")[0]
rotatePrefix := strings.Split(rotateFilename, ".")[0]
if currentPrefix == rotatePrefix {
//relay log rotated
blp.currentPosition.Position.RelayFilename = path.Join(logsDir, rotateFilename)
} else {
//master file rotated
blp.currentPosition.Position.MasterFilename = rotateFilename
blp.currentPosition.Position.MasterPosition = rotatePos
}
}
}
作者:kisiel
项目:vig
func TestIterWords(t *testing.T) {
tests := []struct {
in []byte
out [][]byte
}{
{[]byte("hello world"), bytes.Split([]byte("hello:world"), []byte(":"))},
{[]byte(" hello world "), bytes.Split([]byte("hello:world"), []byte(":"))},
}
for i, test := range tests {
out := [][]byte{}
f := func(word []byte) {
out = append(out, word)
}
IterWords(test.in, f)
if len(out) != len(test.out) {
t.Logf("%d: wrong output length: got %d want %d", i, len(out), len(test.out))
}
for j := range out {
if !bytes.Equal(out[j], test.out[j]) {
t.Logf("%d:%d: don't match. got %q want %q", i, j, out[j], test.out[j])
t.Fail()
}
}
}
}
作者:Epictetu
项目:wfd
func DiffBytes(origb, endb []byte) []Diff {
origl := bytes.Split(origb, []byte("\n"), -1)
endl := bytes.Split(endb, []byte("\n"), -1)
// Check if the streams are at all different
// Do length check first for efficiency reasons.
if len(origl) == len(endl) {
if bytes.Equal(origb, endb) {
// Bytes are equal!
return nil
}
}
for i, _ := range origl {
if i >= len(endl) {
fmt.Println("Out of range panic coming up!")
fmt.Println(origl, endl, i)
}
if bytes.Equal(origl[i], endl[i]) {
continue
}
// Search forward for the line
for j := i; j < len(endl); j++ {
if bytes.Equal(origl[i], endl[j]) {
fmt.Println("Found match for line", i, "at line", j)
}
}
for j := i; j >= 0; j-- {
if bytes.Equal(origl[i], endl[j]) {
fmt.Println("Found match for line", i, "at line", j)
}
}
}
return nil
}
作者:nyush
项目:traprox
// NewRequestHeader returns RequestHeader from bytes
func NewRequestHeader(b []byte) (*RequestHeader, error) {
lines := bytes.Split(b, eol)
reqline := bytes.Split(lines[0], []byte{' '})
headers := [][][]byte{}
bodySize := 0
headerLines := lines[1:]
for _, l := range headerLines {
tokens := bytes.SplitN(l, []byte{':', ' '}, 2)
if len(tokens) == 2 {
headers = append(headers, tokens)
}
if bytes.Equal(bytes.ToLower(tokens[0]), []byte("content-length")) {
size, err := strconv.Atoi(string(tokens[1]))
if err != nil {
return nil, err
}
bodySize = size
}
}
r := &RequestHeader{
ReqLineTokens: reqline,
Headers: headers,
BodySize: bodySize,
BodyRead: 0,
}
return r, nil
}
作者:jwowill
项目:mancala
// agentsMap returns a mapping of agents used by the web app to their
// descriptions.
//
// Returns an error if they can't be retrieved correctly.
func agentsMap() ([]map[string]string, error) {
command := "mancalai_cli"
args := []string{"-tool", "compare", "-agents"}
output, err := exec.Command(command, args...).Output()
if err != nil {
return nil, fmt.Errorf(
"Couldn't complete command '%s'",
command,
)
}
lines := bytes.Split(output, []byte{'\n'})
var agents []map[string]string
separator := []byte{':', ' '}
for _, line := range lines {
if len(line) > 0 {
nameAndDescription := bytes.Split(line, separator)
name := nameAndDescription[0]
description := nameAndDescription[1]
agents = append(agents, map[string]string{
"name": string(name),
"description": string(description),
})
}
}
return agents, nil
}
作者:philipmulcah
项目:godebu
// equivalent does a linewise comparison of a and b.
// For each line:
// got exactly equals want OR
// want ends in " //substr" and is a substring of got OR
// want ends in " //slashes" and runtime.GOOS == "windows" and got equals want with its slashes swapped for backslashes
// Otherwise equivalent returns false.
func equivalent(got, want []byte) bool {
var (
gotLines = bytes.Split(got, newline)
wantLines = bytes.Split(want, newline)
substr = []byte(" //substr")
slashes = []byte(" //slashes")
slash = []byte{'/'}
gg, ww []byte
)
if len(gotLines) != len(wantLines) {
return false
}
for i := range gotLines {
gg, ww = gotLines[i], wantLines[i]
if bytes.HasSuffix(ww, slashes) {
ww = bytes.Replace(ww[:len(ww)-len(slashes)], slash, []byte{filepath.Separator}, -1)
}
if !(bytes.Equal(gg, ww) || bytes.HasSuffix(ww, substr) && bytes.Contains(gg, ww[:len(ww)-len(substr)])) {
return false
}
}
return true
}
作者:flowl
项目:coduno-ap
func compare(want, have io.Reader) ([]int, bool, error) {
w, err := ioutil.ReadAll(want)
if err != nil {
return nil, false, err
}
h, err := ioutil.ReadAll(have)
if err != nil {
return nil, false, err
}
wb := bytes.Split(w, []byte("\n"))
hb := bytes.Split(h, []byte("\n"))
if len(wb) != len(hb) {
return nil, false, nil
}
var diff []int
ok := true
for i := 0; i < len(wb); i++ {
if bytes.Compare(wb[i], hb[i]) != 0 {
diff = append(diff, i)
ok = false
}
}
return diff, ok, nil
}
作者:danny800
项目:g
func netshInterfaceIPv6ShowAddress(name string) ([]string, error) {
// TODO: need to test ipv6 netmask too, but netsh does not outputs it
out, err := runCmd("netsh", "interface", "ipv6", "show", "address", "interface=\""+name+"\"")
if err != nil {
return nil, err
}
addrs := make([]string, 0)
lines := bytes.Split(out, []byte{'\r', '\n'})
for _, line := range lines {
if !bytes.HasPrefix(line, []byte("Address")) {
continue
}
if !bytes.HasSuffix(line, []byte("Parameters")) {
continue
}
f := bytes.Split(line, []byte{' '})
if len(f) != 3 {
continue
}
// remove scope ID if present
f = bytes.Split(f[1], []byte{'%'})
addrs = append(addrs, string(bytes.ToLower(bytes.TrimSpace(f[0]))))
}
return addrs, nil
}
作者:nullstyl
项目:flee
// TestHostKeyFile tests to read and write from HostKeyFile
func TestHostKeyFile(t *testing.T) {
os.Remove(hostFileBackup)
defer os.Remove(hostFileBackup)
in := NewHostKeyFile(hostFile)
out := NewHostKeyFile(hostFileBackup)
hostKeys, err := in.GetHostKeys()
if err != nil {
t.Fatal("reading host file error:", err)
}
for i, v := range hostKeys {
if err = out.PutHostKey(i, v); err != nil {
t.Fatal("append error:", err)
}
}
keysByte, _ := ioutil.ReadFile(hostFile)
keysByteBackup, _ := ioutil.ReadFile(hostFileBackup)
keyBytes := bytes.Split(keysByte, []byte{'\n'})
keyBytesBackup := bytes.Split(keysByteBackup, []byte{'\n'})
for _, keyByte := range keyBytes {
find := false
for _, keyByteBackup := range keyBytesBackup {
find = bytes.Compare(keyByte, keyByteBackup) == 0
if find {
break
}
}
if !find {
t.Fatalf("host file difference")
}
}
}
作者:b1nar
项目:goPasteWik
func saveHandler(w http.ResponseWriter, r *http.Request, title string) {
page, _ := loadPage(title)
boder := bytes.Split(page.Body, []byte("~![META]!~"))
confer := bytes.Split(boder[0], []byte("::"))
if strings.TrimSpace(r.FormValue("password_")) == strings.TrimSpace(string(confer[2])) {
body := r.FormValue("body")
public_edit := "false"
public_view := "false"
if r.FormValue("public_view") == "on" {
public_view = "true"
}
if r.FormValue("public_edit") == "on" {
public_edit = "true"
}
new_password := r.FormValue("new_password")
body = public_view + "::" + public_edit + "::" + new_password + "\n~![META]!~\n" + body
p := &Page{Title: title, Body: []byte(body)}
err := p.save()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/view/"+title+"?password="+new_password+"&saved=succes", http.StatusFound)
} else {
http.Redirect(w, r, "/view/"+title+"?password="+strings.TrimSpace(r.FormValue("password_"))+"&saved=fail", http.StatusFound)
}
}
作者:bernerdschaefe
项目:statsd.g
func (server *CollectionServer) handlePacket(packet []byte) {
server.metrics.UpdateCounter("statsd.packets_received", 1, 1)
metrics := bytes.Split(packet, []byte("\n"))
for _, metric := range metrics {
parts := bytes.Split(metric, []byte(":"))
key := string(parts[0])
for _, bit := range parts[1:] {
fields := bytes.Split(bit, []byte("|"))
if len(fields) == 1 {
server.metrics.UpdateCounter("statsd.bad_lines_seen", 1, 1)
continue
}
sampleRate := float64(1)
value, _ := strconv.ParseFloat(string(fields[0]), 64)
if len(fields) == 3 {
sampleRate, _ = strconv.ParseFloat(string(fields[2]), 64)
}
switch {
case string(fields[1]) == "ms":
server.metrics.UpdateTimer(key, value)
case string(fields[1]) == "g":
server.metrics.UpdateGauge(key, value)
default:
server.metrics.UpdateCounter(key, value, sampleRate)
}
}
}
}