Пример #1
0
// Count the occurences of a pattern in input
func reCount(pattern []byte, input []byte) (count int) {
	// Compile regexp
	so := C.Tcl_NewStringObj((*C.char)(data(pattern)),
		(C.int)(len(pattern)))
	C.ref(so)
	defer C.dref(so)
	re := C.Tcl_GetRegExpFromObj(nil, so,
		C.TCL_REG_ADVANCED|C.TCL_REG_NOCASE|C.TCL_REG_NEWLINE)
	if re == nil {
		panic("Could not compile regexp \"" + string(pattern) + "\"")
	}

	// Count occurances.
	in := C.Tcl_NewStringObj((*C.char)(data(input)), (C.int)(len(input)))
	idx_max := len(input)
	for idx := 0; idx < idx_max; {
		rv := C.Tcl_RegExpExecObj(nil, re, in, C.int(idx), 1, 0)
		if rv == -1 {
			panic("Tcl_RegExpExecObj")
		}
		if rv == 0 {
			break
		}
		var info C.Tcl_RegExpInfo
		C.Tcl_RegExpGetInfo(re, &info)
		idx += int(C.end(&info, 0))
		count++
	}
	return
}
Пример #2
0
func go_value_to_tcl_obj(value interface{}) *C.Tcl_Obj {
	v := reflect.ValueOf(value)
	switch v.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Int()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Uint()))
	case reflect.Float32, reflect.Float64:
		return C.Tcl_NewDoubleObj(C.double(v.Float()))
	case reflect.Bool:
		if v.Bool() {
			return C.Tcl_NewBooleanObj(1)
		}
		return C.Tcl_NewBooleanObj(0)
	case reflect.String:
		s := v.String()
		sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
		return C.Tcl_NewStringObj((*C.char)(unsafe.Pointer(sh.Data)), C.int(len(s)))
	}
	return nil
}
Пример #3
0
func go_value_to_tcl_obj(value interface{}, interp *interpreter) *C.Tcl_Obj {
	v := reflect.ValueOf(value)
	switch v.Kind() {
	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
		return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Int()))
	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
		return C.Tcl_NewWideIntObj(C.Tcl_WideInt(v.Uint()))
	case reflect.Float32, reflect.Float64:
		return C.Tcl_NewDoubleObj(C.double(v.Float()))
	case reflect.Bool:
		if v.Bool() {
			return C.Tcl_NewBooleanObj(1)
		}
		return C.Tcl_NewBooleanObj(0)
	case reflect.String:
		s := v.String()
		sh := *(*reflect.StringHeader)(unsafe.Pointer(&s))
		return C.Tcl_NewStringObj((*C.char)(unsafe.Pointer(sh.Data)), C.int(len(s)))
	case reflect.Slice, reflect.Array:
		objv := make([]*C.Tcl_Obj, v.Len())
		for i := 0; i < v.Len(); i++ {
			objv[i] = go_value_to_tcl_obj(v.Index(i).Interface(), interp)
		}

		return C.Tcl_NewListObj(C.int(len(objv)), &objv[0])
	case reflect.Map:
		tclmap := C.Tcl_NewDictObj()

		for _, value := range v.MapKeys() {
			key := go_value_to_tcl_obj(value.Interface(), interp)
			val := go_value_to_tcl_obj(v.MapIndex(value).Interface(), interp)
			if key != nil && val != nil {
				C.Tcl_DictObjPut(interp.C, tclmap, key, val)
			}
		}
		return tclmap
	}
	return nil
}