func dumpInst(b *bytes.Buffer, i *Inst) { switch i.Op { case InstAlt: bw(b, "alt -> ", u32(i.Out), ", ", u32(i.Arg)) case InstAltMatch: bw(b, "altmatch -> ", u32(i.Out), ", ", u32(i.Arg)) case InstCapture: bw(b, "cap ", u32(i.Arg), " -> ", u32(i.Out)) case InstEmptyWidth: bw(b, "empty ", u32(i.Arg), " -> ", u32(i.Out)) case InstMatch: bw(b, "match") case InstFail: bw(b, "fail") case InstNop: bw(b, "nop -> ", u32(i.Out)) case InstRune: if i.Rune == nil { // shouldn't happen bw(b, "rune <nil>") } bw(b, "rune ", strconv.QuoteToASCII(string(i.Rune))) if Flags(i.Arg)&FoldCase != 0 { bw(b, "/i") } bw(b, " -> ", u32(i.Out)) case InstRune1: bw(b, "rune1 ", strconv.QuoteToASCII(string(i.Rune)), " -> ", u32(i.Out)) case InstRuneAny: bw(b, "any -> ", u32(i.Out)) case InstRuneAnyNotNL: bw(b, "anynotnl -> ", u32(i.Out)) } }
func TestMixedEncodings(t *testing.T) { input := testData[0].other + "; " + testData[1].other + "; " + testData[0].other expected := testData[0].utf8 + "; " + testData[1].utf8 + "; " + testData[0].utf8 ic, err := OpenWithFallback(testData[0].otherEncoding, "UTF-8", NEXT_ENC_UNRECOGNIZED) if err != nil { t.Errorf("Error on opening: %s\n", err) return } fallbackic, err := Open(testData[1].otherEncoding, "UTF-8") if err != nil { t.Errorf("Error on opening: %s\n", err) return } ic.SetFallback(fallbackic) b, err := ic.Conv([]byte(input)) if err != nil { t.Errorf("Error on conversion: %s\n", err) return } println(strconv.QuoteToASCII(expected)) println(strconv.QuoteToASCII(string(b))) if string(b) != expected { t.Errorf("mix failed") } ic.Close() }
func (rr *CAA) String() string { s := rr.Hdr.String() + strconv.FormatInt(int64(rr.Flag), 10) + " " + rr.Tag for i, s1 := range rr.Value { if i > 0 { s += " " + strconv.QuoteToASCII(s1) } else { s += strconv.QuoteToASCII(s1) } } return s }
func (rr *SPF) String() string { s := rr.Hdr.String() for i, s1 := range rr.Txt { if i > 0 { s += " " + strconv.QuoteToASCII(s1) } else { s += strconv.QuoteToASCII(s1) } } return s }
func (rr *NINFO) String() string { s := rr.Hdr.String() for i, s1 := range rr.ZSData { if i > 0 { s += " " + strconv.QuoteToASCII(s1) } else { s += strconv.QuoteToASCII(s1) } } return s }
func (rr *URI) String() string { s := rr.Hdr.String() + strconv.Itoa(int(rr.Priority)) + " " + strconv.Itoa(int(rr.Weight)) for i, s1 := range rr.Target { if i > 0 { s += " " + strconv.QuoteToASCII(s1) } else { s += strconv.QuoteToASCII(s1) } } return s }
func TestMoveToFront(t *testing.T) { test := func(buffer []byte) { input, output := make(chan []byte), make(chan []byte, 1) coder := BijectiveBurrowsWheelerCoder(input).MoveToFrontCoder() decoder := BijectiveBurrowsWheelerDecoder(output).MoveToFrontDecoder() input <- buffer close(input) output <- buffer close(output) for out := range coder.Input { for _, symbol := range out { decoder.Output(symbol) } } } for _, v := range TESTS { buffer := make([]byte, len(v)) copy(buffer, []byte(v)) test(buffer) if string(buffer) != v { t.Errorf("inverse should be '%v'; got '%v'", v, strconv.QuoteToASCII(string(buffer))) } } }
func (e entry) printOut(w io.Writer, displayTS bool) error { var err error err = writeHelper(prefix, w, err) if displayTS { err = writeHelper(tsPrefix, w, err) err = writeHelper([]byte(time.Now().String()), w, err) err = writeHelper(tsSuffix, w, err) } err = writeHelper([]byte(e.level.String()), w, err) err = writeHelper(separatorSpace, w, err) err = writeHelper([]byte(e.msg), w, err) if len(e.kv) > 0 { err = writeHelper(separator, w, err) for _, kve := range e.kv { err = writeHelper(space, w, err) err = writeHelper([]byte(kve.K), w, err) err = writeHelper(equals, w, err) vstr := fmt.Sprint(kve.V) // TODO this is only here because logstash is dumb and doesn't // properly handle escaped quotes. Once // https://github.com/elastic/logstash/issues/1645 // gets figured out this Replace can be removed vstr = strings.Replace(vstr, `"`, `'`, -1) vstr = strconv.QuoteToASCII(vstr) err = writeHelper([]byte(vstr), w, err) } } err = writeHelper(newline, w, err) return err }
func embed(variable, wrap, filename string, in io.Reader, out io.Writer) error { _, err := fmt.Fprintf(out, "var %s = %s(asset.init(asset{Name: %q, Content: \"\" +\n", variable, wrap, filename) if err != nil { return err } buf := make([]byte, 1*1024*1024) eof := false for !eof { n, err := in.Read(buf) switch err { case io.EOF: eof = true case nil: default: return err } if n == 0 { continue } s := string(buf[:n]) s = strconv.QuoteToASCII(s) s = "\t" + s + " +\n" if _, err := io.WriteString(out, s); err != nil { return err } } if _, err := fmt.Fprintf(out, "\t\"\"}))\n"); err != nil { return err } return nil }
func TestBurrowsWheeler(t *testing.T) { test := func(input string) { buffer := make([]byte, len(input)) copy(buffer, input) tree := BuildSuffixTree(buffer) bw, sentinel := tree.BurrowsWheelerCoder() index, out_buffer := 0, make([]byte, len(buffer)+1) for b := range bw { out_buffer[index] = b index++ } s := <-sentinel for b, c := out_buffer[s], s+1; c < len(out_buffer); c++ { out_buffer[c], b = b, out_buffer[c] } /*fmt.Println(strconv.QuoteToASCII(string(out_buffer)))*/ original := burrowsWheelerDecoder(out_buffer, s) if bytes.Compare(buffer, original) != 0 { t.Errorf("should be '%v'; got '%v'", input, strconv.QuoteToASCII(string(original))) } } for _, v := range TESTS { test(v) } }
// authPassword records any incoming request trying to auth with a username/password func authPassword(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) { r := &AuthEvent{ Time: fmt.Sprintf("%d", time.Now().Unix()), AuthType: "sshPass", SrcIP: strings.Split(conn.RemoteAddr().String(), ":")[0], DestIP: extIP, User: conn.User(), TypeData: fmt.Sprintf("client-version: %s", strconv.QuoteToASCII(string(conn.ClientVersion()))), Credentials: strconv.QuoteToASCII(string(password)), } addToBatch(r) return nil, errAuthenticationFailed }
// authHandler is called in a goroutine to handle each incoming request func authHandler(w http.ResponseWriter, r *http.Request) { // pull the auth details from the request user, pass, ok := r.BasicAuth() if ok == true { host, _, err := net.SplitHostPort(r.RemoteAddr) if err != nil { host = "" } r := &AuthEvent{ Time: fmt.Sprintf("%d", time.Now().Unix()), AuthType: "httpAuth", SrcIP: host, DestIP: extIP, User: user, Credentials: strconv.QuoteToASCII(pass), } addToBatch(r) } // always return auth fail w.Header().Set("WWW-Authenticate", "Basic realm=Restricted") http.Error(w, "authorization failed", http.StatusUnauthorized) }
// Append (to satisfy the Appender interface) adds a log message to the internal // buffer, and translates the log message into a format that is used by the // remote endpoint. func (apiLgr *APILogger) Append(log *slogger.Log) error { message := strings.TrimRight(log.Message(), "\r\n \t") // MCI-972: ensure message is valid UTF-8 if !utf8.ValidString(message) { message = strconv.QuoteToASCII(message) } logMessage := &model.LogMessage{ Timestamp: log.Timestamp, Severity: levelToString(log.Level), Type: log.Prefix, Version: evergreen.LogmessageCurrentVersion, Message: message, } apiLgr.appendLock.Lock() defer apiLgr.appendLock.Unlock() apiLgr.messages = append(apiLgr.messages, *logMessage) if len(apiLgr.messages) < apiLgr.SendAfterLines || time.Since(apiLgr.lastFlush) < apiLgr.SendAfterDuration { return nil } apiLgr.flushInternal() return nil }
func ExampleQuoteToASCII() { s := strconv.QuoteToASCII(`"Fran & Freddie's Diner ☺"`) fmt.Println(s) // Output: // "\"Fran & Freddie's Diner\t\u263a\"" }
func TestBijectiveBurrowsWheeler(t *testing.T) { input, output := make(chan []byte), make(chan []byte, 2) coder, decoder := BijectiveBurrowsWheelerCoder(input), BijectiveBurrowsWheelerDecoder(output) test := func(buffer []byte) { for c := 0; c < repeated; c++ { input <- buffer <-coder.Input } in := make([]byte, len(buffer)) for c := 0; c < repeated; c++ { output <- in output <- nil for _, i := range buffer { decoder.Output(i) } copy(buffer, in) } } for _, v := range TESTS { buffer := make([]byte, len(v)) copy(buffer, []byte(v)) test(buffer) if string(buffer) != v { t.Errorf("should be '%v'; got '%v'", v, strconv.QuoteToASCII(string(buffer))) } } close(input) close(output) }
// doTest performs a single test f(input) and verifies that the output matches // out and that the returned error is expected. The errors string contains // all allowed error codes as categorized in // http://www.unicode.org/Public/idna/9.0.0/IdnaTest.txt: // P: Processing // V: Validity // A: to ASCII // B: Bidi // C: Context J func doTest(t *testing.T, f func(string) (string, error), name, input, want, errors string) { errors = strings.Trim(errors, "[]") test := "ok" if errors != "" { test = "err:" + errors } // Replace some of the escape sequences to make it easier to single out // tests on the command name. in := strings.Trim(strconv.QuoteToASCII(input), `"`) in = strings.Replace(in, `\u`, "#", -1) in = strings.Replace(in, `\U`, "#", -1) name = fmt.Sprintf("%s/%s/%s", name, in, test) testtext.Run(t, name, func(t *testing.T) { got, err := f(input) if err != nil { code := err.(interface { code() string }).code() if strings.Index(errors, code) == -1 { t.Errorf("error %q not in set of expected errors {%v}", code, errors) } } else if errors != "" { t.Errorf("no errors; want error in {%v}", errors) } if want != "" && got != want { t.Errorf(`string: got %+q; want %+q`, got, want) } }) }
func (e *ParseError) Error() (s string) { if e.file != "" { s = e.file + ": " } s += "dns: " + e.err + ": " + strconv.QuoteToASCII(e.lex.token) + " at line: " + strconv.Itoa(e.lex.line) + ":" + strconv.Itoa(e.lex.column) return }
func parseLangFile(filePath string, dstFileName string) { b, err := ioutil.ReadFile(filePath) if err != nil { fmt.Println(err) } r := bufio.NewReader(strings.NewReader(string(b))) var lineNum int = 0 for err == nil { isP := true line := []byte{} for isP { line, isP, err = r.ReadLine() lineNum = lineNum + 1 if err != nil && err.Error() != "EOF" { panic("Can't read") } if strings.Contains(string(line), "__") && string(line[:2]) != "//" { re, _ := regexp.Compile("__\\(\\s*(\\'|\")(.*?)(\\'|\")\\s*\\)") submatchall := re.FindAllSubmatch(line, -1) if len(submatchall) >= 1 { for _, submatch := range submatchall { item := []byte{} if len(submatch) > 2 { if _, eok := exportSrcFileContent[string(submatch[2])]; !eok { if debug == true { fileI := fmt.Sprintf("line:%d %s", lineNum, filePath) item = append(item, []byte(fileI)...) item = append(item, []byte(",")...) item = append(item, line...) item = append(item, []byte(",")...) } item = append(item, submatch[2]...) item = append(item, []byte(",")...) item = append(item, submatch[2]...) item = append(item, []byte("\n")...) keyStr := strconv.QuoteToASCII(string(submatch[2])) if _, ok := maps[keyStr]; !ok && string(submatch[2]) != "" { maps[keyStr] = 1 exportTable = append(exportTable, item...) totalI = totalI + 1 } } } } } } } } }
// authPassword records any incoming request trying to auth with a username/password func authPassword(conn ssh.ConnMetadata, password []byte) (*ssh.Permissions, error) { log.Printf("sshPass: %s %s %s\n", conn.RemoteAddr().String(), conn.User(), strconv.QuoteToASCII(string(password))) return nil, errAuthenticationFailed }
// make sure we don't generate invalid headers func sanitize(input string) string { toAscii := strconv.QuoteToASCII(input) var b bytes.Buffer for _, i := range toAscii { if validHeaderFieldByte(byte(i)) { b.WriteRune(i) } } return b.String() }
// Inspect prints a reflect.Value the way you would enter it. // Some like this should really be part of the reflect package. func Inspect(val reflect.Value) string { if val.CanInterface() && val.Interface() == nil { return "nil" } switch val.Kind() { case reflect.String: return strconv.QuoteToASCII(val.String()) case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Complex64, reflect.Complex128: if val.CanInterface() { fmt.Sprintf("%v", val.Interface()) } else { fmt.Sprintf("<%v>", val.Type()) } case reflect.Slice, reflect.Array: sep := "{" str := "" for i := 0; i < val.Len(); i++ { str += sep sep = ", " str += Inspect(val.Index(i)) } str += "}" return str case reflect.Struct: str := "{" n := val.NumField() for i := 0; i < n; i++ { if i > 0 { str += " " } name := val.Type().Field(i).Name field := val.Field(i) str += fmt.Sprintf("%s: %s,", name, Inspect(field)) } str += "}" return str case reflect.Ptr: if val.IsNil() { return "nil" } else { return "&" + Inspect(reflect.Indirect(val)) } } // FIXME: add more Kinds as folks are annoyed with the output of // the below: if val.CanInterface() { return fmt.Sprintf("%v", val.Interface()) } else { return fmt.Sprintf("<%v>", val.Type()) } }
func dumpInst(b *bytes.Buffer, i *syntax.Inst) { switch i.Op { case syntax.InstAlt: bw(b, "alt -> ", u32(i.Out), ", ", u32(i.Arg)) case syntax.InstAltMatch: bw(b, "altmatch -> ", u32(i.Out), ", ", u32(i.Arg)) case syntax.InstCapture: bw(b, "cap ", u32(i.Arg), " -> ", u32(i.Out)) case syntax.InstEmptyWidth: bw(b, "empty ", u32(i.Arg), " -> ", u32(i.Out)) case syntax.InstMatch: bw(b, "match") case syntax.InstFail: bw(b, "fail") case syntax.InstNop: bw(b, "nop -> ", u32(i.Out)) case instByteRange: fmt.Fprintf(b, "byte %02x-%02x", (i.Arg>>8)&0xFF, i.Arg&0xFF) if i.Arg&argFold != 0 { bw(b, "/i") } bw(b, " -> ", u32(i.Out)) // Should not happen case syntax.InstRune: if i.Rune == nil { // shouldn't happen bw(b, "rune <nil>") } bw(b, "rune ", strconv.QuoteToASCII(string(i.Rune))) if syntax.Flags(i.Arg)&syntax.FoldCase != 0 { bw(b, "/i") } bw(b, " -> ", u32(i.Out)) case syntax.InstRune1: bw(b, "rune1 ", strconv.QuoteToASCII(string(i.Rune)), " -> ", u32(i.Out)) case syntax.InstRuneAny: bw(b, "any -> ", u32(i.Out)) case syntax.InstRuneAnyNotNL: bw(b, "anynotnl -> ", u32(i.Out)) } }
func (p DefPath) symbolPath() graph.SymbolPath { p.Path = strconv.QuoteToASCII(p.Path) p.Path = p.Path[1 : len(p.Path)-1] if p.Module == "" { return graph.SymbolPath(fmt.Sprintf("%s/-/%s", p.Namespace, p.Path)) } else if p.Path == "" { return graph.SymbolPath(fmt.Sprintf("%s/%s", p.Namespace, p.Module)) } else { return graph.SymbolPath(fmt.Sprintf("%s/%s/-/%s", p.Namespace, p.Module, strings.Replace(p.Path, ".", "/", -1))) } }
func updateTwitterWindow(win *Window, tweets []an.Tweet, feedList []FeedItem) { for { UpdatePanels() tweets = updateTimeline(api) win.Erase() win.NoutRefresh() for i := 0; i < len(tweets); i++ { t, _ := time.Parse(time.RubyDate, tweets[i].CreatedAt) win.ColorOn(2) _, my := win.MaxYX() lineLength := 1 if my > 40 { win.Print(t.Format(" 15:04") + " ") lineLength = 7 } win.ColorOff(2) win.ColorOn(3) win.AttrOn(A_BOLD) padding := 8 - len(tweets[i].User.ScreenName) for i := 0; i < padding; i++ { win.Print(" ") } if len(tweets[i].User.ScreenName) > 8 { win.Print(tweets[i].User.ScreenName[:8]) lineLength += 8 } else { win.Print(tweets[i].User.ScreenName) lineLength += len(tweets[i].User.ScreenName) } win.AttrOff(A_BOLD) win.ColorOff(3) win.ColorOn(4) win.Print(" | ") lineLength += 3 win.ColorOff(4) UseDefaultColors() text := strconv.QuoteToASCII(tweets[i].Text) var newFeed FeedItem newFeed.Body = text newFeed.Name = tweets[i].User.ScreenName newFeed.Read = false newFeed.Time = t feedList = append(feedList, newFeed) if len(text) > my-lineLength-7 { win.Println(text[:my-lineLength-7] + "...") } else { win.Println(text) } } win.NoutRefresh() Update() time.Sleep(10 * time.Second) } }
func (e *SyntaxError) Error() string { if e.Got == token.Error { text := strconv.QuoteToASCII(e.Text) return fmt.Sprintf("%s: unexpected rune in input [%s]", e.Start, text[1:len(text)-1]) } switch e.Got { case token.Semicolon: e.Text = ";" case token.EOF: e.Text = "EOF" } return fmt.Sprintf("%s: expected %s, got %s", e.Start, e.Wanted, e.Text) }
// fmt_q formats a string as a double-quoted, escaped Go string constant. func (f *fmt) fmt_q(s string) { s = f.truncate(s) var quoted string if f.sharp && strconv.CanBackquote(s) { quoted = "`" + s + "`" } else { if f.plus { quoted = strconv.QuoteToASCII(s) } else { quoted = strconv.Quote(s) } } f.padString(quoted) }
// Error wraps http.Error by writing appropriate error messages // depending on the handler's Content-Type. // // For example, with the Content-Type set to application/json // and the error string "oops!", the response body would be // `{"error":"oops!"}` func Error(w http.ResponseWriter, err string, code int) { if rw, ok := w.(*ResponseWriter); ok { switch rw.ContentType { case "application/json": err = fmt.Sprintf(`{"error":%s,"status":%d}`, strconv.QuoteToASCII(err), code) case "text/html": err = html.EscapeString(err) case "text/plain": fallthrough default: err = "error: " + err } } http.Error(w, err, code) }
func applyContentDispositionHdr(w http.ResponseWriter, r *http.Request, filename string, isAttachment bool) { disposition := "inline" if isAttachment { disposition = "attachment" } if strings.ContainsRune(r.RequestURI, '?') { // Help the UA realize that the filename is just // "filename.txt", not // "filename.txt?disposition=attachment". // // TODO(TC): Follow advice at RFC 6266 appendix D disposition += "; filename=" + strconv.QuoteToASCII(filename) } if disposition != "inline" { w.Header().Set("Content-Disposition", disposition) } }
// AddToSearchIndexTask is the implementation of the task described above. // This adds an item to the search index. func AddToSearchIndexTask(con *data.Context, searchItem *Item, namespace string, URL string) error { index, err := search.Open("items_" + namespace) if err != nil { con.Log.Errorf("Error while opening the item index %v", err) return err } searchItem.HTMLforSearch = search.HTML(optimizeSearchInput(string(searchItem.HTMLforSearch))) searchItem.Description = optimizeSearchInput(searchItem.Description) _, err = index.Put(con.C, strconv.QuoteToASCII(URL), searchItem) if err != nil { con.Log.Errorf("Error while puting the search item in the index %v", err) return err } return nil }
// authKey records any incoming request trying to auth with an ssh key func authKey(conn ssh.ConnMetadata, key ssh.PublicKey) (*ssh.Permissions, error) { r := &AuthEvent{ Time: fmt.Sprintf("%d", time.Now().Unix()), AuthType: "sshKey", SrcIP: strings.Split(conn.RemoteAddr().String(), ":")[0], DestIP: extIP, User: conn.User(), TypeData: fmt.Sprintf("ssh-key-type: %s client-version: %s", key.Type(), strconv.QuoteToASCII(string(conn.ClientVersion()))), } h := sha256.New() h.Write(key.Marshal()) r.Credentials = base64.StdEncoding.EncodeToString(h.Sum(nil)) addToBatch(r) return nil, errAuthenticationFailed }