diff options
Diffstat (limited to 'libgo/go/strconv/quote.go')
-rw-r--r-- | libgo/go/strconv/quote.go | 67 |
1 files changed, 59 insertions, 8 deletions
diff --git a/libgo/go/strconv/quote.go b/libgo/go/strconv/quote.go index 53d51b5a46a..40d0667551a 100644 --- a/libgo/go/strconv/quote.go +++ b/libgo/go/strconv/quote.go @@ -12,7 +12,7 @@ import ( const lowerhex = "0123456789abcdef" -func quoteWith(s string, quote byte, ASCIIonly bool) string { +func quoteWith(s string, quote byte, ASCIIonly, graphicOnly bool) string { var runeTmp [utf8.UTFMax]byte buf := make([]byte, 0, 3*len(s)/2) // Try to avoid more allocations. buf = append(buf, quote) @@ -38,7 +38,7 @@ func quoteWith(s string, quote byte, ASCIIonly bool) string { buf = append(buf, byte(r)) continue } - } else if IsPrint(r) { + } else if IsPrint(r) || graphicOnly && isInGraphicList(r) { n := utf8.EncodeRune(runeTmp[:], r) buf = append(buf, runeTmp[:n]...) continue @@ -90,7 +90,7 @@ func quoteWith(s string, quote byte, ASCIIonly bool) string { // control characters and non-printable characters as defined by // IsPrint. func Quote(s string) string { - return quoteWith(s, '"', false) + return quoteWith(s, '"', false, false) } // AppendQuote appends a double-quoted Go string literal representing s, @@ -103,7 +103,7 @@ func AppendQuote(dst []byte, s string) []byte { // The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for // non-ASCII characters and non-printable characters as defined by IsPrint. func QuoteToASCII(s string) string { - return quoteWith(s, '"', true) + return quoteWith(s, '"', true, false) } // AppendQuoteToASCII appends a double-quoted Go string literal representing s, @@ -112,12 +112,25 @@ func AppendQuoteToASCII(dst []byte, s string) []byte { return append(dst, QuoteToASCII(s)...) } +// QuoteToGraphic returns a double-quoted Go string literal representing s. +// The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) for +// non-ASCII characters and non-printable characters as defined by IsGraphic. +func QuoteToGraphic(s string) string { + return quoteWith(s, '"', false, true) +} + +// AppendQuoteToGraphic appends a double-quoted Go string literal representing s, +// as generated by QuoteToGraphic, to dst and returns the extended buffer. +func AppendQuoteToGraphic(dst []byte, s string) []byte { + return append(dst, QuoteToGraphic(s)...) +} + // QuoteRune returns a single-quoted Go character literal representing the -// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) +// rune. The returned string uses Go escape sequences (\t, \n, \xFF, \u0100) // for control characters and non-printable characters as defined by IsPrint. func QuoteRune(r rune) string { // TODO: avoid the allocation here. - return quoteWith(string(r), '\'', false) + return quoteWith(string(r), '\'', false, false) } // AppendQuoteRune appends a single-quoted Go character literal representing the rune, @@ -127,12 +140,12 @@ func AppendQuoteRune(dst []byte, r rune) []byte { } // QuoteRuneToASCII returns a single-quoted Go character literal representing -// the rune. The returned string uses Go escape sequences (\t, \n, \xFF, +// the rune. The returned string uses Go escape sequences (\t, \n, \xFF, // \u0100) for non-ASCII characters and non-printable characters as defined // by IsPrint. func QuoteRuneToASCII(r rune) string { // TODO: avoid the allocation here. - return quoteWith(string(r), '\'', true) + return quoteWith(string(r), '\'', true, false) } // AppendQuoteRuneToASCII appends a single-quoted Go character literal representing the rune, @@ -141,6 +154,21 @@ func AppendQuoteRuneToASCII(dst []byte, r rune) []byte { return append(dst, QuoteRuneToASCII(r)...) } +// QuoteRuneToGraphic returns a single-quoted Go character literal representing +// the rune. The returned string uses Go escape sequences (\t, \n, \xFF, +// \u0100) for non-ASCII characters and non-printable characters as defined +// by IsGraphic. +func QuoteRuneToGraphic(r rune) string { + // TODO: avoid the allocation here. + return quoteWith(string(r), '\'', false, true) +} + +// AppendQuoteRuneToGraphic appends a single-quoted Go character literal representing the rune, +// as generated by QuoteRuneToGraphic, to dst and returns the extended buffer. +func AppendQuoteRuneToGraphic(dst []byte, r rune) []byte { + return append(dst, QuoteRuneToGraphic(r)...) +} + // CanBackquote reports whether the string s can be represented // unchanged as a single-line backquoted string without control // characters other than tab. @@ -453,3 +481,26 @@ func IsPrint(r rune) bool { j := bsearch16(isNotPrint, uint16(r)) return j >= len(isNotPrint) || isNotPrint[j] != uint16(r) } + +// IsGraphic reports whether the rune is defined as a Graphic by Unicode. Such +// characters include letters, marks, numbers, punctuation, symbols, and +// spaces, from categories L, M, N, P, S, and Zs. +func IsGraphic(r rune) bool { + if IsPrint(r) { + return true + } + return isInGraphicList(r) +} + +// isInGraphicList reports whether the rune is in the isGraphic list. This separation +// from IsGraphic allows quoteWith to avoid two calls to IsPrint. +// Should be called only if IsPrint fails. +func isInGraphicList(r rune) bool { + // We know r must fit in 16 bits - see makeisprint.go. + if r > 0xFFFF { + return false + } + rr := uint16(r) + i := bsearch16(isGraphic, rr) + return i < len(isGraphic) && rr == isGraphic[i] +} |