Beispiel #1
0
/*
Diff given two list of old and new revision id, compare each of them and save
their diff (deletions and additions as single string joined by " ") on dataset.

The revision text will be looked up in directory `Dir` and with extension
".txt".
*/
func Diff(oldids, newids []string, ext string) (
	tabula.DatasetInterface,
	error,
) {
	diffset := &tabula.Dataset{
		Mode: tabula.DatasetModeColumns,
	}

	colAdds := tabula.NewColumn(tabula.TString, "additions")
	colDels := tabula.NewColumn(tabula.TString, "deletions")

	// Get minimum length
	minlen := len(oldids)
	newlen := len(newids)
	if newlen < minlen {
		minlen = newlen
	}

	for x := 0; x < minlen; x++ {
		oldrevid := Dir + "/" + oldids[x] + ext
		newrevid := Dir + "/" + newids[x] + ext

		diffs, e := diff.Files(oldrevid, newrevid, diff.LevelWords)
		if e != nil {
			return nil, e
		}

		dels := diffs.GetAllDels()
		delstr := dels.Join(" ")
		delrec, e := tabula.NewRecordBy(delstr, tabula.TString)

		if e != nil {
			return nil, e
		}

		adds := diffs.GetAllAdds()
		addstr := adds.Join(" ")
		addrec, e := tabula.NewRecordBy(addstr, tabula.TString)

		if e != nil {
			return nil, e
		}

		colDels.PushBack(delrec)
		colAdds.PushBack(addrec)
	}

	diffset.PushColumn(*colDels)
	diffset.PushColumn(*colAdds)

	return diffset, nil
}
Beispiel #2
0
func populateWithRows(t *testing.T, dataset *tabula.Dataset) {
	for _, rowin := range datasetRows {
		row := make(tabula.Row, len(rowin))

		for x, recin := range rowin {
			rec, e := tabula.NewRecordBy(recin, datasetTypes[x])
			if e != nil {
				t.Fatal(e)
			}

			row[x] = rec
		}

		dataset.PushRow(&row)
	}
}
Beispiel #3
0
/*
ParseLine parse a line containing records. The output is array of record
(or single row).

This is how the algorithm works
(1) create n slice of record, where n is number of column metadata
(2) for each metadata
	(2.0) Check if next sequence if match with separator.
	(2.0.1) If its match, create empty record
	(2.1) If using left quote, skip until we found left-quote
	(2.2) If using right quote, append byte to buffer until right-quote
		(2.2.1) If using separator, skip until separator
	(2.3) If using separator, append byte to buffer until separator
	(2.4) else append all byte to buffer.
(3) save buffer to record
*/
func ParseLine(reader ReaderInterface, line []byte) (
	prow *tabula.Row, eRead *ReaderError,
) {
	p := 0
	rIdx := 0
	inputMd := reader.GetInputMetadata()
	row := make(tabula.Row, 0)

	for _, md := range inputMd {
		lq := md.GetLeftQuote()
		rq := md.GetRightQuote()
		sep := md.GetSeparator()
		v := []byte{}

		// (2.0)
		if sep != "" && sep != lq {
			match := tekstus.BytesMatchForward(line, []byte(sep),
				p)

			// (2.0.1)
			if match {
				p += len(sep)
				goto empty
			}
		}

		// (2.1)
		if lq != "" {
			p, eRead = parsingLeftQuote([]byte(lq), line, p)

			if eRead != nil {
				return
			}
		}

		// (2.2)
		if rq != "" {
			v, line, p, eRead = parsingRightQuote(reader, []byte(rq),
				line, p)

			if eRead != nil {
				return
			}

			if sep != "" {
				p, eRead = parsingSkipSeparator([]byte(sep),
					line, p)

				if eRead != nil {
					return
				}

				// Handle multi space if separator is a single
				// space.
				if sep == " " {
					p = parsingSkipSpace(line, p)
				}
			}
		} else {
			if sep != "" {
				// Skip space at beginning if separator is a
				// single space.
				if sep == " " {
					p = parsingSkipSpace(line, p)
				}

				v, p, eRead = parsingSeparator([]byte(sep),
					line, p)

				if eRead != nil {
					return
				}

				// Handle multi space if separator is a single
				// space.
				if sep == " " {
					p = parsingSkipSpace(line, p)
				}
			} else {
				v = line[p:]
				p = p + len(line)
			}
		}

		if md.GetSkip() {
			continue
		}
	empty:
		r, e := tabula.NewRecordBy(string(v), md.GetType())

		if nil != e {
			msg := fmt.Sprintf("md %s: Type convertion error from %q to %s",
				md.GetName(), string(v), md.GetTypeName())

			return nil, &ReaderError{
				T:    ETypeConversion,
				Func: "ParseLine",
				What: msg,
				Line: string(line),
				Pos:  p,
				N:    0,
			}
		}

		row = append(row, r)
		rIdx++
	}

	return &row, nil
}