Commit 18004fbd authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Kamil Kisiel

Move pyquote into main codebase

We added pyquote in b429839d (tests: Show pickles in a way that can be
copy-pasted into Python). However in the next patch this functionality
will be needed for the encoder to fix encoding of strings at protocol 0.
Thus we need this function to be not test-only.

Plain code movement here, no semantic change.
parent cdd8269c
...@@ -10,7 +10,6 @@ import ( ...@@ -10,7 +10,6 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"testing" "testing"
"unicode/utf8"
) )
func bigInt(s string) *big.Int { func bigInt(s string) *big.Int {
...@@ -875,58 +874,6 @@ func TestUnquoteCharEOF(t *testing.T) { ...@@ -875,58 +874,6 @@ func TestUnquoteCharEOF(t *testing.T) {
} }
} }
// pyquote, similarly to strconv.Quote, quotes s with " but does not use "\u" and "\U" inside.
//
// We need to avoid \u and friends, since for regular strings Python translates
// \u to \\u, not an UTF-8 character.
//
// Dumping strings in a way that is possible to copy/paste into Python and use
// pickletools.dis and pickle.loads there to verify a pickle is handy.
func pyquote(s string) string {
const hexdigits = "0123456789abcdef"
out := make([]byte, 0, len(s))
for {
r, width := utf8.DecodeRuneInString(s)
if width == 0 {
break
}
emitRaw := false
switch {
// invalid & everything else goes in numeric byte escapes
case r == utf8.RuneError:
fallthrough
default:
emitRaw = true
case r == '\\' || r == '"':
out = append(out, '\\', byte(r))
case strconv.IsPrint(r):
out = append(out, s[:width]...)
case r < ' ':
rq := strconv.QuoteRune(r) // e.g. "'\n'"
rq = rq[1:len(rq)-1] // -> `\n`
out = append(out, rq...)
}
if emitRaw {
for i := 0; i < width; i++ {
out = append(out, '\\', 'x', hexdigits[s[i]>>4], hexdigits[s[i]&0xf])
}
}
s = s[width:]
}
return "\"" + string(out) + "\""
}
// like io.LimitedReader but for writes // like io.LimitedReader but for writes
// XXX it would be good to have it in stdlib // XXX it would be good to have it in stdlib
type LimitedWriter struct { type LimitedWriter struct {
......
package ogórek
import (
"strconv"
"unicode/utf8"
)
// pyquote, similarly to strconv.Quote, quotes s with " but does not use "\u" and "\U" inside.
//
// We need to avoid \u and friends, since for regular strings Python translates
// \u to \\u, not an UTF-8 character.
//
// Dumping strings in a way that is possible to copy/paste into Python and use
// pickletools.dis and pickle.loads there to verify a pickle is handy.
func pyquote(s string) string {
const hexdigits = "0123456789abcdef"
out := make([]byte, 0, len(s))
for {
r, width := utf8.DecodeRuneInString(s)
if width == 0 {
break
}
emitRaw := false
switch {
// invalid & everything else goes in numeric byte escapes
case r == utf8.RuneError:
fallthrough
default:
emitRaw = true
case r == '\\' || r == '"':
out = append(out, '\\', byte(r))
case strconv.IsPrint(r):
out = append(out, s[:width]...)
case r < ' ':
rq := strconv.QuoteRune(r) // e.g. "'\n'"
rq = rq[1:len(rq)-1] // -> `\n`
out = append(out, rq...)
}
if emitRaw {
for i := 0; i < width; i++ {
out = append(out, '\\', 'x', hexdigits[s[i]>>4], hexdigits[s[i]&0xf])
}
}
s = s[width:]
}
return "\"" + string(out) + "\""
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment