Пример #1
0
// 返回一个qwordBuilder的描述信息
func (qb *QWBuilder) String() string {
	sb := z.StringBuilder()
	kwidth := 15
	sb.Append("QWorldBuilder Setup").EOL()
	sb.Append(z.AlignLeft("gOr", kwidth, ' ')).Append(": ").Append(qb.GOr).EOL()
	sb.Append(z.AlignLeft("gAnd", kwidth, ' ')).Append(": ").Append(qb.GAnd).EOL()
	sb.Append(z.AlignLeft("sepOr", kwidth, ' ')).Append(": ").Append(qb.SepOr).EOL()
	sb.Append(z.AlignLeft("sepAnd", kwidth, ' ')).Append(": ").Append(qb.SepAnd).EOL()
	sb.Append(z.AlignLeft("quoteBegin", kwidth, ' ')).Append(": ").Append(qb.QuoteBegin).EOL()
	sb.Append(z.AlignLeft("quoteEnd", kwidth, ' ')).Append(": ").Append(qb.QuoteEnd).EOL()
	sb.Append(z.AlignLeft("bracketBegin", kwidth, ' ')).Append(": ").Append(qb.BracketBegin).EOL()
	sb.Append(z.AlignLeft("bracketEnd", kwidth, ' ')).Append(": ").Append(qb.BracketEnd).EOL()
	// QWordRule
	if len(qb.Rules) > 0 {
		sb.Append(z.AlignLeft("rules", kwidth, ' ')).Append(": ").EOL()
		for i := 0; i < len(qb.Rules); i++ {
			rule := qb.Rules[i]
			sb.Append(z.DupChar(' ', kwidth)).Append(fmt.Sprintf(" %2d. {", i))
			sb.Append(z.DupChar(' ', kwidth)).Append(z.DupChar(' ', kwidth)).Append(fmt.Sprint("{")).EOL()
			sb.Append(z.DupChar(' ', kwidth+9)).Append(fmt.Sprintf("key    : %s", rule.Key)).EOL()
			sb.Append(z.DupChar(' ', kwidth+9)).Append(fmt.Sprintf("regex  : %s", rule.Regex.String())).EOL()
			sb.Append(z.DupChar(' ', kwidth+9)).Append(fmt.Sprintf("seg    : %s", rule.Seg)).EOL()
			sb.Append(z.DupChar(' ', kwidth+9)).Append(fmt.Sprintf("type   : %v", rule.Type)).EOL()
			sb.Append(z.DupChar(' ', kwidth+5)).Append(fmt.Sprint("}")).EOL()
		}
	}
	return sb.String()
}
Пример #2
0
func Test_SBuilder(t *testing.T) {
	sb := z.StringBuilder()
	sb.Append("abc")
	if sb.String() != "abc" {
		t.Errorf("sb has error, should be %s, but be %s", "abc", sb.String())
	}
	sb.Append('d')
	if sb.String() != "abcd" {
		t.Errorf("sb has error, should be %s, but be %s", "abcd", sb.String())
	}
	if sb.Len() != 4 {
		t.Errorf("sb length should be 4, but be %d", sb.Len())
	}
	sb2 := z.StringBuilder()
	sb2.Append([]string{"acb", "111", "gdgdg"})
	if sb2.String() != "[acb 111 gdgdg]" {
		t.Errorf("sb2 has error, should be %s, but be %s", "[acb 111 gdgdg]", sb2.String())
	}
}
Пример #3
0
func (qc *QCnd) String() string {
	sb := z.StringBuilder()
	sb.Append("{").EOL()
	sb.Append("    ").Append("key   : ").Append(qc.Key).EOL()
	sb.Append("    ").Append("plain : ").Append(qc.Plain).EOL()
	sb.Append("    ").Append("type  : ").Append(qc.Type.String()).EOL()
	sb.Append("    ").Append("value : ").Append(qc.Value).EOL()
	sb.Append("}")
	return sb.String()
}
Пример #4
0
func (qword *QWord) String() string {
	sb := z.StringBuilder()
	sb.Append("rels is :").Append(qword.Rels).EOL()
	sb.Append("cnds is :\n")
	for _, cnd := range qword.Cnds {
		sb.Append(cnd.String()).EOL()
	}
	if len(qword.Unmatchs) > 0 {
		sb.Append("unmathchs is :\n")
		for _, um := range qword.Unmatchs {
			sb.Append("    " + um).EOL()
		}
	}
	return sb.String()
}
Пример #5
0
// 提取查询语句与连接符
func (qb *QWBuilder) extractFldsAndSeps(kwd string) (isGOr, isGAnd bool, flds []string, seps []byte) {
	// 判断全局参数 (先强制认为 'xxx:')
	// FIXME 后面改成正则匹配, 可以多加空格
	isGOr = strings.HasPrefix(kwd, qb.GOr+":")
	isGAnd = strings.HasPrefix(kwd, qb.GAnd+":")
	if isGOr || isGAnd {
		kwd = kwd[strings.Index(kwd, ":")+1:]
	}
	// 分割查询字符串
	flds = make([]string, 0, 5)
	seps = make([]byte, 0, 5)
	// FIXME 这个地方需要在好好想想
	// 保证一个特殊情况下的正确性, 比如或是" "与是"," kwd中有类似"xxx , yyy"这样的语句,需要把处理
	// 这个特例是一搬在" "当做或的情况下
	if z.IsInStrings(qb.SepOr, " ") {
		for _, sAnd := range qb.SepAnd {
			ss := " " + sAnd + " "
			kwd = strings.Replace(kwd, ss, sAnd, -1)
		}
	}
	// 开始解析
	kszie := len(kwd)
	fld := z.StringBuilder()
	for i := 0; i < kszie; i++ {
		c := kwd[i]
		z.DebugPrintf("extract kwd %3d -> [%s]\n", i, string(c))
		// 如果是引用
		if pos := z.IndexOfStrings(qb.QuoteBegin, string(c)); pos > 0 {
			// 这里面的空格啥的就不会被跳过了, 会一直取到引用结束, 逃逸字符会调整一下
			for i++; i < kszie; i++ {
				c = kwd[i]
				if string(c) == qb.QuoteEnd[pos] {
					break
				} else if c == '\\' {
					// FIXME 这里没有做防守
					i++
					fld.Append(kwd[i])
				} else {
					fld.Append(c)
				}
			}
			continue
		}
		// 如果是括弧
		if z.IsInStrings(qb.BracketBegin, string(c)) {
			fld.Append(c)
			for i++; i < kszie; i++ {
				c = kwd[i]
				fld.Append(c)
				if z.IsInStrings(qb.BracketEnd, string(c)) {
					break
				}
			}
			continue
		}
		// 如果是分隔符
		if z.IsInStrings(qb.SepAnd, string(c)) || z.IsInStrings(qb.SepOr, string(c)) {
			cfld := z.Trim(fld.String())
			if len(cfld) > 0 {
				flds = append(flds, cfld)
				seps = append(seps, c)
				fld = z.StringBuilder()
			}
			continue
		}
		// 没什么特殊情况,那就加入到sb
		fld.Append(c)
	}
	// 加入最后一个条件
	lfld := z.Trim(fld.String())
	if len(lfld) > 0 {
		flds = append(flds, lfld)
	}
	// 打印一下
	if z.IsDebugOn() && len(flds) > 0 {
		z.DebugPrintf("kwd : %s\n", kwd)
		for _, f := range flds {
			z.DebugPrintf("fld : %s\n", f)
		}
		for _, s := range seps {
			z.DebugPrintf("sep : '%s'\n", string(s))
		}
	}
	return isGOr, isGAnd, flds, seps
}