webtoolkit.sprintf.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. *
  3. * Javascript sprintf
  4. * http://www.webtoolkit.info/
  5. *
  6. *
  7. **/
  8. sprintfWrapper = {
  9. init : function () {
  10. if (typeof arguments == "undefined") { /*return null;*/ }
  11. if (arguments.length <1) { return null; }
  12. if (typeof arguments[0] != "string") { return null; }
  13. if (typeof RegExp == "undefined") { /*return null;*/ }
  14. var string = arguments[0];
  15. var exp = new RegExp(/(%([%]|(\-)?(\+|\x20)?(0)?(\d+)?(\.(\d)?)?([bcdfosxX])))/g);
  16. var matches = new Array();
  17. var strings = new Array();
  18. var convCount = 0;
  19. var stringPosStart = 0;
  20. var stringPosEnd = 0;
  21. var matchPosEnd = 0;
  22. var newString = '';
  23. var match = null;
  24. while (match = exp.exec(string)) {
  25. if (match[9]) { convCount += 1; }
  26. stringPosStart = matchPosEnd;
  27. stringPosEnd = exp.lastIndex - match[0].length;
  28. strings[strings.length] = string.substring(stringPosStart, stringPosEnd);
  29. matchPosEnd = exp.lastIndex;
  30. matches[matches.length] = {
  31. match: match[0],
  32. left: match[3] ? true : false,
  33. sign: match[4] || '',
  34. pad: match[5] || ' ',
  35. min: match[6] || 0,
  36. precision: match[8],
  37. code: match[9] || '%',
  38. negative: parseInt(arguments[convCount]) < 0 ? true : false,
  39. argument: String(arguments[convCount])
  40. };
  41. }
  42. strings[strings.length] = string.substring(matchPosEnd);
  43. if (matches.length == 0) { return string; }
  44. if ((arguments.length - 1) < convCount) { return null; }
  45. var code = null;
  46. var match = null;
  47. var i = null;
  48. for (i=0; i<matches.length; i++) {
  49. if (matches[i].code == '%') { substitution = '%' }
  50. else if (matches[i].code == 'b') {
  51. matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(2));
  52. substitution = sprintfWrapper.convert(matches[i], true);
  53. }
  54. else if (matches[i].code == 'c') {
  55. matches[i].argument = String(String.fromCharCode(parseInt(Math.abs(parseInt(matches[i].argument)))));
  56. substitution = sprintfWrapper.convert(matches[i], true);
  57. }
  58. else if (matches[i].code == 'd') {
  59. matches[i].argument = String(Math.abs(parseInt(matches[i].argument)));
  60. substitution = sprintfWrapper.convert(matches[i]);
  61. }
  62. else if (matches[i].code == 'f') {
  63. matches[i].argument = String(Math.abs(parseFloat(matches[i].argument)).toFixed(matches[i].precision ? matches[i].precision : 6));
  64. substitution = sprintfWrapper.convert(matches[i]);
  65. }
  66. else if (matches[i].code == 'o') {
  67. matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(8));
  68. substitution = sprintfWrapper.convert(matches[i]);
  69. }
  70. else if (matches[i].code == 's') {
  71. matches[i].argument = matches[i].argument.substring(0, matches[i].precision ? matches[i].precision : matches[i].argument.length)
  72. substitution = sprintfWrapper.convert(matches[i], true);
  73. }
  74. else if (matches[i].code == 'x') {
  75. matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
  76. substitution = sprintfWrapper.convert(matches[i]);
  77. }
  78. else if (matches[i].code == 'X') {
  79. matches[i].argument = String(Math.abs(parseInt(matches[i].argument)).toString(16));
  80. substitution = sprintfWrapper.convert(matches[i]).toUpperCase();
  81. }
  82. else {
  83. substitution = matches[i].match;
  84. }
  85. newString += strings[i];
  86. newString += substitution;
  87. }
  88. newString += strings[i];
  89. return newString;
  90. },
  91. convert : function(match, nosign){
  92. if (nosign) {
  93. match.sign = '';
  94. } else {
  95. match.sign = match.negative ? '-' : match.sign;
  96. }
  97. var l = match.min - match.argument.length + 1 - match.sign.length;
  98. var pad = new Array(l < 0 ? 0 : l).join(match.pad);
  99. if (!match.left) {
  100. if (match.pad == "0" || nosign) {
  101. return match.sign + pad + match.argument;
  102. } else {
  103. return pad + match.sign + match.argument;
  104. }
  105. } else {
  106. if (match.pad == "0" || nosign) {
  107. return match.sign + match.argument + pad.replace(/0/g, ' ');
  108. } else {
  109. return match.sign + match.argument + pad;
  110. }
  111. }
  112. }
  113. }
  114. sprintf = sprintfWrapper.init;