/library/sprintf.js
JavaScript | 57 lines | 51 code | 3 blank | 3 comment | 47 complexity | 5ec31497a46ed1ff0f4f9a222caaa19b MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
-
- // args is a pyjslib_List
- // str is a String
- function sprintf(str, args)
- {
- if(args && args.__class__ != "pyjslib_List")
- args = new pyjslib_List([args]);
- if (!args || pyjslib_len(args) < 1 || !RegExp)
- {
- return;
- }
- var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
- var a = b = [], numSubstitutions = 0, numMatches = 0;
- while (a = re.exec(str))
- {
- var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
- var pPrecision = a[5], pType = a[6], rightPart = a[7];
-
- //alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);
-
- numMatches++;
- if (pType == '%')
- {
- subst = '%';
- }
- else
- {
- if (numSubstitutions >= args.length)
- {
- alert('Error! Not enough function args (' + (args.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
- }
- var param = args.__getitem__(numSubstitutions);
- var pad = '';
- if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
- else if (pPad) pad = pPad;
- var justifyRight = true;
- if (pJustify && pJustify === "-") justifyRight = false;
- var minLength = -1;
- if (pMinLength) minLength = parseInt(pMinLength);
- var precision = -1;
- if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
- var subst = param;
- if (pType == 'b') subst = parseInt(param).toString(2);
- else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
- else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
- else if (pType == 'u') subst = Math.abs(param);
- else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
- else if (pType == 'o') subst = parseInt(param).toString(8);
- else if (pType == 's') subst = param;
- else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
- else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
- numSubstitutions++;
- }
- str = leftpart + subst + rightPart;
- }
- return str;
- }