lvm.js

changeset 139
055fe658d0b2
parent 138
f9bb0e212d28
child 140
2286f9b970b7
equal deleted inserted replaced
138:f9bb0e212d28 139:055fe658d0b2
896 } 896 }
897 return new RegExp(regexp, "g"); 897 return new RegExp(regexp, "g");
898 }; 898 };
899 899
900 var string = { 900 var string = {
901 "char": function ()
902 {
903 var nArgs = arguments.length;
904 if(nArgs < 1)
905 throw "string.char(): Expects at least 1 parameter";
906 var results = [];
907 for(var i=0; i<nArgs; i++)
908 {
909 var code = arguments[i];
910 if(code.type != "number")
911 throw "string.char(): Argument #"+(i+1)+" expected number, got "+code.type;
912 results.push(String.fromCharCode(code.value));
913 }
914 return [this.LValue(results.join(''))];
915 },
916 find: function (str, patt, init, plain)
917 {
918 if(arguments.length > 2)
919 throw "string.find(): No more than the first 2 arguments supported";
920 var re = _patternToRegExp(patt.value);
921 var result = re.exec(str);
922 if(!result)
923 return [this.LValue(null)];
924 var start = result.index+1;
925 var end = start + result[0].length - 1;
926 var ret = [this.LValue(start), this.LValue(end)];
927 for(var i=1; i<result.length; i++)
928 ret.push(this.LValue(result[i]));
929 return ret;
930 },
901 gmatch: function (str, patt) 931 gmatch: function (str, patt)
902 { 932 {
903 var re = _patternToRegExp(patt.value); 933 var re = _patternToRegExp(patt.value);
904 var matches = str.value.match(re)||[]; 934 var matches = str.value.match(re)||[];
905 var curr = 0; 935 var curr = 0;
906 var iter = function () 936 var iter = function ()
907 { 937 {
908 return [this.LValue(matches[curr++])]; 938 return [this.LValue(matches[curr++])];
909 }; 939 };
910 return [this.LValue(iter)]; 940 return [this.LValue(iter)];
941 },
942 sub: function (str, from, to)
943 {
944 var result;
945 switch(arguments.length)
946 {
947 case 0:
948 case 1:
949 throw "string.sub(): Expected 2 or more arguments";
950 case 2:
951 result = str.value.substring(from.value);
952 case 3:
953 result = str.value.substring(from.value, to.value);
954 }
955 return [this.LValue(result)];
911 } 956 }
912 }; 957 };
913 testvm.registerLib(_G, null, baselib); 958 testvm.registerLib(_G, null, baselib);
914 testvm.registerLib(_G, "math", math); 959 testvm.registerLib(_G, "math", math);
915 testvm.registerLib(_G, "string", string); 960 testvm.registerLib(_G, "string", string);

mercurial