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.