<commit_before>package main

import "strings"

func initRString() *RObject {
	obj := &RObject{}
	obj.name = "RString"
	obj.ivars = make(map[string]Object)
	obj.class = nil
	obj.methods = make(map[string]*RMethod)

	// RString method initialization
	obj.methods["new"] = &RMethod{gofunc: RString_new}
	obj.methods["to_s"] = &RMethod{gofunc: RString_to_s}
	obj.methods["inspect"] = &RMethod{gofunc: RString_inspect}
	obj.methods["size"] = &RMethod{gofunc: RString_length}
	obj.methods["len"] = &RMethod{gofunc: RString_length}
	obj.methods["split"] = &RMethod{gofunc: RString_split}

	return obj
}

// String.new(str='')
// v = [string]
func RString_new(vm *GobiesVM, receiver Object, v []Object) Object {
	str := ""
	if len(v) == 1 {
		str = v[0].(string)
	}

	obj := &RObject{}
	obj.class = vm.consts["RString"]
	obj.val.str = str

	return obj
}

func RString_to_s(vm *GobiesVM, receiver Object, v []Object) Object {
	obj := receiver.(*RObject)
	return obj.val.str
}

func RString_inspect(vm *GobiesVM, receiver Object, v []Object) Object {
	obj := receiver.(*RObject)
	str := obj.val.str
	array := []string{"'", str, "'"}
	return strings.Join(array, "")
}

func RString_length(vm *GobiesVM, receiver Object, v []Object) Object {
	obj := receiver.(*RObject)
	arg := make([]Object, 1, 1)
	arg[0] = int64(len(obj.val.str))
	return RFixnum_new(vm, receiver, arg)
}

func RString_split(vm *GobiesVM, receiver Object, v []Object) Object {
	obj := receiver.(*RObject)
	sep := v[0].(*RObject).val.str

	// Manually escape linebreak if any
	sep = strings.Replace(sep, "\\n", "\n", -1)

	strList := strings.Split(obj.val.str, sep)
	arg := make([]Object, len(strList), len(strList))
	for i, v := range strList {
		dummy_arg := make([]Object, 1, 1)
		dummy_arg[0] = v
		arg[i] = RString_new(vm, nil, dummy_arg)
	}
	return RArray_new(vm, nil, arg)
}<commit_msg>[RString] modify split() behavior to fit MRI stdlib<commit_after>package main

import "strings"

func initRString() *RObject {
	obj := &RObject{}
	obj.name = "RString"
	obj.ivars = make(map[string]Object)
	obj.class = nil
	obj.methods = make(map[string]*RMethod)

	// RString method initialization
	obj.methods["new"] = &RMethod{gofunc: RString_new}
	obj.methods["to_s"] = &RMethod{gofunc: RString_to_s}
	obj.methods["inspect"] = &RMethod{gofunc: RString_inspect}
	obj.methods["size"] = &RMethod{gofunc: RString_length}
	obj.methods["len"] = &RMethod{gofunc: RString_length}
	obj.methods["split"] = &RMethod{gofunc: RString_split}

	return obj
}

// String.new(str='')
// v = [string]
func RString_new(vm *GobiesVM, receiver Object, v []Object) Object {
	str := ""
	if len(v) == 1 {
		str = v[0].(string)
	}

	obj := &RObject{}
	obj.class = vm.consts["RString"]
	obj.val.str = str

	return obj
}

func RString_to_s(vm *GobiesVM, receiver Object, v []Object) Object {
	obj := receiver.(*RObject)
	return obj.val.str
}

func RString_inspect(vm *GobiesVM, receiver Object, v []Object) Object {
	obj := receiver.(*RObject)
	str := obj.val.str
	array := []string{"'", str, "'"}
	return strings.Join(array, "")
}

func RString_length(vm *GobiesVM, receiver Object, v []Object) Object {
	obj := receiver.(*RObject)
	arg := make([]Object, 1, 1)
	arg[0] = int64(len(obj.val.str))
	return RFixnum_new(vm, receiver, arg)
}

func RString_split(vm *GobiesVM, receiver Object, v []Object) Object {
	obj := receiver.(*RObject)
	sep := v[0].(*RObject).val.str
	str := obj.val.str

	// Manually unescape linebreak if any
	sep = strings.Replace(sep, "\\n", "\n", -1)

	strList := []string{str}

	// Split string individually for each separator
	for _, pass := range sep {
		oldStrList := []string{}

		for _, item := range strList {
			oldStrList = append(oldStrList, item)
		}
		strList = []string{}
		for _, item := range oldStrList {
			tempStrList := strings.Split(item, string(pass))
			for _, splitted := range tempStrList {
				if len(splitted) == 0 {
					continue
				}
				strList = append(strList, splitted)
			}
		}
	}

	arg := make([]Object, len(strList), len(strList))
	for i, v := range strList {
		dummy_arg := make([]Object, 1, 1)
		dummy_arg[0] = v
		arg[i] = RString_new(vm, nil, dummy_arg)
	}
	return RArray_new(vm, nil, arg)
}
{'fraction_non_alphanumeric': 0.17257060794638582, 'fraction_numerical': 0.005983724269985639, 'mean_word_length': 4.41321243523316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2076404', 'n_tokens_mistral': 1666, 'n_tokens_neox': 1597, 'n_words': 371}