1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
func main() {
text := `jour="3" article="banane" pepins="sans" categorie="fruit" article="banane" pepins="sans" categorie="fruit"
jour="5" article="pomme de terre" pepins="sans" categorie="légume" article="orange" pepins="avec" categorie="fruit"
jour="55" article="pomme de terre" pepins="sans" categorie="légume" article="orange" pepins="sans" categorie="fruit"
jour="6" article="orange" pepins="avec" categorie="fruit" article="courgette" pepins="sans" categorie="légume"
jour="7" article="orange" pepins="avec" categorie="fruit" article="banane" pepins="sans" categorie="fruit"
`
r := strings.NewReader(text)
sc := bufio.NewScanner(r)
type kv struct {
name string
value string
}
for sc.Scan() {
var n []kv
key := ""
value := ""
in_quote := false
in_escape := false
in_key := true
for _, r := range sc.Text() {
if !in_quote && r == '"' {
in_quote = true
continue
}
if in_quote && r == '\\' {
in_escape = true
continue
}
if in_quote && r == '"' {
if in_escape {
in_escape = false
} else {
in_quote = false
continue
}
}
if !in_quote && r == ' ' {
if key != "" {
n = append(n, kv{name: key, value: value})
}
key = ""
value = ""
in_key = true
continue
}
if in_key && r == '=' {
in_key = false
continue
}
if in_key {
key += string(r)
} else {
value += string(r)
}
}
if key != "" {
n = append(n, kv{name: key, value: value})
}
articles := map[string]map[string]string{}
keys := map[string]int{}
var article string
var jour string
var t int
for _, a := range n {
if a.name == "jour" {
jour = a.value
continue
}
if a.name == "article" {
article = a.value
}
keys[a.name] = t
t++
if _, ok := articles[article]; !ok {
articles[article] = map[string]string{}
}
articles[article][a.name] = a.value
}
var ukeys []string
for k := range keys {
ukeys = append(ukeys, k)
}
sort.Slice(ukeys, func(i, j int) bool {
return keys[ukeys[i]] < keys[ukeys[j]]
})
var res string
res = fmt.Sprintf("jour=%q", jour)
for _, key := range ukeys {
var val string
var prev string
var j int
for _, k := range articles {
if j < 1 {
val += k[key]
} else if prev != k[key] {
val += fmt.Sprintf(":[%v]", k[key])
}
prev = k[key]
j++
}
res += fmt.Sprintf(" %v=%q", key, val)
}
res = strings.TrimSpace(res)
fmt.Fprintf(os.Stdout, "%v\n", res)
}
} |
Partager