Beispiel #1
0
func Test_Trim_Extra_Space(t *testing.T) {
	s1 := " a b  c    d\te \n"
	s2 := z.TrimExtraSpace(s1)
	if s2 != "a b c d e" {
		t.Errorf("can't trim [%s]", s1)
	}
}
Beispiel #2
0
// 解析查询字符串
func (qb *QWBuilder) Parse(kwd string) *QWord {
	kwd = z.TrimExtraSpace(kwd)
	if len(kwd) == 0 {
		return nil
	}
	isGOr, isGAnd, flds, seps := qb.extractFldsAndSeps(kwd)
	// 解析为QWord
	qword := new(QWord)
	qword.CndMap = map[string]*QCnd{}
	sseps := len(seps)
	for i, fld := range flds {
		// qcnd
		qc := qb.evalQCnd(fld)
		if qc != nil {
			qword.Cnds = append(qword.Cnds, qc)
			qword.CndMap[qc.Key] = qc
			// 分隔符
			if i < sseps {
				sp := "&"
				if isGOr {
					sp = "|"
				} else if isGAnd {
					sp = "&"
				} else if z.IsInStrings(qb.SepOr, string(seps[i])) {
					sp = "|"
				} else if z.IsInStrings(qb.SepAnd, string(seps[i])) {
					sp = "&"
				}
				qword.Rels = append(qword.Rels, sp)
			}
		} else {
			qword.Unmatchs = append(qword.Unmatchs, fld)
		}
	}
	return qword
}