This does not work in cases where someone want to use a Transformer in a streaming context (e.g. to create a Reader or Writer or to include the Transform in a Chain).
It may we useful to add something like strings.Replacer in runes as well. Alternatively, I have once implemented a generic rewriter for package runes that makes it much easier to create a Transform for arbitrary rewrites than writing one from scratch. It is a bit more involved than a Replacer. It was deemed a bit out of place in the runes package, so it was never submitted. I could add it to my github repo, though, if there is any interest. On Sat, Jun 25, 2016 at 5:47 PM, Matt Harden <matt.har...@gmail.com> wrote: > Sorry, make that > > strings.NewReplacer("\r\n", "\r\n", "\n", "\r\n").Replace(mystring) > > On Sat, Jun 25, 2016 at 8:46 AM Matt Harden <matt.har...@gmail.com> wrote: > >> Don't use x/text/runes for this. It's overkill. >> >> import "strings" >> ... >> strings.NewReplacer("\r\n", "\r\n", "\r", "\r\n").Replace(mystring) >> >> >> On Fri, Jun 24, 2016 at 2:00 PM mhhcbon <cpasmaboiteas...@gmail.com> >> wrote: >> >>> I forgot to mention another difficulty i have using replacement. >>> >>> As it will receive only one rune at a time in >>> runes.Map(func(r rune) rune {}) >>> >>> If the file already contains \r\n, i guess i will be doubling the \r, >>> resulting in an ugly \r\r\n >>> >>> Any ideas ? >>> >>> >>> >>> Le vendredi 24 juin 2016 22:54:35 UTC+2, mhhcbon a écrit : >>>> >>>> Hi, >>>> >>>> I have a small func like this >>>> >>>> >>>> func WriteAsWindows1252 (src string, dst string) error { >>>> bSrc, err := ioutil.ReadFile(src) >>>> if err != nil { >>>> return err >>>> } >>>> >>>> bDst := make([]byte, len(bSrc)*2) >>>> replaceNonAscii := runes.Map(func(r rune) rune { >>>> if r > unicode.MaxASCII { >>>> return rune('?') >>>> } >>>> return r >>>> }) >>>> transformer := transform.Chain(replaceNonAscii, charmap.Windows1252. >>>> NewEncoder()) >>>> _, _, err = transformer.Transform(bDst, bSrc, true) >>>> if err != nil { >>>> return err >>>> } >>>> >>>> return ioutil.WriteFile(dst, bDst, 0644) >>>> } >>>> >>>> I would like to add a new replacement of \n to \r\n. >>>> >>>> I don't see how i can do that as rune can take only \r or \n but not >>>> both. And runes.Map take a function which returns a unique rune. If i don t >>>> mistake. >>>> >>>> Is there a way to achieve this with Chain ? Or i got to go with a >>>> []byte.Replace https://golang.org/pkg/bytes/#Replace ? >>>> >>>> BTW, is it the correct way to encode an utf-8 file to windows1252 ? >>>> >>>> thanks! >>>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "golang-nuts" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to golang-nuts+unsubscr...@googlegroups.com. >>> For more options, visit https://groups.google.com/d/optout. >>> >> -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.