escape character (convert character to byte[] or string)

3,026 views
Skip to first unread message

laurent

unread,
Apr 18, 2011, 8:32:57 AM4/18/11
to golang-nuts
Hi,
I would like to escape character, I do it like this, is it correct?:

type DirectoryInfoWriter struct {
writer io.Writer
}

// this function escape '\n' '\r' ';' ',' character with the '\\'
character
func (di *DirectoryInfoWriter) WriteValue(value string) {
i := 0
for _, c := range value {
if i == 76 {
// if line to long fold value on multiple line
io.WriteString(di.writer, "\n ")
i=0;
}
if c == '\r' {
io.WriteString(di.writer, "\\r")
} else if c == '\n' {
io.WriteString(di.writer, "\\n")
} else if c == ';' {
io.WriteString(writer, "\\;")
} else if c == ',' {
io.WriteString(di.writer, "\\,")
} else {
// c is an int representing a character? convert it to string
(Multibyte character)
io.WriteString(di.writer, string(c))
}
i++
}
}

I'm not sure of how to write c ?

thanks

Jan Mercl

unread,
Apr 18, 2011, 9:22:38 AM4/18/11
to golan...@googlegroups.com
strconv.Quote is of possible utility in this case:
 

laurent

unread,
Apr 18, 2011, 10:22:15 AM4/18/11
to golang-nuts
thanks Jan
Great, It seems that it convert the current character into string with
string() native function as I do.

On 18 avr, 15:22, Jan Mercl <jan.me...@nic.cz> wrote:
> strconv.Quote is of possible utility in this case:http://golang.org/pkg/strconv/#Quote
> <http://golang.org/pkg/strconv/#Quote>

laurent

unread,
Apr 18, 2011, 12:00:38 PM4/18/11
to golang-nuts
ok,
This is a magic cast.
I thought that string() may do some transformation (this is why I call
it a function).
An int is not exactly map to a character in Go?
I misunderstand something:
An UTF-8 character is coded form 1 to 4 bytes.
And I thought that Go string is UTF-8 string.
I may have to look deeper in the documentation to understand.
Laurent

bflm

unread,
Apr 18, 2011, 12:16:55 PM4/18/11
to golang-nuts
On Apr 18, 6:00 pm, laurent <legoff.laur...@gmail.com> wrote:
> ok,
> This is a magic cast.

It's not a cast, it's a conversion. See "Conversions to and from a
string typ" here:
http://golang.org/doc/go_spec.html#Conversions

> I thought that string() may do some transformation (this is why I call
> it a function).
> An int is not exactly map to a character in Go?
> I misunderstand something:
> An UTF-8 character is coded form 1 to 4 bytes.
> And I thought that Go string is UTF-8 string.

Yes, conversion from an integer to string should produce an UTF-8
coded thing.

Quite often converting from UTF-8 to ints and back is not necessary
and can be completely avoided when processing UTF-8 data (depends on
what one is doing).

peterGo

unread,
Apr 18, 2011, 12:33:03 PM4/18/11
to golang-nuts
laurent,

What specifically don't you like?

Here's a more idiomatic version your program with test results.

package main

import (
"bytes"
"fmt"
"io"
)

type DirectoryInfoWriter struct {
writer io.Writer
}

// this function escape '\n' '\r' ';' ',' character
// with the '\\' character
func (di *DirectoryInfoWriter) WriteValue(value string) {
i := 0
for _, c := range value {
if i == 76 {
// if line to long fold value on multiple line
io.WriteString(di.writer, "\n ")
i = 0
}
var e string
switch c {
case '\r':
e = `\r`
case '\n':
e = `\n`
case ';':
e = `\;`
case ',':
e = `\,`
default:
// c is an int representing a Unicode code point.
// convert it to string (UTF-8 encoded character)
e = string(c)
}
io.WriteString(di.writer, e)
i++
}
}

func main() {
w := bytes.NewBufferString("")
di := DirectoryInfoWriter{w}
v := "| \n | \r | ; | , |"
fmt.Println(len(v), []byte(v))
di.WriteValue(v)
s := w.String()
fmt.Println(len(s), []byte(s))
fmt.Println(len(s), s)
}

Output:
17 [124 32 10 32 124 32 13 32 124 32 59 32 124 32 44 32 124]
21 [124 32 92 110 32 124 32 92 114 32 124 32 92 59 32 124 32 92 44 32
124]
21 | \n | \r | \; | \, |

Peter

laurent

unread,
Apr 18, 2011, 3:41:20 PM4/18/11
to golang-nuts
ok thanks I take it ;-)

laurent

unread,
Apr 18, 2011, 3:57:51 PM4/18/11
to golang-nuts
I was not sure about the conversion string(), that's all.
Thanks a lot for clarification
Reply all
Reply to author
Forward
0 new messages