# HG changeset patch # User Matthew Wild # Date 1290569774 0 # Node ID 16b833862ae250cce02a37cd2c0ecf70f30af307 # Parent 2286f9b970b782d4978a6cff1b6198f559b39d15 Implement basic string.format() supporting %d/%s diff -r 2286f9b970b7 -r 16b833862ae2 lvm.js --- 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);