编辑代码

import (
	"bufio"
    "fmt"
	"io"
	"strings"
    "unicode/utf8"
    )
func main() {

    inputText := "车牌号:苏A02WC7\n邮政编码:211700\n宗教信仰:伊斯兰教"
    rd := bufio.NewReaderSize(strings.NewReader(inputText), 1024)

	for {
		line, err := rd.ReadBytes('\n')

        fmt.Printf("%c \n", line)

        sz := len(line)
	for i := 0; i < sz; {
		// k:v k=v k:=v k==v, chinese big ":"
		ch, width := utf8.DecodeRune(line[i:])
        fmt.Printf("line[i:] %c \n", line[i:])
        fmt.Printf("i %v \n", i)
        fmt.Printf("width %v \n", width)
        fmt.Printf("ch %c \n", ch)
		if width == 0 { // error
			break
		}
		if i+1 < sz && isEqualChar(ch) {
			left := ""
			right := ""
			//vPos := []int{-1, -1}
			kPos := []int{-1, -1}
			isFound := false
			if i+2 < sz {
				nx, nxWidth := utf8.DecodeRune(line[i+width:])
                 fmt.Printf("%s \n", nxWidth)
				if nx == '=' {
					left, kPos = lastToken(line, i)
					//right, vPos = firstToken(line, i+width+nxWidth)
					isFound = true
				}
			}
			if !isFound {
				left, kPos = lastToken(line, i)
				//right, vPos = firstToken(line, i+width)
				isFound = true
			}
			//log.Debugf("%s [%d,%d) = %s [%d,%d)", left, kPos[0], kPos[1], right, vPos[0], vPos[1])
			_ = kPos
			if len(left) != 0 && len(right) != 0 {
				//loLeft := strings.ToLower(left)
			}
		}
		i += width
	}

        if err != nil {
			if err != io.EOF {
				// show err
			}
			break
		}
    }
}


// isEqualChar checks whether the r is = or : or :=
func isEqualChar(r rune) bool {
	return r == ':' || r == '=' || r == ':'
}

// firstToken extract the first token from bytes, returns token and position info
func firstToken(line []byte, offset int) (string, []int) {
	sz := len(line)
	if offset >= 0 && offset < sz {
		st := offset
		ed := sz
		// find first non cutter
		for i := offset; i < sz; i++ {
			if strings.IndexByte(" /\r\n\\[](){}:=\"'," , line[i]) == -1 {
				st = i
				break
			}
		}
		// find first cutter
		for i := st + 1; i < sz; i++ {
			if strings.IndexByte(" /\r\n\\[](){}:=\"'," , line[i]) != -1 {
				ed = i
				break
			}
		}
		return string(line[st:ed]), []int{st, ed}
	} else { // out of bound
		return "", nil
	}
}

// lastToken extract the last token from bytes, returns token and position info
func lastToken(line []byte, offset int) (string, []int) {
	sz := len(line)
	if offset >= 0 && offset < sz {
		st := 0
		ed := offset
		// find first non cutter
		for i := offset - 1; i >= 0; i-- {
			if strings.IndexByte(" /\r\n\\[](){}:=\"'," , line[i]) == -1 {
				ed = i + 1
				break
			}
		}
		// find first cutter
		for i := ed - 1; i >= 0; i-- {
			if strings.IndexByte(" /\r\n\\[](){}:=\"'," , line[i]) != -1 {
				st = i + 1
				break
			}
		}
		return string(line[st:ed]), []int{st, ed}
	} else {
		return "", nil
	}
}