Exemplo n.º 1
0
func builtinString(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.AnyValue); err != nil {
		return nil, err
	}

	return runtime.NewStringValue(context.Args[0].String()), nil
}
Exemplo n.º 2
0
func stringsUpper(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue); err != nil {
		return nil, err
	}

	return runtime.NewStringValue(strings.ToUpper(context.Args[0].Str)), nil
}
Exemplo n.º 3
0
func stringsReverse(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue); err != nil {
		return nil, err
	}

	return runtime.NewStringValue(util.ReverseString(context.Args[0].Str)), nil
}
Exemplo n.º 4
0
func builtinCat(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	s := ""

	for _, arg := range context.Args {
		s += arg.String()
	}

	return runtime.NewStringValue(s), nil
}
Exemplo n.º 5
0
func stringsRuneAt(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue, runtime.NumberValue); err != nil {
		return nil, err
	}

	source := context.Args[0].Str
	index := context.Args[1].NumberToInt64()
	return runtime.NewStringValue(string([]rune(source)[index])), nil
}
Exemplo n.º 6
0
func stringsTrim(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue, runtime.StringValue); err != nil {
		return nil, err
	}

	source := context.Args[0].Str
	cutset := context.Args[1].Str
	return runtime.NewStringValue(strings.Trim(source, cutset)), nil
}
Exemplo n.º 7
0
func stringsSplit(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue, runtime.StringValue); err != nil {
		return nil, err
	}

	parts := runtime.NewListValue()

	for _, item := range strings.Split(context.Args[0].Str, context.Args[1].Str) {
		parts.List = append(parts.List, runtime.NewStringValue(item))
	}

	return parts, nil
}
Exemplo n.º 8
0
func stringsRange(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if err := runtime.ValidateArguments(context, runtime.StringValue, runtime.NumberValue, runtime.NumberValue); err != nil {
		return nil, err
	}

	source := context.Args[0].Str
	start := context.Args[1].NumberToInt64()
	end := context.Args[2].NumberToInt64()

	sourceLen := int64(len(source))

	if start < 0 || start > sourceLen || end < 0 || end > sourceLen {
		return nil, runtime.NewRuntimeError(context.Pos, "out of bounds (length is %d, trying to access %d:%d)", sourceLen, start, end)
	}

	result := source[start:end]

	return runtime.NewStringValue(result), nil
}
Exemplo n.º 9
0
func stringsReplace(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	n := -1

	err := runtime.ValidateArguments(context, runtime.StringValue, runtime.StringValue, runtime.StringValue, runtime.NumberValue)

	if err != nil {
		optionalErr := runtime.ValidateArguments(context, runtime.StringValue, runtime.StringValue, runtime.StringValue)

		if optionalErr != nil {
			return nil, err
		}
	} else {
		n = int(context.Args[3].NumberToInt64())
	}

	source := context.Args[0].Str
	search := context.Args[1].Str
	replace := context.Args[2].Str

	return runtime.NewStringValue(strings.Replace(source, search, replace, n)), nil
}
Exemplo n.º 10
0
func stringsFormat(context *runtime.FunctionCallContext) (*runtime.Value, error) {
	if len(context.Args) < 1 {
		return nil, runtime.NewRuntimeError(context.Pos, "missing format specifier")
	}

	if context.Args[0].Type != runtime.StringValue {
		return nil, runtime.NewRuntimeError(context.Pos, "format specifier should be a string")
	}

	format := context.Args[0].Str

	args := strings.Count(format, "~")

	if len(context.Args)-1 != args {
		return nil, runtime.NewRuntimeError(context.Pos, "format specifier expected %d arguments, got %d", args, len(context.Args)-1)
	}

	for _, item := range context.Args[1:] {
		format = strings.Replace(format, "~", item.String(), 1)
	}

	return runtime.NewStringValue(format), nil
}