func sortQuery(u *url.URL) { q := u.Query() if len(q) == 0 { return } arKeys := make([]string, len(q)) i := 0 for k, _ := range q { arKeys[i] = k i++ } sort.Strings(arKeys) buf := new(bytes.Buffer) for _, k := range arKeys { sort.Strings(q[k]) for _, v := range q[k] { if buf.Len() > 0 { buf.WriteRune('&') } buf.WriteString(fmt.Sprintf("%s=%s", k, url.QueryEscape(v))) } } // Rebuild the raw query string u.RawQuery = buf.String() }
func escapeShell(s string) string { i := strings.IndexAny(s, "$`!\\\"") if i < 0 { return s } var buf bytes.Buffer var lastDollar bool for _, c := range s { switch c { case '$': if lastDollar { buf.WriteRune(c) lastDollar = false continue } buf.WriteString(`\$`) lastDollar = true continue case '`', '"', '!', '\\': buf.WriteByte('\\') } buf.WriteRune(c) lastDollar = false } return buf.String() }
// EvalSymlinks returns the path name after the evaluation of any symbolic // links. // If path is relative it will be evaluated relative to the current directory. func EvalSymlinks(path string) (string, os.Error) { if runtime.GOOS == "windows" { // Symlinks are not supported under windows. _, err := os.Lstat(path) if err != nil { return "", err } return Clean(path), nil } const maxIter = 255 originalPath := path // consume path by taking each frontmost path element, // expanding it if it's a symlink, and appending it to b var b bytes.Buffer for n := 0; path != ""; n++ { if n > maxIter { return "", os.NewError("EvalSymlinks: too many links in " + originalPath) } // find next path component, p i := strings.IndexRune(path, Separator) var p string if i == -1 { p, path = path, "" } else { p, path = path[:i], path[i+1:] } if p == "" { if b.Len() == 0 { // must be absolute path b.WriteRune(Separator) } continue } fi, err := os.Lstat(b.String() + p) if err != nil { return "", err } if !fi.IsSymlink() { b.WriteString(p) if path != "" { b.WriteRune(Separator) } continue } // it's a symlink, put it at the front of path dest, err := os.Readlink(b.String() + p) if err != nil { return "", err } if IsAbs(dest) { b.Reset() } path = dest + string(Separator) + path } return Clean(b.String()), nil }
// scanWhitespace consumes the current rune and all contiguous whitespace. func (s *Scanner) scanWhitespace() (tok Token, lit string) { // Create a buffer and read the current character into it. var buf bytes.Buffer buf.WriteRune(s.read()) // Read every subsequent whitespace character into the buffer. // Non-whitespace characters and EOF will cause the loop to exit. for { if ch := s.read(); ch == eof { break } else if !isWhitespace(ch) { s.unread() break } else { buf.WriteRune(ch) } } return WS, buf.String() }
// toFold returns a string with the property that // strings.EqualFold(s, t) iff toFold(s) == toFold(t) // This lets us test a large set of strings for fold-equivalent // duplicates without making a quadratic number of calls // to EqualFold. Note that strings.ToUpper and strings.ToLower // have the desired property in some corner cases. func toFold(s string) string { // Fast path: all ASCII, no upper case. // Most paths look like this already. for i := 0; i < len(s); i++ { c := s[i] if c >= utf8.RuneSelf || 'A' <= c && c <= 'Z' { goto Slow } } return s Slow: var buf bytes.Buffer for _, r := range s { // SimpleFold(x) cycles to the next equivalent rune > x // or wraps around to smaller values. Iterate until it wraps, // and we've found the minimum value. for { r0 := r r = unicode.SimpleFold(r0) if r <= r0 { break } } // Exception to allow fast path above: A-Z => a-z if 'A' <= r && r <= 'Z' { r += 'a' - 'A' } buf.WriteRune(r) } return buf.String() }
func (d *WordDecoder) convert(buf *bytes.Buffer, charset string, content []byte) error { switch { case strings.EqualFold("utf-8", charset): buf.Write(content) case strings.EqualFold("iso-8859-1", charset): for _, c := range content { buf.WriteRune(rune(c)) } case strings.EqualFold("us-ascii", charset): for _, c := range content { if c >= utf8.RuneSelf { buf.WriteRune(unicode.ReplacementChar) } else { buf.WriteByte(c) } } default: if d.CharsetReader == nil { return fmt.Errorf("mime: unhandled charset %q", charset) } r, err := d.CharsetReader(strings.ToLower(charset), bytes.NewReader(content)) if err != nil { return err } if _, err = buf.ReadFrom(r); err != nil { return err } } return nil }
func ReplaceVariableShorthands(text string, chr byte, name string) string { repl := fmt.Sprintf("$%s.", name) var buf bytes.Buffer buf.Grow(len(text)) cmd := false quoted := false runes := []rune(text) for ii, v := range runes { if byte(v) == chr && cmd && !quoted { buf.WriteString(repl) continue } if cmd && v == '}' { cmd = ii < len(runes)-1 && runes[ii+1] != '}' } else if !cmd && v == '{' { cmd = ii < len(runes)-1 && runes[ii+1] == '{' } if cmd { if v == '"' && (ii == 0 || runes[ii-1] != '\\') { quoted = !quoted } } buf.WriteRune(v) } return buf.String() }
// decode transforms punycode input bytes (that represent DNS label) into Unicode bytestream func decode(b []byte) []byte { src := b // b would move and we need to keep it n, bias := _N, _BIAS if !bytes.HasPrefix(b, []byte(_PREFIX)) { return b } out := make([]rune, 0, len(b)) b = b[len(_PREFIX):] for pos := len(b) - 1; pos >= 0; pos-- { // only last delimiter is our interest if b[pos] == _DELIMITER { out = append(out, bytes.Runes(b[:pos])...) b = b[pos+1:] // trim source string break } } if len(b) == 0 { return src } var ( i, oldi, w rune ch byte t, digit rune ln int ) for i = 0; len(b) > 0; i++ { oldi, w = i, 1 for k := _BASE; len(b) > 0; k += _BASE { ch, b = b[0], b[1:] digit = digitval(rune(ch)) if digit == errdigit { return src } i += digit * w t = tfunc(k, bias) if digit < t { break } w *= _BASE - t } ln = len(out) + 1 bias = adapt(i-oldi, ln, oldi == 0) n += i / rune(ln) i = i % rune(ln) // insert out = append(out, 0) copy(out[i+1:], out[i:]) out[i] = n } var ret bytes.Buffer for _, r := range out { ret.WriteRune(r) } return ret.Bytes() }
func TestIsEnvEditValid(t *testing.T) { v := "VAR1=VAL1" if err := isEnvEditValid(v); err != nil { t.Fatal(v, "should be valid, got", err) } vs := []string{"VAR1=", "=VAL1", "VAR"} for _, v = range vs { if err := isEnvEditValid(v); err == nil { t.Fatal(v, "should not be valid") } else if err != setInvalidSyntaxError { t.Fatal("expected", setInvalidSyntaxError, "error, got", err) } } vs = []string{"VA R=VAL", " VAR=VAL", " VAR=VAL", "%%%=VAL"} for _, v = range vs { if err := isEnvEditValid(v); err == nil { t.Fatal(v, "should not be valid") } else if err != invalidNameFormatError { t.Fatal("expected", invalidNameFormatError, "error, got", err) } } longName := new(bytes.Buffer) for i := 0; i < scalingo.EnvNameMaxLength; i++ { longName.WriteRune('A') } longName.WriteString("A=VAL") if err := isEnvEditValid(longName.String()); err == nil { t.Fatal(v, "should not be valid") } else if err != nameTooLongError { t.Fatal("expected", nameTooLongError, "error, got", err) }
func (r *Reader) readUnquotedField() (string, error) { // TODO: Use bytes.Buffer s := bytes.Buffer{} for { char, _, err := r.r.ReadRune() if err != nil || char == r.opts.Delimiter { // TODO Can a non quoted string be escaped? In that case, it should be // handled here. Should probably have a look at how Python's csv module // is handling this. // Putting it back for the outer loop to read separators. This makes more // compatible with readQuotedField(). r.r.UnreadRune(char) return s.String(), err } else { s.WriteRune(char) } if ok, _ := r.nextIsLineTerminator(); ok { return s.String(), nil } } // Required by Go 1.0 to compile. Unreachable code. return s.String(), nil }
func methodQtSignature(method reflect.Method) (signature, result string) { var buf bytes.Buffer for i, rune := range method.Name { if i == 0 { buf.WriteRune(unicode.ToLower(rune)) } else { buf.WriteString(method.Name[i:]) break } } buf.WriteByte('(') n := method.Type.NumIn() for i := 1; i < n; i++ { if i > 1 { buf.WriteByte(',') } buf.WriteString("QVariant") } buf.WriteByte(')') signature = buf.String() switch method.Type.NumOut() { case 0: // keep it as "" case 1: result = "QVariant" default: result = "QVariantList" } return }
// StripHTML returns s without HTML tags. It is fairly naive // but works with most valid HTML inputs. func (c Context) StripHTML(s string) string { var buf bytes.Buffer var inTag, inQuotes bool var tagStart int for i, ch := range s { if inTag { if ch == '>' && !inQuotes { inTag = false } else if ch == '<' && !inQuotes { // false start buf.WriteString(s[tagStart:i]) tagStart = i } else if ch == '"' { inQuotes = !inQuotes } continue } if ch == '<' { inTag = true tagStart = i continue } buf.WriteRune(ch) } if inTag { // false start buf.WriteString(s[tagStart:]) inTag = false } return buf.String() }
func (p *PrimaryLexer) scanString() { p.Consume() buf := new(bytes.Buffer) for p.Peek() != '"' { ch := p.Consume() if ch == eof { p.Emit(TokenError, "Unexpected eof while parsing string. Start "+p.Mark().String()+", end "+p.Position().String()) } if ch == '\\' { escapee := p.Consume() if escapee == eof { p.Emit(TokenError, "Unexpected eof while parsing escape sequence in string. Start "+p.Mark().String()+", end "+p.Position().String()) return } ch, ok := EscapedCharacters[escapee] if !ok { p.Emit(TokenError, "Invalid escape sequence at "+p.Position().String()) } buf.WriteRune(ch) continue } if ch != '"' { buf.WriteRune(ch) } } p.Consume() p.Emit(TokenString, buf.String()) }
// processString is utilized by DecodeHDU to process string-type values in the header // it uses a 3-state machine to process double single quotes func processString(s string) (string, int, error) { var buf bytes.Buffer state := 0 for i, char := range s { quote := (char == '\'') switch state { case 0: if !quote { return "", i, fmt.Errorf("fitsio: string does not start with a quote (%q)", s) } state = 1 case 1: if quote { state = 2 } else { buf.WriteRune(char) state = 1 } case 2: if quote { buf.WriteRune(char) state = 1 } else { return strings.TrimRight(buf.String(), " "), i, nil } } } if s[len(s)-1] == '\'' { return strings.TrimRight(buf.String(), " "), len(s), nil } return "", 0, fmt.Errorf("fitsio: string ends prematurely (%q)", s) }
func createId(hash string) string { var uuid bytes.Buffer for i := 0; i < len(hash); i += 2 { uuid.WriteRune(rune(hash[i])) } return uuid.String() }
// benchInsertVaryingLengthBuilder builds an INSERT statement with varying // length data. func benchInsertVaryingLengthBuilder(maxBytes, maxText int) (*dat.InsertBuilder, error) { var columns []string var values []interface{} if maxBytes > 0 { image := make([]byte, maxBytes) for i := 0; i < maxBytes; i++ { image[i] = byte(i % 256) } columns = append(columns, "image") values = append(values, image) } if maxText > 0 { var buf bytes.Buffer for i := 0; i < maxText; i++ { if i > 0 && i%1024 == 0 { // force escaping buf.WriteRune('\'') } else { buf.WriteRune('t') } } columns = append(columns, "name") values = append(values, buf.String()) } builder := dat. NewInsertBuilder("benches"). Columns(columns...). Values(values...) return builder, nil }
func (x *parserLex) lexString(yylval *parserSymType, quoted bool) (int, bool) { var b bytes.Buffer terminated := false for { c := x.next() if c == lexEOF { if quoted { x.Error("unterminated string") } break } // Behavior is a bit different if we're lexing within a quoted string. if quoted { // If its a double quote, we've reached the end of the string if c == '"' { terminated = true break } // Let's check to see if we're escaping anything. if c == '\\' { switch n := x.next(); n { case '\\', '"': c = n case 'n': c = '\n' default: x.backup() } } } // If we hit a dollar sign, then check if we're starting // another interpolation. If so, then we're done. if c == '$' { n := x.peek() // If it is '{', then we're starting another interpolation if n == '{' { x.backup() break } // If it is '$', then we're escaping a dollar sign if n == '$' { x.next() } } if _, err := b.WriteRune(c); err != nil { x.Error(err.Error()) return lexEOF, false } } yylval.token = &parserToken{Value: b.String()} return STRING, terminated }
// Resize resizes the string with the given length. It ellipses with '...' when the string's length exceeds // the desired length or pads spaces to the right of the string when length is smaller than desired func Resize(s string, length uint) string { slen := runewidth.StringWidth(s) n := int(length) if slen == n { return s } // Pads only when length of the string smaller than len needed s = PadRight(s, n, ' ') if slen > n { rs := []rune(s) var buf bytes.Buffer w := 0 for _, r := range rs { buf.WriteRune(r) rw := runewidth.RuneWidth(r) if w+rw >= n-3 { break } w += rw } buf.WriteString("...") s = buf.String() } return s }
// platformReload updates configuration with platform specific options func (daemon *Daemon) platformReload(config *Config) map[string]string { if config.IsValueSet("runtimes") { daemon.configStore.Runtimes = config.Runtimes // Always set the default one daemon.configStore.Runtimes[stockRuntimeName] = types.Runtime{Path: DefaultRuntimeBinary} } if config.DefaultRuntime != "" { daemon.configStore.DefaultRuntime = config.DefaultRuntime } // Update attributes var runtimeList bytes.Buffer for name, rt := range daemon.configStore.Runtimes { if runtimeList.Len() > 0 { runtimeList.WriteRune(' ') } runtimeList.WriteString(fmt.Sprintf("%s:%s", name, rt)) } return map[string]string{ "runtimes": runtimeList.String(), "default-runtime": daemon.configStore.DefaultRuntime, } }
func (l *Lexer) Arith() { // Upon entering this state we have read the '$((' l.buffer.WriteRune(SentinalSubstitution) arithBuf := bytes.Buffer{} sa := SubArith{} parenCount := 0 for { c := l.nextChar() if parenCount == 0 && c == ')' { if l.hasNext(')') { break } // Bash just ignores a closing brakcet with no opening // bracket so we will emulate that. continue } if c == '(' { parenCount++ } if c == ')' { parenCount-- } arithBuf.WriteRune(c) } sa.Raw = arithBuf.String() l.subs = append(l.subs, sa) }
func wrap(text string, length int) string { var buf bytes.Buffer var last rune var lastNL bool var col int for _, c := range text { switch c { case '\r': // ignore this continue // and also don't track `last` case '\n': // ignore this too, but reset col if col >= length || last == '\n' { buf.WriteString("\n\n") } col = 0 case ' ', '\t': // opportunity to split if col >= length { buf.WriteByte('\n') col = 0 } else { if !lastNL { buf.WriteRune(c) } col++ // count column } default: buf.WriteRune(c) col++ } lastNL = c == '\n' last = c } return buf.String() }
// UPDATE keyspace.Movies SET col1 = val1, col2 = val2 func updateStatement(kn, cfName string, fields map[string]interface{}, opts Options) (string, []interface{}) { buf := new(bytes.Buffer) buf.WriteString(fmt.Sprintf("UPDATE %s.%s ", kn, cfName)) // Apply options if opts.TTL != 0 { buf.WriteString("USING TTL ") buf.WriteString(strconv.FormatFloat(opts.TTL.Seconds(), 'f', 0, 64)) buf.WriteRune(' ') } buf.WriteString("SET ") i := 0 ret := []interface{}{} for k, v := range fields { if i > 0 { buf.WriteString(", ") } if mod, ok := v.(Modifier); ok { stmt, vals := mod.cql(k) buf.WriteString(stmt) ret = append(ret, vals...) } else { buf.WriteString(k + " = ?") ret = append(ret, v) } i++ } return buf.String(), ret }
// ToEnvVarName converts a name into a safe valid environment variable name. // - All characters are converted to upper case. // - All accents are removed, for example é is replaced by e // - Any whitespace is replaced by underscore '_'. // - The safe name never contains two consecutive underscores '_'. Looks better with only one. // - Digits 0-9 are allowed. // - Letters A-Z are allowed. // - Underscore '_' is allowed. // - All other characters are removed. // // References: // - http://stackoverflow.com/questions/2821043/allowed-characters-in-linux-environment-variable-names // - https://blog.golang.org/normalization func ToEnvVarName(name string) string { buf := new(bytes.Buffer) name = norm.NFKD.String(name) name = strings.ToUpper(name) var lastRune rune for _, r := range name { var writeRune rune if unicode.IsSpace(r) { writeRune = '_' } else if ((r >= 'A') && (r <= 'Z')) || (r == '_') { writeRune = r } else if (buf.Len() > 0) && (r >= '0' && r <= '9') { // var must not start with digit writeRune = r } // We don't want to write two consecutive '_' underscores. Looks better with only one. if (writeRune > 0) && ((writeRune != '_') || (lastRune != '_')) { buf.WriteRune(writeRune) lastRune = writeRune } } // Trim underscores from start and end. varName := strings.Trim(buf.String(), "_") return varName }
func (id *IndexDefinition) String() string { ret := bytes.Buffer{} wr := func(r rune) { _, err := ret.WriteRune(r) if err != nil { panic(err) } } ws := func(s string) { _, err := ret.WriteString(s) if err != nil { panic(err) } } if id.Builtin() { wr('B') } else { wr('C') } wr(':') ws(id.Kind) if id.Ancestor { ws("|A") } for _, sb := range id.SortBy { wr('/') if sb.Descending { wr('-') } ws(sb.Property) } return ret.String() }
func (s *ESAPIV0) Bulk(data *bytes.Buffer) { if data == nil || data.Len() == 0 { return } data.WriteRune('\n') url := fmt.Sprintf("%s/_bulk", s.Host) client := &http.Client{} reqest, _ := http.NewRequest("POST", url, data) if s.Auth != nil { reqest.SetBasicAuth(s.Auth.User, s.Auth.Pass) } resp, errs := client.Do(reqest) if errs != nil { log.Error(errs) return } body, err := ioutil.ReadAll(resp.Body) if err != nil { log.Error(err) return } log.Trace(url, string(body)) defer resp.Body.Close() defer data.Reset() if resp.StatusCode != 200 { log.Errorf("bad bulk response: %s %s", body, resp.StatusCode) return } }
func (this Buffer) String() string { var result bytes.Buffer for i := 0; i < this.Length; i++ { result.WriteRune(this.Buffer[i]) } return result.String() }
// scanIdent consumes the current rune and all contiguous ident runes. func (s *Scanner) scanIdent() (tok Token, lit string) { // Create a buffer and read the current character into it. var buf bytes.Buffer buf.WriteRune(s.read()) // Read every subsequent ident character into the buffer. // Non-ident characters and EOF will cause the loop to exit. for { if ch := s.read(); ch == eof { break } else if !isLetter(ch) && !isDigit(ch) && ch != '_' { s.unread() break } else { _, _ = buf.WriteRune(ch) } } // If the string matches a keyword then return that keyword. switch strings.ToUpper(buf.String()) { case "SELECT": return SELECT, buf.String() case "FROM": return FROM, buf.String() } // Otherwise return as a regular identifier. return IDENT, buf.String() }
// expandEnv expands any $XXX invocations in word. func (g *Generator) expandEnv(word string) string { if !strings.ContainsRune(word, '$') { return word } var buf bytes.Buffer var w int var r rune for i := 0; i < len(word); i += w { r, w = utf8.DecodeRuneInString(word[i:]) if r != '$' { buf.WriteRune(r) continue } w += g.identLength(word[i+w:]) envVar := word[i+1 : i+w] var sub string switch envVar { case "GOARCH": sub = runtime.GOARCH case "GOOS": sub = runtime.GOOS case "GOFILE": sub = g.file case "GOPACKAGE": sub = g.pkg default: sub = os.Getenv(envVar) } buf.WriteString(sub) } return buf.String() }
func saveDraw(w *draw.Window, statusCh chan string) { var buffer bytes.Buffer dir, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { panic(err) } buffer.WriteString(dir) statusCh <- fmt.Sprintf("Choose a Filename: %s", buffer.String()) for { ev := termbox.PollEvent() switch ev.Key { case termbox.KeyEnter: statusCh <- "Saved" return case termbox.KeyBackspace: if buffer.Len() > 0 { buffer.Truncate(buffer.Len() - 1) } default: buffer.WriteRune(ev.Ch) } statusCh <- fmt.Sprintf("Choose a Filename: %s", buffer.String()) } }
func (self *Lexer) readNumber() (int, Token, string) { self.pos = self.last // Keep this token position var sb bytes.Buffer has_dot := false for { r, _ := self.peek() if unicode.IsDigit(r) { self.skip() sb.WriteRune(r) } else if r == '.' { if has_dot { return self.illegal("Bad floating number literal") } else { has_dot = true } self.skip() sb.WriteRune(r) } else if unicode.IsLetter(r) { return self.illegal("Bad floating number literal") } else { break } } if has_dot { return self.pos, FLOAT_LITERAL, sb.String() } else { return self.pos, INT_LITERAL, sb.String() } }