1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function check_param(name, parameter_types, given_params)
	if ( #given_params ~= #parameter_types ) then
		error("in call to "..name.."("..table.concat(parameter_types,", ")..") "..name.." does not take "..#given_params.." parameter(s)")
	end
	for i = 1, #parameter_types do
		given_params[i] = type(given_params[i]);
		if ( not (given_params[i] == parameter_types[i] ) ) then
			error("in call to "..name.."("..table.concat(parameter_types,", ")..") recieved "..name.."("..table.concat(given_params,", ")..")");
		end
	end
end
string.length = string.len;
function string.substr(...) check_param("string.sub", {"string", "number", "number"}, {...});
	local argv = {...};
	return string.sub(argv[1], argv[2]+1, argv[2]+argv[3]);
end
function string.insert(...) check_param("string.insert", {"string", "number", "string"}, {...});
	local argv = {...};
	return string.substr(argv[1], 0, argv[2])..argv[3]..string.substr(argv[1], argv[2], string.length(argv[1])-argv[2])
end
function string.append(...) check_param("string.append", {"string", "string"}, {...});
	local argv = {...};
	return argv[1]..argv[2];
end
function string.at(...) check_param("string.at", {"string", "number"}, {...});
	local argv = {...};
	return string.substr(argv[1],argv[2],1);
end
function string.push_back(...) check_param("string.push_back", {"string", "number"}, {...});
	local argv = {...};
	return argv[1]..string.char(argv[2]%256);
end
function string.pop_back(...) check_param("string.pop_back", {"string"}, {...});
	local argv = {...};
	return string.substr(argv[1], 0, string.length(argv[1])-1);
end
function string.empty(...) check_param("string.empty", {"string"}, {...});
	local argv = {...};
	return string.length(argv[1]) == 0;
end
function string.clear(...) check_param("string.clear", {"string"}, {...});
	local argv = {...};
	return string.substr(argv[1],0,0);
end
function string.replace(...) check_param("string.replace", {"number", "number", "string", "string"}, {...});
	local argv = {...};
	return string.substr(argv[3], 0, argv[1])..argv[4]..string.substr(argv[3], argv[2]+1, string.length(argv[3])-argv[2]);
end
function string.copy(...) check_param("string.copy", {"string", "number", "number"}, {...});
	local argv = {...};
	return string.substr(argv[1], argv[3], argv[2]);
end
function string.find(...) check_param("string.find", {"string", "string"}, {...});
	local argv = {...};
	for i = 0, string.length(argv[1]),1 do
		if ( string.substr(argv[1], i, string.length(argv[2])) == argv[2] ) then
			return i;
		end
	end
	return -1;
end
function string.compare(...) check_param("string.compare", {"string", "string"}, {...});
	local argv = {...};
	local ret = 0;
	local min = math.min(string.length(argv[1]), string.length(argv[2]));
	for i = 0, min, 1 do
		if ( string.substr(argv[1], i, 1) ~= string.substr(argv[2], i, 1) ) then
			ret = ret + 1;
		end
	end
	ret = ret + math.max(string.length(argv[1]), string.length(argv[2])) - min;
	return ret;
end