Thanks for confirming. I wrote that function and erased a good bit of the overhead.
bytes.Buffer for the no-escapes path and strconv.Quote otherwise. On Wed, Oct 13, 2021, 8:49 PM Ian Lance Taylor <i...@golang.org> wrote: > On Wed, Oct 13, 2021 at 3:46 PM 'Tim Hockin' via golang-nuts > <golang-nuts@googlegroups.com> wrote: > > > > Is there any ready-built function that can tell me whether > `strconv.Quote()` would produce a different string than its input, without > actually running it? Or is there a clearly documented set of rules one > could use to test each rune in a string? > > > > I am trying to avoid allocations, and MOST of the inputs will be safe > (but not all). Calling strconv.Quote() has a measurable impact, so I'd > avoid it if I could... > > > > Does such an animal exist? > > To answer your exact question, strconv.Quote always allocates a new > string, because it always adds quotation marks around the returned > string. That is, the output of strconv.Quote("a") is `"a"`, with > literal quotation characters. > > Other than calling strconv.Quote will replace any rune for which > strconv.IsPrint returns false, and it will also replace `\` and `"`. > But I don't think there is a function to check that. > > Ian > -- 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. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAO_RewYJUMFz36u%2B%2B%3DHp3-Dqi33jhjgadehE6KbC1eaakMvpzg%40mail.gmail.com.