Penser à gérer le case de délimiteurs multiples qui s'enchaînent et qui ne doivent être pris en compte que pour 1 seul
This commit is contained in:
parent
3e4b170099
commit
9f83b3ce1f
26
csvparser.go
26
csvparser.go
|
@ -8,27 +8,21 @@ import (
|
||||||
|
|
||||||
type CsvParser struct {
|
type CsvParser struct {
|
||||||
enclosers []string
|
enclosers []string
|
||||||
delimiter rune
|
delimiters string
|
||||||
fields []string
|
fields []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *CsvParser) Initialize(delimiter string, enclosers []string, lineFormat string) error {
|
func (p *CsvParser) Initialize(delimiters string, enclosers []string, lineFormat string) error {
|
||||||
if utf8.RuneCountInString(delimiter) != 1 {
|
if utf8.RuneCountInString(delimiter) == 0 {
|
||||||
return fmt.Errorf("delimiter shoud be one character")
|
return fmt.Errorf("delimiter shoud be at least one character")
|
||||||
}
|
}
|
||||||
p.enclosers = make([]string, 0)
|
p.delimiters = delimiters
|
||||||
for _, encloser := range enclosers {
|
for _, encloser := range enclosers {
|
||||||
if utf8.RuneCountInString(encloser) != 2 {
|
if utf8.RuneCountInString(encloser) != 2 {
|
||||||
return fmt.Errorf("encolser should have to characters")
|
return fmt.Errorf("encolser should have to characters")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.enclosers = enclosers
|
p.enclosers = enclosers
|
||||||
p.delimiter = []rune(delimiter)[0]
|
|
||||||
for _, pair := range enclosers {
|
|
||||||
if utf8.RuneCountInString(pair) != 2 {
|
|
||||||
return fmt.Errorf("encoloser should contain two characters: %s", pair)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// line format is in the form of: field1 field2 ignore ...
|
// line format is in the form of: field1 field2 ignore ...
|
||||||
// if field name is ignore, it is parsed but not retained
|
// if field name is ignore, it is parsed but not retained
|
||||||
p.fields = strings.Split(lineFormat, " ")
|
p.fields = strings.Split(lineFormat, " ")
|
||||||
|
@ -45,7 +39,7 @@ func (p *CsvParser) Parse(line string) (map[string]string, error) {
|
||||||
ret := make(map[string]string)
|
ret := make(map[string]string)
|
||||||
indexMax := len(line) - 1
|
indexMax := len(line) - 1
|
||||||
maxFieldIndex := len(p.fields) - 1
|
maxFieldIndex := len(p.fields) - 1
|
||||||
delimiter := p.delimiter
|
delimiters := p.delimiters
|
||||||
for index, r := range line {
|
for index, r := range line {
|
||||||
if r == '\\' {
|
if r == '\\' {
|
||||||
// Check if EOL before continue
|
// Check if EOL before continue
|
||||||
|
@ -56,7 +50,10 @@ func (p *CsvParser) Parse(line string) (map[string]string, error) {
|
||||||
escape = false
|
escape = false
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if r == delimiter {
|
for _, d := range delimiters {
|
||||||
|
delimiter = true
|
||||||
|
}
|
||||||
|
if delimiter {
|
||||||
if p.fields[currentFieldIndex] != "ignore" {
|
if p.fields[currentFieldIndex] != "ignore" {
|
||||||
ret[p.fields[currentFieldIndex]] = line[valueStart:index]
|
ret[p.fields[currentFieldIndex]] = line[valueStart:index]
|
||||||
}
|
}
|
||||||
|
@ -73,6 +70,7 @@ func (p *CsvParser) Parse(line string) (map[string]string, error) {
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if index >= indexMax {
|
if index >= indexMax {
|
||||||
if p.fields[currentFieldIndex] != "ignore" {
|
if p.fields[currentFieldIndex] != "ignore" {
|
||||||
ret[p.fields[currentFieldIndex]] = line[valueStart:]
|
ret[p.fields[currentFieldIndex]] = line[valueStart:]
|
||||||
|
@ -85,7 +83,7 @@ func (p *CsvParser) Parse(line string) (map[string]string, error) {
|
||||||
if r == runes[0] {
|
if r == runes[0] {
|
||||||
// opening encloser
|
// opening encloser
|
||||||
enclosed = true
|
enclosed = true
|
||||||
delimiter = runes[1]
|
delimiters = runes[1]
|
||||||
valueStart++
|
valueStart++
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue