Exemple #1
0
func (field_editor *FieldEditor) handleKeyEvent(event termbox.Event) (string, bool) {
	is_done := false
	if event.Key == termbox.KeyEnter {
		is_done = true
	} else if event.Key == termbox.KeyEsc {
		is_done = true
		field_editor.value = nil
	} else if event.Key == termbox.KeyArrowLeft {
		if field_editor.cursor_pos > 0 {
			field_editor.cursor_pos--
		}
	} else if event.Key == termbox.KeyArrowUp || event.Key == termbox.KeyCtrlA {
		field_editor.cursor_pos = 0
	} else if event.Key == termbox.KeyArrowRight {
		if field_editor.cursor_pos < utf8.RuneCount(field_editor.value) {
			field_editor.cursor_pos++
		}
	} else if event.Key == termbox.KeyArrowDown || event.Key == termbox.KeyCtrlE {
		field_editor.cursor_pos = utf8.RuneCount(field_editor.value)
	} else if event.Key == termbox.KeyCtrlH || event.Key == termbox.KeyBackspace {
		if field_editor.cursor_pos > 0 {
			field_editor.value = removeRuneAtIndex(field_editor.value, field_editor.cursor_pos-1)
			field_editor.cursor_pos--
		}
	} else if unicode.IsPrint(event.Ch) {
		field_editor.value = insertRuneAtIndex(field_editor.value, field_editor.cursor_pos, event.Ch)
		field_editor.cursor_pos++
	} else if event.Key == termbox.KeySpace {
		field_editor.value = insertRuneAtIndex(field_editor.value, field_editor.cursor_pos, ' ')
		field_editor.cursor_pos++
	}
	return string(field_editor.value), is_done
}
Exemple #2
0
// parseStringToken takes a token of either LITERAL or STRING type and
// returns the interpreted string, after processing any relevant
// escape sequences.
func (p *parser) parseStringToken(tok *scanner.Token) (string, error) {
	var backslashes bool
	switch tok.Type {
	case scanner.LITERAL:
		backslashes = false
	case scanner.STRING:
		backslashes = true
	default:
		panic("unsupported string token type")
	}

	raw := []byte(tok.Content)
	buf := make([]byte, 0, len(raw))

	for i := 0; i < len(raw); i++ {
		b := raw[i]
		more := len(raw) > (i + 1)

		if b == '$' {
			if more && raw[i+1] == '$' {
				// skip over the second dollar sign
				i++
			}
		} else if backslashes && b == '\\' {
			if !more {
				return "", Errorf(
					ast.Pos{
						Column: tok.Pos.Column + utf8.RuneCount(raw[:i]),
						Line:   tok.Pos.Line,
					},
					`unfinished backslash escape sequence`,
				)
			}
			escapeType := raw[i+1]
			switch escapeType {
			case '\\':
				// skip over the second slash
				i++
			case 'n':
				b = '\n'
				i++
			case '"':
				b = '"'
				i++
			default:
				return "", Errorf(
					ast.Pos{
						Column: tok.Pos.Column + utf8.RuneCount(raw[:i]),
						Line:   tok.Pos.Line,
					},
					`invalid backslash escape sequence`,
				)
			}
		}

		buf = append(buf, b)
	}

	return string(buf), nil
}
Exemple #3
0
func (v *RuneCountValidator) Validate(value string, criteria *Criteria) (bool, error) {
	if criteria == nil {
		return false, errors.New("Criteria for 'rune_count' not enough")
	}
	if criteria.Has("eq") {
		eq, err := criteria.Int("eq")
		if err != nil {
			return false, err
		}
		return utf8.RuneCount([]byte(value)) == eq, nil
	} else if criteria.Has("to") && criteria.Has("from") {
		to, err := criteria.Int("to")
		if err != nil {
			return false, err
		}
		from, err := criteria.Int("from")
		if err != nil {
			return false, err
		}
		count := utf8.RuneCount([]byte(value))
		return count >= from && count <= to, nil
	} else {
		return false, errors.New("Criteria for 'rune_count' not enough")
	}
}
Exemple #4
0
func Timeline(screen_name string, since_id int64) []Post {
	var posts = []Post{}
	for _, p := range sina.TimeLine(0, screen_name, since_id, 20) {
		id := p.Id
		text := p.Text
		link := ""
		if p.Original_Pic != "" {
			link = " ✈ " + p.Original_Pic
		}
		if p.Retweeted_Status != nil {
			if p.Retweeted_Status.User != nil {
				re_user := p.Retweeted_Status.User
				text = text + " //RT @" + re_user.Name + ": " + p.Retweeted_Status.Text
			}

			if p.Retweeted_Status.Original_Pic != "" {
				link = " ✈ " + p.Retweeted_Status.Original_Pic
			}
		}
		len1 := utf8.RuneCount([]byte(text))
		len2 := utf8.RuneCount([]byte(link))
		if len1+len2 > 140 {
			link2 := fmt.Sprintf("http://weibo.com/%d/%s", p.User.Id, sina.QueryMid(id, 1))
			text = SubStringByChar(text, 140-38-len2) + link + " " + link2
		} else {
			text = text + link
		}
		posts = append(posts, Post{id, text})
	}
	return posts
}
Exemple #5
0
func (v *jsonSchema) validateString(currentSchema *jsonSchema, value interface{}, result *ValidationResult, context *jsonContext) {

	// Ignore non strings
	if !isKind(value, reflect.String) {
		return
	}

	stringValue := value.(string)

	// minLength & maxLength:
	if currentSchema.minLength != nil {
		if utf8.RuneCount([]byte(stringValue)) < *currentSchema.minLength {
			result.addError(context, value, fmt.Sprintf(ERROR_MESSAGE_STRING_LENGTH_MUST_BE_GREATER_OR_EQUAL, *currentSchema.minLength))
		}
	}
	if currentSchema.maxLength != nil {
		if utf8.RuneCount([]byte(stringValue)) > *currentSchema.maxLength {
			result.addError(context, value, fmt.Sprintf(ERROR_MESSAGE_STRING_LENGTH_MUST_BE_LOWER_OR_EQUAL, *currentSchema.maxLength))
		}
	}

	// pattern:
	if currentSchema.pattern != nil {
		if !currentSchema.pattern.MatchString(stringValue) {
			result.addError(context, value, fmt.Sprintf(ERROR_MESSAGE_DOES_NOT_MATCH_PATTERN, currentSchema.pattern))

		}
	}

	result.incrementScore()
}
func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{}, result *Result, context *jsonContext) {

	internalLog("validateString %s", context.String())
	internalLog(" %v", value)

	// Ignore non strings
	if !isKind(value, reflect.String) {
		return
	}

	stringValue := value.(string)

	// minLength & maxLength:
	if currentSubSchema.minLength != nil {
		if utf8.RuneCount([]byte(stringValue)) < *currentSubSchema.minLength {
			result.addError(
				new(StringLengthGTEError),
				context,
				value,
				ErrorDetails{"min": *currentSubSchema.minLength},
			)
		}
	}
	if currentSubSchema.maxLength != nil {
		if utf8.RuneCount([]byte(stringValue)) > *currentSubSchema.maxLength {
			result.addError(
				new(StringLengthLTEError),
				context,
				value,
				ErrorDetails{"max": *currentSubSchema.maxLength},
			)
		}
	}

	// pattern:
	if currentSubSchema.pattern != nil {
		if matcher := currentSubSchema.pattern.MatcherString(stringValue, 0); !matcher.Matches() {
			result.addError(
				new(DoesNotMatchPatternError),
				context,
				value,
				ErrorDetails{"pattern": currentSubSchema.patternString},
			)
		}
	}

	// format
	if currentSubSchema.format != "" {
		if !FormatCheckers.IsFormat(currentSubSchema.format, stringValue) {
			result.addError(
				new(DoesNotMatchFormatError),
				context,
				value,
				ErrorDetails{"format": currentSubSchema.format},
			)
		}
	}

	result.incrementScore()
}
Exemple #7
0
func Timeline(access_token string, screen_name string, since_id int64) []Post {
	url := "https://api.weibo.com/2/statuses/user_timeline.json?access_token=" + access_token
	url += fmt.Sprintf("&screen_name=%s&since_id=%d", screen_name, since_id)

	//url = "http://www.baidu.com"

	resp, err := http.Get(url)
	if err != nil {
		log.Fatal(url, err)
	}
	defer resp.Body.Close()
	bytes, _ := ioutil.ReadAll(resp.Body)

	log.Println(url)

	var posts = []Post{}
	if resp.StatusCode == 200 {
		var data map[string]interface{}
		json.Unmarshal(bytes, &data)

		// log.Println(string(bytes))

		if data["statuses"] != nil {
			for _, entry := range data["statuses"].([]interface{}) {
				entry := entry.(map[string]interface{})
				id, _ := strconv.ParseInt(entry["idstr"].(string), 10, 64)
				text := entry["text"].(string)
				link := ""
				if entry["original_pic"] != nil {
					link = " ✈ " + entry["original_pic"].(string)
				}

				if entry["retweeted_status"] != nil {
					retweeted := entry["retweeted_status"].(map[string]interface{})
					if retweeted["user"] != nil {
						re_user := retweeted["user"].(map[string]interface{})
						text = text + " //RT @" + re_user["name"].(string) + ": " + retweeted["text"].(string)
					}

					if retweeted["original_pic"] != nil {
						link = " ✈ " + retweeted["original_pic"].(string)
					}
				}
				len1 := utf8.RuneCount([]byte(text))
				len2 := utf8.RuneCount([]byte(link))
				if len1+len2 > 140 {
					text = SubStringByChar(text, 140-len2) + link
				} else {
					text = text + link
				}

				posts = append(posts, Post{id, text})
			}
		}
	} else {
		log.Fatal(string(bytes))
	}
	return posts
}
Exemple #8
0
func extractColor(str string, state *ansiState, proc func(string, *ansiState) bool) (string, *[]ansiOffset, *ansiState) {
	var offsets []ansiOffset
	var output bytes.Buffer

	if state != nil {
		offsets = append(offsets, ansiOffset{[2]int32{0, 0}, *state})
	}

	idx := 0
	for _, offset := range ansiRegex.FindAllStringIndex(str, -1) {
		prev := str[idx:offset[0]]
		output.WriteString(prev)
		if proc != nil && !proc(prev, state) {
			return "", nil, nil
		}
		newState := interpretCode(str[offset[0]:offset[1]], state)

		if !newState.equals(state) {
			if state != nil {
				// Update last offset
				(&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))
			}

			if newState.colored() {
				// Append new offset
				state = newState
				newLen := int32(utf8.RuneCount(output.Bytes()))
				offsets = append(offsets, ansiOffset{[2]int32{newLen, newLen}, *state})
			} else {
				// Discard state
				state = nil
			}
		}

		idx = offset[1]
	}

	rest := str[idx:]
	if len(rest) > 0 {
		output.WriteString(rest)
		if state != nil {
			// Update last offset
			(&offsets[len(offsets)-1]).offset[1] = int32(utf8.RuneCount(output.Bytes()))
		}
	}
	if proc != nil {
		proc(rest, state)
	}
	if len(offsets) == 0 {
		return output.String(), nil, state
	}
	return output.String(), &offsets, state
}
Exemple #9
0
// Encode takes payload, encodes it and writes it to dst. Payload must be one
// of the following: a heartbeat, a handshake, []byte, string, int or anything
// than can be marshalled by the default json package. If payload can't be
// encoded or the writing fails, an error will be returned.
func (enc *sioEncoder) Encode(dst io.Writer, payload interface{}) (err error) {
	enc.elem.Reset()

	switch t := payload.(type) {
	case heartbeat:
		s := strconv.Itoa(int(t))
		_, err = fmt.Fprintf(dst, "%s%d%s%s%s", sioFrameDelim, len(s)+len(sioFrameDelimHeartbeat), sioFrameDelim, sioFrameDelimHeartbeat, s)

	case handshake:
		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, len(t), sioFrameDelim, t)

	case []byte:
		l := utf8.RuneCount(t)
		if l == 0 {
			break
		}
		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, l, sioFrameDelim, t)

	case string:
		l := utf8.RuneCountInString(t)
		if l == 0 {
			break
		}
		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, l, sioFrameDelim, t)

	case int:
		s := strconv.Itoa(t)
		if s == "" {
			break
		}
		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, len(s), sioFrameDelim, s)

	default:
		data, err := json.Marshal(payload)
		if len(data) == 0 || err != nil {
			break
		}
		err = json.Compact(&enc.elem, data)
		if err != nil {
			break
		}

		_, err = fmt.Fprintf(dst, "%s%d%s%s", sioFrameDelim, utf8.RuneCount(enc.elem.Bytes())+len(sioFrameDelimJSON), sioFrameDelim, sioFrameDelimJSON)
		if err == nil {
			_, err = enc.elem.WriteTo(dst)
		}
	}

	return err
}
Exemple #10
0
func makeFakeData(size1, size2 int) ([]byte, Annotations) {
	input := []byte(strings.Repeat(strings.Repeat("a", size1)+"⌘", size2))
	inputLength := utf8.RuneCount(input)
	n := len(input)/2 - (size1+1)/2
	anns := make(Annotations, n)
	for i := 0; i < n; i++ {
		if i%2 == 0 {
			anns[i] = &Annotation{Start: 2 * i, End: 2*i + 1}
		} else {
			anns[i] = &Annotation{Start: 2*i - 50, End: 2*i + 50}
			if anns[i].Start < 0 {
				anns[i].Start = 0
				anns[i].End = i
			}
			if anns[i].End >= inputLength {
				anns[i].End = inputLength
			}
		}
		anns[i].Left = []byte("L")  //[]byte(strings.Repeat("L", i%20))
		anns[i].Right = []byte("R") //[]byte(strings.Repeat("R", i%20))
		anns[i].WantInner = i % 5
	}
	sort.Sort(anns)
	return input, anns
}
Exemple #11
0
// Render cell as text.
// As a special case a cell that is nil is treated as an empty cell.
func (cell *Cell) renderText(cellWidth int) []byte {
	if cell == nil {
		return fill(' ', cellWidth)
	}
	buf := make([]byte, 0)
	if cell.PadLeft > 0 {
		buf = append(buf, fill(' ', int(cell.PadLeft))...)
	}
	buf = append(buf, cell.Content...)
	if cell.PadRight > 0 {
		buf = append(buf, fill(' ', int(cell.PadRight))...)
	}

	pad := cellWidth - utf8.RuneCount(buf)
	var padLeft, padRight int
	switch cell.Align {
	case AlignRight:
		padLeft = pad
	case AlignCenter:
		// Pad with a bias of more padding to the right.
		padLeft = pad / 2
		padRight = pad - padLeft
	default:
		// Since it's possible to pass values other than the specified
		// constants use AlignLeft as the default.
		padRight = pad
	}
	buf = append(buf, fill(' ', padRight)...)
	return append(fill(' ', padLeft), buf...)
}
Exemple #12
0
func (s *CJKWidthFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
	for _, token := range input {
		runeCount := utf8.RuneCount(token.Term)
		runes := bytes.Runes(token.Term)
		for i := 0; i < runeCount; i++ {
			ch := runes[i]
			if ch >= 0xFF01 && ch <= 0xFF5E {
				// fullwidth ASCII variants
				runes[i] -= 0xFEE0
			} else if ch >= 0xFF65 && ch <= 0xFF9F {
				// halfwidth Katakana variants
				if (ch == 0xFF9E || ch == 0xFF9F) && i > 0 && combine(runes, i, ch) {
					runes = analysis.DeleteRune(runes, i)
					i--
					runeCount = len(runes)
				} else {
					runes[i] = kanaNorm[ch-0xFF65]
				}
			}
		}
		token.Term = analysis.BuildTermFromRunes(runes)
	}

	return input
}
Exemple #13
0
func CountChar(str string) (int, int) {
	b := []byte(str)
	//	int bytes = len(b)
	var bytes int = len(b)
	var runes int = utf8.RuneCount(b)
	return bytes, runes
}
Exemple #14
0
func tabsToSpaces(in []byte, tabsize int) []byte {
	if bytes.IndexByte(in, '\t') == -1 {
		return in
	}

	spaces := bytes.Repeat([]byte(" "), tabsize)

	var out []byte
	i := bytes.IndexAny(in, "\n\r\f\t")
	col := 0

	for i != -1 {
		out = append(out, in[:i]...)
		col += utf8.RuneCount(in[:i])
		if in[i] == '\t' {
			nspaces := tabsize - (col % tabsize)
			out = append(out, spaces[:nspaces]...)
			col += nspaces
		} else {
			// line feed
			out = append(out, in[i])
			col = 0
		}

		in = in[i+1:]
		i = bytes.IndexAny(in, "\n\r\f\t")
	}

	return append(out, in...)
}
Exemple #15
0
func TestTextState(t *testing.T) {
	gen := new(testGenerator)
	p := NewParser(gen)

	data := parserData{isTagLine: false, asIs: false, parseText: true, tagMode: tagModeLtGt}

	text := "txt <"
	buf := bytes.NewBufferString(text)

	i := 0
	charsInBuf := utf8.RuneCount(buf.Bytes())

	for r, _, err := buf.ReadRune(); err == nil; r, _, err = buf.ReadRune() {
		if st, err := p.states[stText].processChar(r, gen, &data); err == nil {
			if i == charsInBuf-1 {
				if st != stInlineTag {
					t.Error(fmt.Sprintf("In %s at %d state %d must be %d.", text, i, st, stInlineTag))
					t.Fail()
				}
			} else if st != stText {
				t.Error(fmt.Sprintf("In %s at %d state %d must be %d.", text, i, st, stText))
				t.Fail()
			}
		} else {
			t.Error(fmt.Sprintf("Error on %s at %d %s.", text, i, err.String()))
			t.Fail()
		}

		i++
	}
}
Exemple #16
0
func (s *NgramFilter) Filter(input analysis.TokenStream) analysis.TokenStream {
	rv := make(analysis.TokenStream, 0, len(input))

	for _, token := range input {
		runeCount := utf8.RuneCount(token.Term)
		runes := bytes.Runes(token.Term)
		for i := 0; i < runeCount; i++ {
			// index of the starting rune for this token
			for ngramSize := s.minLength; ngramSize <= s.maxLength; ngramSize++ {
				// build an ngram of this size starting at i
				if i+ngramSize <= runeCount {
					ngramTerm := buildTermFromRunes(runes[i : i+ngramSize])
					token := analysis.Token{
						Position: token.Position,
						Start:    token.Start,
						End:      token.End,
						Type:     token.Type,
						Term:     ngramTerm,
					}
					rv = append(rv, &token)
				}
			}
		}
	}

	return rv
}
Exemple #17
0
func StrPos(str string, sep string) int64 {
	//indez := bytes.Index([]byte(str), []byte(sep))
	indez := strings.Index(str, sep)
	var index int64 = -1
	if indez != -1 {
		index = int64(utf8.RuneCount([]byte(str)[:indez]))
		//fmt.Println(index, index2, str[index:index+len(sep)])

	}
	return index
	/*rstr := []rune(sep)[0]
	return int64(strings.Index(str, sep))
	*/
	/*
		n := len(sep)
		fmt.Println(n)
		if n == 0 {
			return 0
		}
		c := sep[0]

		if n == 1 {
			for i, k := range rs {
				if strs[i] == c {
					fmt.Println(k)
					fmt.Println(i)
					return int64(i)
				}
			}
		}
		return -1
	*/
}
Exemple #18
0
// Count counts the number of non-overlapping instances of sep in s.
func Count(s, sep []byte) int {
	n := len(sep)
	if n == 0 {
		return utf8.RuneCount(s) + 1
	}
	if n > len(s) {
		return 0
	}
	count := 0
	c := sep[0]
	i := 0
	t := s[:len(s)-n+1]
	for i < len(t) {
		if t[i] != c {
			o := IndexByte(t[i:], c)
			if o < 0 {
				break
			}
			i += o
		}
		if n == 1 || Equal(s[i:i+n], sep) {
			count++
			i += n
			continue
		}
		i++
	}
	return count
}
Exemple #19
0
// NewCanvas returns an initialized Canvas.
func NewCanvas(data []byte) *Canvas {
	c := &Canvas{}

	c.rawData = data
	lines := bytes.Split(data, []byte("\n"))
	c.size.Y = len(lines)
	for _, line := range lines {
		if i := utf8.RuneCount(line); i > c.size.X {
			c.size.X = i
		}
	}
	for _, line := range lines {
		t := make([]char, c.size.X)
		i := 0
		for len(line) > 0 {
			r, l := utf8.DecodeRune(line)
			t[i] = char(r)
			i++
			line = line[l:]
		}
		for ; i < c.size.X; i++ {
			t[i] = ' '
		}
		c.grid = append(c.grid, t)
	}

	return c
}
Exemple #20
0
// MinLength validates a string for minimum length
func MinLength(path, in, data string, minLength int64) *errors.Validation {
	strLen := int64(utf8.RuneCount([]byte(data)))
	if strLen < minLength {
		return errors.TooShort(path, in, minLength)
	}
	return nil
}
Exemple #21
0
// MaxLength validates a string for maximum length
func MaxLength(path, in, data string, maxLength int64) *errors.Validation {
	strLen := int64(utf8.RuneCount([]byte(data)))
	if strLen > maxLength {
		return errors.TooLong(path, in, maxLength)
	}
	return nil
}
Exemple #22
0
func (f *File) SetByteOffsetsForContent(content []byte) {

	offset := 0
	if len(content) >= 3 {
		if bytes.HasPrefix(content, utf8bom) {
			content = content[3:]
			offset = 3
		}
	}

	s := string(content)

	byteOffsetOfRune := make([]int, utf8.RuneCount(content))
	i := 0
	for b, _ := range s {
		byteOffsetOfRune[i] = b + offset
		i++
	}

	f.numRunes = len(byteOffsetOfRune)

	// set lines table
	f.set.mutex.Lock()
	f.byteOffsetOfRune = byteOffsetOfRune
	f.set.mutex.Unlock()
}
Exemple #23
0
// Q3. (1) string
func Q3() {
	// 1
	str := "A"
	for i := 1; i <= 10; i++ {
		fmt.Printf("%s\n", str)
		str = str + "A"
	}

	// 2
	var s string = "asSASA ddd dsjkdsjs dk"
	fmt.Println(len(s), " ", utf8.RuneCount([]byte(s)))

	// 3
	//s3 := strings.Replace(s, s[4:7], "abc", 3)
	s3 := []byte(s)
	copy(s3[4:4+3], []byte("abc"))
	fmt.Println(string(s3))

	// 4
	var s4 string = "foobar"
	reverse := func(s string) string {
		array := []byte(s)
		for i, j := 0, len(array)-1; i < j; i, j = i+1, j-1 {
			array[i], array[j] = array[j], array[i]
		}
		return string(array)
	}

	s5 := reverse(s4)
	fmt.Println(s5)
}
Exemple #24
0
func (f *DictionaryCompoundFilter) decompose(token *analysis.Token) []*analysis.Token {
	runes := bytes.Runes(token.Term)
	rv := make([]*analysis.Token, 0)
	rlen := len(runes)
	for i := 0; i <= (rlen - f.minSubWordSize); i++ {
		var longestMatchToken *analysis.Token
		for j := f.minSubWordSize; j <= f.maxSubWordSize; j++ {
			if i+j > rlen {
				break
			}
			_, inDict := f.dict[string(runes[i:i+j])]
			if inDict {
				newtoken := analysis.Token{
					Term:     []byte(string(runes[i : i+j])),
					Position: token.Position,
					Start:    token.Start + i,
					End:      token.Start + i + j,
					Type:     token.Type,
					KeyWord:  token.KeyWord,
				}
				if f.onlyLongestMatch {
					if longestMatchToken == nil || utf8.RuneCount(longestMatchToken.Term) < j {
						longestMatchToken = &newtoken
					}
				} else {
					rv = append(rv, &newtoken)
				}
			}
		}
		if f.onlyLongestMatch && longestMatchToken != nil {
			rv = append(rv, longestMatchToken)
		}
	}
	return rv
}
Exemple #25
0
func (m *Message) writeRecipient(b *bytes.Buffer) {
	if m.From != "" {
		fmt.Fprintf(b, "From: %s %s", m.From, NewLine)
	}

	if m.ReplyTo != "" {
		fmt.Fprintf(b, "Reply-To: %s %s", m.ReplyTo, NewLine)
	}

	if len(m.To) > 0 {
		fmt.Fprintf(b, "To: %s %s", strings.Join(m.To, ", "), NewLine)
	}

	if len(m.Cc) > 0 {
		fmt.Fprintf(b, "Cc: %s %s", strings.Join(m.Cc, ", "), NewLine)
	}

	if m.Subject != "" {
		subjectBytes := []byte(m.Subject)
		if len(subjectBytes) == utf8.RuneCount(subjectBytes) {
			fmt.Fprintf(b, "Subject: %s %s", m.Subject, NewLine)
		} else {
			fmt.Fprintf(b, "Subject: =?UTF-8?B?%s?= %s", base64.StdEncoding.EncodeToString(subjectBytes), NewLine)
		}
	}
}
Exemple #26
0
// extractLetterSequence extracts first word (sequence of letters ending with a non-letter)
// starting with the specified index and wraps it to dateStringLayoutItem according to the type
// of the word.
func extractLetterSequence(originalStr string, index int) (it dateStringLayoutItem) {
	letters := ""

	bytesToParse := []byte(originalStr[index:])
	runeCount := utf8.RuneCount(bytesToParse)

	var isWord bool
	var isDigit bool

	for i := 0; i < runeCount; i++ {
		rune, runeSize := utf8.DecodeRune(bytesToParse)
		bytesToParse = bytesToParse[runeSize:]

		if i == 0 {
			isWord = unicode.IsLetter(rune)
			isDigit = unicode.IsDigit(rune)
		} else {
			if (isWord && (!unicode.IsLetter(rune) && !unicode.IsDigit(rune))) ||
				(isDigit && !unicode.IsDigit(rune)) ||
				(!isWord && unicode.IsLetter(rune)) ||
				(!isDigit && unicode.IsDigit(rune)) {
				break
			}
		}

		letters += string(rune)
	}

	it.item = letters
	it.isWord = isWord
	it.isDigit = isDigit

	return
}
Exemple #27
0
func main() {
	s := "abcdefg韩"
	s1 := s[:4] + "abc" + s[7:]

	fmt.Printf("The string [%s] has %v unicode characters\n", s, utf8.RuneCount([]byte(s)))
	fmt.Printf("The string [%s] has %v bytes\n", s, len(s))
	fmt.Printf("The old string [%s] new string [%s]\n", s, s1)
}
Exemple #28
0
func ExampleRuneCount() {
	buf := []byte("Hello, 世界")
	fmt.Println("bytes =", len(buf))
	fmt.Println("runes =", utf8.RuneCount(buf))
	// Output:
	// bytes = 13
	// runes = 9
}
func (self *WordDict) AddReplaceRule(rule []byte) {
	if utf8.RuneCount(rule) != 2 {
		self.Panic("rule format differs from '=xX'")
	}
	from, fromSize := utf8.DecodeRune(rule)
	to, _ := utf8.DecodeRune(rule[fromSize:])
	self.runeMapping[from] = to
}
Exemple #30
0
func (p *Preprocessor) calcColumn(added []byte) {
	i := bytes.LastIndex(added, []byte{_LF})
	if i >= 0 {
		p.column = 0
		added = added[i:]
	}
	p.column += utf8.RuneCount(added)
}