~nilium/goquote

9aa1d7be02c4dbcfd1162c264ffa0cef41c3124a — Noel Cower 4 years ago bb2ccd5 v0.1.1
Add ql, qla format options

These can be used to produce multiple lines of strings that are
concatenated together.
1 files changed, 28 insertions(+), 1 deletions(-)

M goquote.go
M goquote.go => goquote.go +28 -1
@@ 16,6 16,7 @@ import (
	"log"
	"os"
	"strconv"
	"strings"
)

func usage() {


@@ 28,7 29,11 @@ MODE may be one of the following to change quote behavior:
  q   - Quoted string (default)
        "string"
  qa  - Quoted ASCII string
        "string\tescaped"
        "string\n\tescaped"
  ql  - Quoted multi-line ASCII string.
        "string" +
	    "\tescaped"
  qla - Same as ql, but with ASCII string formatting.
  ra  - Backquoted single-line ASCII string
        `+"`string`"+`
  r   - Backquoted single-line string


@@ 85,6 90,28 @@ loop:
		buf.WriteByte('`')
		buf.Write(b)
		buf.WriteByte('`')
	case "ql", "qla":
		quotefn := strconv.Quote
		fallback := "q"
		if mode == "qla" {
			quotefn = strconv.QuoteToASCII
			fallback = "qa"
		}
		lines := strings.Split(string(b), "\n")
		if len(lines) <= 1 {
			mode = fallback
			goto loop
		}
		lead := ""
		for i, line := range lines {
			line = quotefn(line)
			buf.WriteString(lead)
			buf.WriteString(line)
			if i < len(lines)-1 {
				buf.WriteString(" +\n")
			}
			lead = "\t"
		}
	case "x":
		buf.WriteByte('"')
		for _, c := range b {