// Handle state after meeting one '@' func compileColorSyntax(input, output *bytes.Buffer) { i, _, err := input.ReadRune() if err != nil { // EOF got log.Fatal("Parse failed on color syntax") } switch i { default: output.WriteString(colorMap(string(i))) case '{': color := bytes.NewBufferString("") for { i, _, err := input.ReadRune() if err != nil { log.Fatal("Parse failed on color syntax") } if i == '}' { break } color.WriteRune(i) } output.WriteString(colorMap(color.String())) case escapeChar: output.WriteRune(escapeChar) } }
//SD-NAME = 1*32PRINTUSASCII ; except '=', SP, ']', %d34 (") func parseSDName(buf *bytes.Buffer, delim rune) (string, error) { var count int slice := make([]rune, 0, 32) for { char, _, err := buf.ReadRune() count = count + 1 if err != nil { return "", err } if count > 32 { return "", BadSDErr } switch char { case ']', '"': return "", BadSDErr case delim: return string(slice), nil default: slice = append(slice, char) } } }
func (c *char) prependString(cbuf *bytes.Buffer, rbuf []rune, str string) []rune { if len(str) == 0 { return rbuf } charsToAppend := utf8.RuneCountInString(str) - len(rbuf) if charsToAppend > 0 { for i := 0; i < charsToAppend; i++ { rbuf = append(rbuf, ' ') } } else if charsToAppend < 0 { rbuf = rbuf[:len(rbuf)+charsToAppend] } cbuf.Reset() cbuf.WriteString(str) for i := len(rbuf) - 1; i >= 0; i-- { r, _, _ := cbuf.ReadRune() rbuf[i] = r } for i := 0; i < len(rbuf); i++ { c.prependRune(rbuf[i]) } return rbuf }
func parse(buf *bytes.Buffer) Program { var p Program for { r, _, err := buf.ReadRune() if err == io.EOF { return p } var cmd Command switch r { case '+': cmd = Command{Op: Add, Arg: 1} case '-': cmd = Command{Op: Add, Arg: -1} case '>': cmd = Command{Op: Move, Arg: 1} case '<': cmd = Command{Op: Move, Arg: -1} case '.': cmd = Command{Op: Print} case ',': cmd = Command{Op: Scan} case '[': cmd = Command{Op: BNZ, Branch: parse(buf)} case ']': return p default: continue } p = append(p, cmd) } }
func (c *Converter) convert2(s string, proc resultProc) (string, error) { if !c.balanced { c.balance() } var out, pending bytes.Buffer r := readutil.NewStackabeRuneReader() r.PushFront(s) n := c.trie.Root() for { ch, _, err := r.ReadRune() if err == io.EOF { err = nil break } else if err != nil { return "", err } n = n.Get(ch) if n == nil { pending.WriteRune(ch) ch2, _, err := pending.ReadRune() if err == nil { out.WriteRune(ch2) r.PushFront(pending.String()) pending.Reset() n = c.trie.Root() } else if err != io.EOF { return "", err } } else if e, ok := n.Value().(*entry); ok { if len(e.output) > 0 { out.WriteString(e.output) } if len(e.remain) > 0 { r.PushFront(e.remain) } pending.Reset() n = c.trie.Root() } else { pending.WriteRune(ch) } } if proc != nil { proc(out.String(), pending.String(), n) } if pending.Len() > 0 { out.WriteString(pending.String()) } return out.String(), nil }
func TestOutput(t *testing.T) { in := new(bytes.Buffer) out := new(bytes.Buffer) m := NewMachine(".", in, out) // futz with the memory so the current cell has an "a" in m.memory[0] = 97 m.Run() if result, _, err := out.ReadRune(); result != 'a' || err != nil { t.Errorf(". should print one byte") } }
func (t *tScreen) parseRune(buf *bytes.Buffer) (bool, bool) { b := buf.Bytes() if b[0] >= ' ' && b[0] <= 0x7F { // printable ASCII easy to deal with -- no encodings ev := NewEventKey(KeyRune, rune(b[0]), ModNone) t.PostEvent(ev) buf.ReadByte() return true, true } if b[0] < 0x80 { // No encodings start with low numbered values return false, false } switch t.charset { case "UTF-8": if utf8.FullRune(b) { r, _, e := buf.ReadRune() if e == nil { ev := NewEventKey(KeyRune, r, ModNone) t.PostEvent(ev) return true, true } } case "US-ASCII": // ASCII cannot generate this, so most likely it was // entered as an Alt sequence ev := NewEventKey(KeyRune, rune(b[0]-128), ModAlt) t.PostEvent(ev) buf.ReadByte() return true, true default: utfb := make([]byte, 12) for l := 1; l <= len(b); l++ { t.decoder.Reset() nout, nin, _ := t.decoder.Transform(utfb, b[:l], true) if nout != 0 { if r, _ := utf8.DecodeRune(utfb[:nout]); r != utf8.RuneError { ev := NewEventKey(KeyRune, r, ModNone) t.PostEvent(ev) } for eat := 0; eat < nin; eat++ { buf.ReadByte() } return true, true } } } // Looks like potential escape return true, false }
func (c *char) appendString(cbuf *bytes.Buffer, str string) { if len(str) == 0 { return } cbuf.Reset() cbuf.WriteString(str) runesCount := utf8.RuneCountInString(str) for i := 0; i < runesCount; i++ { r, _, _ := cbuf.ReadRune() c.appendRune(r) } }
func handleEscape(p *parser, in, out *bytes.Buffer) (handler, error) { r, _, err := in.ReadRune() if err == io.EOF { return nil, ErrUnexpectedEOF } if r == '{' { return handleEscapeClause, nil } else if r == p.escape { out.WriteRune(p.escape) } else if p.color { out.WriteString(ColorCode(string(r))) } return handleRegular, nil }
func handleRegular(p *parser, in, out *bytes.Buffer) (handler, error) { for { r, _, err := in.ReadRune() // The only error that can happen here is that we have reached the end of file, // or that a rune is messed up. If the rune is messed up, we treat it normally. // This is why we only check for io.EOF. if err == io.EOF { break } if r == p.escape { return handleEscape, nil } out.WriteRune(r) } return nil, nil }
func readRawMsgBytes(buf *bytes.Buffer) ([]byte, error) { nilChar, _, err := buf.ReadRune() if err == io.EOF { return nil, nil } // see if there is a nil char if nilChar == '-' { return nil, nil } buf.UnreadRune() data := buf.Next(buf.Len()) return data, nil }
func replaseEmoji(input *bytes.Buffer) string { r := "" emoji := bytes.NewBufferString(":") for { i, _, err := input.ReadRune() if err != nil { // not replase r = emoji.String() break } emoji.WriteRune(i) if i == ':' { r = emojize(emoji.String()) break } } return r }
func handleEscapeClause(p *parser, in, out *bytes.Buffer) (handler, error) { bs := bytes.NewBufferString("") for { r, _, err := in.ReadRune() if err == io.EOF { return nil, ErrUnexpectedEOF } if r == '}' { break } bs.WriteRune(r) } if p.color { out.WriteString(ColorCode(bs.String())) } return handleRegular, nil }
func Parse(buf *bytes.Buffer) (*sysmsg.Message, error) { header, err := ParseHeader(buf) if err != nil { return nil, err } sd, err := ParseSData(buf) if err != nil { return nil, err } msg := &sysmsg.Message{header, sd, nil, false} if sd == nil { spChar, _, err := buf.ReadRune() if err == io.EOF { return msg, nil } //there should be a space char if spChar != sysmsg.SP_VAL_BYTE { return nil, BadMsgData } } data, err := readRawMsgBytes(buf) if err != nil { return nil, err } if data != nil && len(data) >= 3 { // check for BOM - EF.BB.BF if data[0] == 0xEF && data[1] == 0xBB && data[2] == 0xBF { data = data[3:] msg.IsUtf8 = true } } msg.RawMsg = data return msg, nil }
func parseSDParamVal(buf *bytes.Buffer) (string, error) { slice := make([]rune, 0, 128) firstQuote := true for { startChar, _, err := buf.ReadRune() if err != nil { return "", err } switch { case startChar == '\\': // check the next char it must be an escape char nextChar, _, err := buf.ReadRune() if err != nil { return "", err } if nextChar != '\\' && nextChar != '"' && nextChar != ']' { slice = append(slice, startChar) } slice = append(slice, nextChar) break case startChar == '"': if !firstQuote { return string(slice), nil } if firstQuote { firstQuote = false } break default: slice = append(slice, startChar) break } } }
func replaceColorTags(input, output *bytes.Buffer, clean bool) bool { tag := bytes.NewBufferString("") LOOP: for { i, _, err := input.ReadRune() if err != nil { output.WriteString("{" + tag.String()) return true } switch i { default: tag.WriteRune(i) case '{': output.WriteString("{" + tag.String()) tag = bytes.NewBufferString("") case '}': break LOOP } } tagStr := tag.String() if !isValidTag(tagStr) { output.WriteString("{" + tagStr + "}") return true } if tagStr == "!" { if !clean { output.WriteString(_CODE_RESET) } return true } output.WriteString(tag2ANSI(tagStr, clean)) return false }
func TestChunkedArtifactWriteIncompleteUnicodeCharacters(t *testing.T) { const bucketName = "bucketName" const ownerName = "ownerName" const artifactName = "artifactName" if testing.Short() { t.Skip("Skipping end-to-end test in short mode.") } client := setup(t) bucket, err := client.NewBucket(bucketName, ownerName, 31) require.NotNil(t, bucket) require.NoError(t, err) // Append bytes to artifacts. cartifact, err := bucket.NewChunkedArtifact(artifactName) require.NotNil(t, cartifact) require.NoError(t, err) { var buf bytes.Buffer buf.WriteRune('☃') // Rune of length 3 bytes require.NoError(t, cartifact.AppendLog(string(buf.Next(2)))) // Send only first two bytes require.NoError(t, cartifact.AppendLog(string(buf.Next(1)))) // Send last byte require.NoError(t, cartifact.Flush()) } { reader, err := cartifact.GetContent() require.NoError(t, err) var buf bytes.Buffer buf.ReadFrom(reader) r, size, buferr := buf.ReadRune() require.NoError(t, buferr) require.Equal(t, '☃', r) require.Equal(t, 3, size) } }
func replaseEmoji(input *bytes.Buffer) string { emoji := bytes.NewBufferString(":") for { i, _, err := input.ReadRune() if err != nil { // not replase return emoji.String() } if i == ':' && emoji.Len() == 1 { return emoji.String() + replaseEmoji(input) } emoji.WriteRune(i) switch { case unicode.IsSpace(i): return emoji.String() case i == ':': return emojize(emoji.String()) } } }
// 与 RecursiveInt.Scan 执行相同的扫描任务,但不会循环通过扫描器,因此我们更直接地比较性能。 func scanInts(r *RecursiveInt, b *bytes.Buffer) (err error) { r.next = nil _, err = Fscan(b, &r.i) if err != nil { return } c, _, err := b.ReadRune() if err != nil { if err == io.EOF { err = nil } return } if c != '.' { return } next := new(RecursiveInt) err = scanInts(next, b) if err == nil { r.next = next } return }
func dequote(source *bytes.Buffer) string { var buffer bytes.Buffer lastchar := ' ' quoteNow := NOTQUOTED yenCount := 0 for { ch, _, err := source.ReadRune() if err != nil { break } if ch == '~' && unicode.IsSpace(lastchar) { if home := dos.GetHome(); home != "" { buffer.WriteString(home) } else { buffer.WriteRune('~') } lastchar = '~' continue } if ch == '%' && quoteNow != '\'' && yenCount%2 == 0 { var nameBuf bytes.Buffer for { ch, _, err = source.ReadRune() if err != nil { buffer.WriteRune('%') buffer.WriteString(nameBuf.String()) return buffer.String() } if ch == '%' { nameStr := nameBuf.String() value := os.Getenv(nameStr) if value != "" { buffer.WriteString(value) } else if m := rxUnicode.FindStringSubmatch(nameStr); m != nil { ucode, _ := strconv.ParseInt(m[1], 16, 32) buffer.WriteRune(rune(ucode)) } else if f, ok := PercentFunc[nameStr]; ok { buffer.WriteString(f()) } else { buffer.WriteRune('%') buffer.WriteString(nameStr) buffer.WriteRune('%') } break } if !unicode.IsLower(ch) && !unicode.IsUpper(ch) && !unicode.IsNumber(ch) && ch != '_' && ch != '+' { source.UnreadRune() buffer.WriteRune('%') buffer.WriteString(nameBuf.String()) break } nameBuf.WriteRune(ch) } continue } if quoteNow != NOTQUOTED && ch == quoteNow && yenCount%2 == 0 { // Close Quotation. for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } quoteNow = NOTQUOTED } else if (ch == '\'' || ch == '"') && quoteNow == NOTQUOTED && yenCount%2 == 0 { // Open Qutation. for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } quoteNow = ch if ch == lastchar { buffer.WriteRune(ch) } } else { if ch == '\\' { yenCount++ } else if ch == '\'' || ch == '"' { for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } yenCount = 0 buffer.WriteRune(ch) } else { for ; yenCount > 0; yenCount-- { buffer.WriteRune('\\') } buffer.WriteRune(ch) } } lastchar = ch } for ; yenCount > 0; yenCount-- { buffer.WriteRune('\\') } return buffer.String() }
func buffer2word(source bytes.Buffer, removeQuote bool) string { var buffer bytes.Buffer lastchar := ' ' quoteNow := NOTQUOTED yenCount := 0 for { ch, _, err := source.ReadRune() if err != nil { break } if ch == '~' && unicode.IsSpace(lastchar) { if home := dos.GetHome(); home != "" { buffer.WriteString(home) } else { buffer.WriteRune('~') } lastchar = '~' continue } if ch == '%' && quoteNow != '\'' && yenCount%2 == 0 { var nameBuf bytes.Buffer for { ch, _, err = source.ReadRune() if err != nil { buffer.WriteRune('%') nameBuf.WriteTo(&buffer) return buffer.String() } if ch == '%' { if value, ok := OurGetEnv(nameBuf.String()); ok { buffer.WriteString(value) } else { buffer.WriteRune('%') nameBuf.WriteTo(&buffer) buffer.WriteRune('%') } break } if ch == '=' { source.UnreadRune() buffer.WriteRune('%') nameBuf.WriteTo(&buffer) break } nameBuf.WriteRune(ch) } continue } if quoteNow != NOTQUOTED && ch == quoteNow && yenCount%2 == 0 { if !removeQuote { buffer.WriteRune(ch) } // Close Quotation. for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } quoteNow = NOTQUOTED } else if (ch == '\'' || ch == '"') && quoteNow == NOTQUOTED && yenCount%2 == 0 { if !removeQuote { buffer.WriteRune(ch) } // Open Qutation. for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } quoteNow = ch if ch == lastchar { buffer.WriteRune(ch) } } else { if ch == '\\' { yenCount++ } else if ch == '\'' || ch == '"' { for ; yenCount >= 2; yenCount -= 2 { buffer.WriteRune('\\') } yenCount = 0 buffer.WriteRune(ch) } else { for ; yenCount > 0; yenCount-- { buffer.WriteRune('\\') } buffer.WriteRune(ch) } } lastchar = ch } for ; yenCount > 0; yenCount-- { buffer.WriteRune('\\') } return buffer.String() }
func replaceDateTag(d time.Time, input, output *bytes.Buffer) { r, _, err := input.ReadRune() if err != nil { return } switch r { case '%': output.WriteString("%") case 'a': output.WriteString(getShortWeekday(d.Weekday())) case 'A': output.WriteString(getLongWeekday(d.Weekday())) case 'b', 'h': output.WriteString(getShortMonth(d.Month())) case 'B': output.WriteString(getLongMonth(d.Month())) case 'c': zn, _ := d.Zone() output.WriteString(fmt.Sprintf("%s %02d %s %d %02d:%02d:%02d %s %s", getShortWeekday(d.Weekday()), d.Day(), getShortMonth(d.Month()), d.Year(), getAMPMHour(d), d.Minute(), d.Second(), getAMPM(d, true), zn)) case 'C', 'g': output.WriteString(strconv.Itoa(d.Year())[0:2]) case 'd': output.WriteString(fmt.Sprintf("%02d", d.Day())) case 'D': output.WriteString(fmt.Sprintf("%02d/%02d/%s", d.Month(), d.Day(), strconv.Itoa(d.Year())[2:4])) case 'e': if d.Day() >= 10 { output.WriteString(strconv.Itoa(d.Day())) } else { output.WriteString(" " + strconv.Itoa(d.Day())) } case 'F': output.WriteString(fmt.Sprintf("%d-%02d-%02d", d.Year(), d.Month(), d.Day())) case 'G': output.WriteString(strconv.Itoa(d.Year())) case 'H': output.WriteString(fmt.Sprintf("%02d", d.Hour())) case 'I': output.WriteString(fmt.Sprintf("%02d", getAMPMHour(d))) case 'j': output.WriteString(fmt.Sprintf("%03d", d.YearDay())) case 'k': output.WriteString(" " + strconv.Itoa(d.Hour())) case 'l': output.WriteString(strconv.Itoa(getAMPMHour(d))) case 'm': output.WriteString(fmt.Sprintf("%02d", d.Month())) case 'M': output.WriteString(fmt.Sprintf("%02d", d.Minute())) case 'N': output.WriteString(fmt.Sprintf("%09d", d.Nanosecond())) case 'p': output.WriteString(getAMPM(d, false)) case 'P': output.WriteString(getAMPM(d, true)) case 'r': output.WriteString(fmt.Sprintf("%02d:%02d:%02d %s", getAMPMHour(d), d.Minute(), d.Second(), getAMPM(d, true))) case 'R': output.WriteString(fmt.Sprintf("%02d:%02d", d.Hour(), d.Minute())) case 's': output.WriteString(strconv.FormatInt(d.Unix(), 10)) case 'S': output.WriteString(fmt.Sprintf("%02d", d.Second())) case 'T': output.WriteString(fmt.Sprintf("%02d:%02d:%02d", d.Hour(), d.Minute(), d.Second())) case 'u': output.WriteString(strconv.Itoa(getWeekdayNum(d))) case 'V': _, wn := d.ISOWeek() output.WriteString(fmt.Sprintf("%02d", wn)) case 'w': output.WriteString(strconv.Itoa(int(d.Weekday()))) case 'y': output.WriteString(strconv.Itoa(d.Year())[2:4]) case 'Y': output.WriteString(strconv.Itoa(d.Year())) case 'z': output.WriteString(getTimezone(d, false)) case ':': input.ReadRune() output.WriteString(getTimezone(d, true)) case 'Z': zn, _ := d.Zone() output.WriteString(zn) default: output.WriteRune('%') output.WriteRune(r) } }
func ParseSData(buf *bytes.Buffer) (*sysmsg.StrctData, error) { var sData *sysmsg.StrctData startChar, _, err := buf.ReadRune() if err != nil { return sData, err } // [ = 91 // ] = 93 // \ = 92 if startChar == '-' { return nil, nil } // if it doesn't start with '[' return error if startChar != 91 { return sData, BadSDErr } buf.UnreadRune() // now, there exists SData, parse it readSData := true endSData := false //[exampleSDID iut="3" eventSource="Application" eventID="1011"][examplePriority@32473 class="high"] var element sysmsg.SDElement var readParam bool loop: for { startChar, _, err := buf.ReadRune() if err == io.EOF && endSData { return sData, nil } else if err != nil { return sData, err } switch { case startChar == '[' || readSData: //fmt.Println("begin SData") readSData = false endSData = false id, err := parseSDName(buf, sysmsg.SP_VAL_BYTE) //fmt.Println("Parsed ID ", id) if err != nil { return sData, err } element = sysmsg.SDElement{} element.Id = id element.Params = make([]sysmsg.SDParam, 0, 2) readParam = true break case startChar == sysmsg.SP_VAL_BYTE || readParam: //fmt.Println("begin SData SP") // step back one char if readParam { buf.UnreadRune() } readParam = false if endSData { break loop } name, err := parseSDName(buf, '=') if err != nil { return sData, err } val, err := parseSDParamVal(buf) if err != nil { return sData, err } param := sysmsg.SDParam{name, val} element.Params = append(element.Params, param) break case startChar == ']': //fmt.Println("end SData") endSData = true if sData == nil { sData = &sysmsg.StrctData{} sData.Elements = make([]sysmsg.SDElement, 0, 2) } sData.Elements = append(sData.Elements, element) continue loop } } fmt.Println("completed parsing SData", sData) return sData, nil }