This commit is contained in:
Laurent Ulrich 2024-07-07 10:32:06 +02:00
parent f5c589127a
commit f9b8ec213a
1 changed files with 21 additions and 7 deletions

View File

@ -7,19 +7,22 @@ import (
)
type Parser struct {
enclosers [][]rune
enclosers []string
delimiter rune
fields []string
}
func (p *Parser) Initialize(delimiter string, enclosers []string, lineFormat string) error {
if utf8.RuneCountInString(delimiter) != 1 {
return fmt.Errorf("delimiter shoud be 1 char length")
return fmt.Errorf("delimiter shoud be one character")
}
p.enclosers = make([][]rune, 0)
for _, str := range enclosers {
p.enclosers = append(p.enclosers, []rune(str))
p.enclosers = make([]string, 0)
for _, encloser := range enclosers {
if utf8.RuneCountInString(encloser) != 2 {
return fmt.Errorf("encolser should have to characters")
}
}
p.enclosers = enclosers
p.delimiter = []rune(delimiter)[0]
for _, pair := range enclosers {
if utf8.RuneCountInString(pair) != 2 {
@ -34,13 +37,24 @@ func (p *Parser) Initialize(delimiter string, enclosers []string, lineFormat str
}
func (p *Parser) Parse(line string) (map[string]string, error) {
currentFieldIndex := 0
ret := make(map[string]string)
value := ""
for index, r := range line {
if r == p.delimiter {
currentFieldIndex++
ret[p.fields[currentFieldIndex]] = value
continue
}
for _, encloser := range p.enclosers {
runes = []rune(encloser)
runes := []rune(encloser)
if r == runes[0] {
// opening encloser
tmpStr := line[index:]
for tmpIndex, tmpR := range tmpStr {
}
}
}
}