Implement basic string.format() supporting %d/%s

Wed, 24 Nov 2010 03:36:14 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 24 Nov 2010 03:36:14 +0000
changeset 141
16b833862ae2
parent 140
2286f9b970b7
child 142
574e0baea136

Implement basic string.format() supporting %d/%s

lvm.js file | annotate | diff | comparison | revisions
--- a/lvm.js	Wed Nov 24 03:35:40 2010 +0000
+++ b/lvm.js	Wed Nov 24 03:36:14 2010 +0000
@@ -932,6 +932,31 @@
 			ret.push(this.LValue(result[i]));
 		return ret;
 	},
+	format: function (format_)
+	{
+		var format = format_.value, result = "";
+		var re = new RegExp("%([0-9. ]*)([a-zA-Z%])", "g");
+		var match, currpos = 0, currparam = 1;
+		while(match = re.exec(format))
+		{
+			result += format.substring(currpos, match.index);
+			currpos = re.lastIndex;
+			switch(match[2])
+			{
+			case "f": case "d":
+				if(match[1].length>0)
+					throw "string.format(): Number format modifers not yet implemented";
+			case "s":
+				result+=arguments[currparam++].value.toString();
+			case "%":
+				break;
+			default:
+				throw "string.format(): Format %"+match[2]+" not implemented";
+			}
+		}
+		result += format.substring(currpos);
+		return [this.LValue(result)];
+	},
 	gmatch: function (str, patt)
 	{
 		var re = _patternToRegExp(patt.value);

mercurial