-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathricerca.go
More file actions
54 lines (46 loc) · 1.46 KB
/
ricerca.go
File metadata and controls
54 lines (46 loc) · 1.46 KB
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
// ricerca all'interno degli array i comuni e nazioni
package generacodicefiscale
import (
"regexp"
"sort"
"strings"
)
// CFSearchError errore nella ricerca
type CFSearchError struct {
msg string
}
func (r *CFSearchError) Error() string {
return r.msg
}
//inizializza per "Normalizza"
var ns1=regexp.MustCompile("è|é")
var ns2=regexp.MustCompile("à")
var ns3=regexp.MustCompile("ù")
var ns4=regexp.MustCompile("ò")
var ns5=regexp.MustCompile("ì")
var ns6=regexp.MustCompile("[^a-z]")
// Normalizza : esegue alcune operazioni per permettere di confrontare i nomi in maniera agnostica dalle vocali
func Normalizza(s string) string {
s = strings.ToLower(s)
s = ns1.ReplaceAllString(s, "e")
s = ns2.ReplaceAllString(s, "a")
s = ns3.ReplaceAllString(s, "u")
s = ns4.ReplaceAllString(s, "o")
s = ns5.ReplaceAllString(s, "i")
return ns6.ReplaceAllString(s, "")
}
// CercaComune all'interno dell'array - normalizza prima, per evitare problemi con spazi, simboli od altro
// in ingresso: nome del comune
// in uscita: voce dell'array relativa o nil se non trovato, errore: nil o CFSearchError se non trovato
func CercaComune(a string) (*Comunecodice, *CFSearchError) {
na := Normalizza(a)
r := sort.Search(len(Comunecod), func(i int) bool { return strings.Compare(Comunecod[i].CoIdx, na) >= 0 })
if r < len(Comunecod) {
if strings.Compare(Comunecod[r].CoIdx, na) == 0 {
return &Comunecod[r], nil
}
}
er := new(CFSearchError)
er.msg = "Non trovato"
return nil, er
}