mirror of
https://github.com/lloeki/fix_latin.git
synced 2025-12-06 10:04:40 +01:00
factoring out stuff, with options
This commit is contained in:
parent
4faf404562
commit
f9abd8a0bb
1 changed files with 43 additions and 5 deletions
48
fix_latin.go
48
fix_latin.go
|
|
@ -1,16 +1,50 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type Encoding int
|
||||||
allow_control := false
|
|
||||||
handle_cp1252 := true
|
|
||||||
|
|
||||||
input, err := ioutil.ReadAll(os.Stdin)
|
const (
|
||||||
|
ISO_8859_1 Encoding = iota
|
||||||
|
// TODO: ISO_8859_15
|
||||||
|
CP1252
|
||||||
|
)
|
||||||
|
|
||||||
|
type Fixer struct {
|
||||||
|
allowControl bool
|
||||||
|
handleCP1252 bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func AllowControl(f *Fixer) error {
|
||||||
|
f.allowControl = true
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Assume(e Encoding) func(*Fixer) error {
|
||||||
|
return func(f *Fixer) error {
|
||||||
|
f.handleCP1252 = e == CP1252
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Fix(r io.Reader, w io.Writer, options ...func(*Fixer) error) {
|
||||||
|
f := &Fixer{}
|
||||||
|
|
||||||
|
for _, option := range options {
|
||||||
|
err := option(f)
|
||||||
|
if err != nil {
|
||||||
|
panic("invalid option")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
allow_control := f.allowControl
|
||||||
|
handle_cp1252 := f.handleCP1252
|
||||||
|
|
||||||
|
input, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
@ -132,5 +166,9 @@ func main() {
|
||||||
panic("unhandled char")
|
panic("unhandled char")
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Stdout.Write(output)
|
w.Write(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
Fix(os.Stdin, os.Stdout, AllowControl, Assume(CP1252))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue