Advertisement
rg443

javascript printf

Jan 19th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // printf.js - version 1.1.0
  2. //
  3. //  http://www.onicos.com/staff/iz/amuse/javascript/expert/printf.txt
  4. //
  5. //  This library is free software; you can redistribute it and/or
  6. //  modify it under the terms of the GNU Lesser General Public
  7. //  License as published by the Free Software Foundation; either
  8. //  version 2.1 of the License, or (at your option) any later version.
  9. //
  10. //  This library is distributed in the hope that it will be useful,
  11. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. //  Lesser General Public License for more details.
  14. //
  15. //  You should have received a copy of the GNU Lesser General Public
  16. //  License along with this library; if not, write to the Free Software
  17. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  18. //
  19. //
  20. // SYNOPSIS:
  21. //   printf("format", ...);
  22. //   str = sprintf("format", ...);
  23. //
  24. // Chages:
  25. // 2002-02-04  Masanao Izumo <mo@goice.co.jp>
  26. //             - Fixed bug about sprintf("%%") will return "%%".
  27. //             - Evaluate undefined "%" argument.  That is:
  28. //                   numerical value ===> 0 (%d, %x, %o, etc)
  29. //                   string value    ===> ''    (%s)
  30.  
  31. // printf(format, ...);
  32. function printf() {
  33.   document.write(va_sprintf(printf.arguments));
  34. }
  35.  
  36. // str = sprintf(format, ...);
  37. function sprintf() {
  38.   return va_sprintf(sprintf.arguments);
  39. }
  40.  
  41. function va_sprintf(args) {
  42.   var ch;
  43.   var value;
  44.   var longflag;
  45.   var ljust;
  46.   var len, llen;
  47.   var zpad;
  48.   var p;
  49.   var output;
  50.   var format_index, arg_index;
  51.   var argc, argv;
  52.   var specin;
  53.   var format;
  54.  
  55.   output = '';
  56.   format_index = 0;
  57.   arg_index = 1;
  58.   argv = args;
  59.   argc = args.length;
  60.   format = args[0];
  61.  
  62.   while (format_index < format.length) {
  63.     ch = format.substr(format_index++, 1);
  64.     if (ch != '%' || format_index == format.length) {
  65.       output += ch;
  66.     } else {
  67.       // ch == '%'
  68.       ljust = len = zpad = longflag = 0;
  69.       llen = -1;
  70.       p = format_index;
  71.       specin = true;
  72.  
  73.       while (specin) {
  74.     ch = format.substr(format_index++, 1);
  75.     switch(ch) {
  76.     case '-':
  77.       ljust = 1;
  78.           continue;
  79.  
  80.     case '0':         // set zero padding if len not set
  81.       if(len == 0)
  82.         zpad = 1;
  83.       // FALLTHROUGH
  84.     case '1': case '2': case '3':
  85.     case '4': case '5': case '6':
  86.     case '7': case '8': case '9':
  87.       len = len * 10 + parseInt(ch);
  88.       continue;
  89.  
  90.     case '.':
  91.       llen = len;
  92.       len = 0;
  93.       continue;
  94.  
  95.     case '*':
  96.       if (arg_index < argc)
  97.         len = parseInt(argv[arg_index++]);
  98.       else
  99.         len = 0;
  100.       if (len < 0) {
  101.         ljust = 1;
  102.         len = -len;
  103.       }
  104.       continue;
  105.  
  106.     case 'l':
  107.       longflag = 1;
  108.       continue;
  109.  
  110.     case 'u': case 'U':
  111.       if (arg_index < argc) {
  112.         if (longflag) {
  113.           value = parseInt(argv[arg_index++]);
  114.         } else {
  115.           value = parseInt(argv[arg_index++]);
  116.           value %= 4294967296;
  117.         }
  118.       } else {
  119.         value = 0;
  120.       }
  121.       output += _dopr_fmtnum(value, 10,0, ljust, len, zpad);
  122.       break;
  123.  
  124.     case 'o': case 'O':
  125.       if (arg_index < argc) {
  126.         if (longflag) {
  127.           value = parseInt(argv[arg_index++]);
  128.         } else {
  129.           value = parseInt(argv[arg_index++]);
  130.           value %= 4294967296;
  131.         }
  132.       } else {
  133.         value = 0;
  134.       }
  135.       output += _dopr_fmtnum(value, 8,0, ljust, len, zpad);
  136.       break;
  137.  
  138.     case 'd': case 'D':
  139.       if (arg_index < argc) {
  140.         if (longflag) {
  141.           value = parseInt(argv[arg_index++]);
  142.         } else {
  143.           value = parseInt(argv[arg_index++]);
  144.           value %= 4294967296;
  145.         }
  146.       } else {
  147.         value = 0;
  148.       }
  149.       output += _dopr_fmtnum(value, 10,1, ljust, len, zpad);
  150.       break;
  151.  
  152.     case 'x':
  153.       if (arg_index < argc) {
  154.         if (longflag) {
  155.           value = parseInt(argv[arg_index++]);
  156.         } else {
  157.           value = parseInt(argv[arg_index++]);
  158.           value %= 4294967296;
  159.         }
  160.       } else {
  161.         value = 0;
  162.       }
  163.       output += _dopr_fmtnum(value, 16,0, ljust, len, zpad);
  164.       break;
  165.  
  166.     case 'X':
  167.       if (arg_index < argc) {
  168.         if (longflag) {
  169.           value = parseInt(argv[arg_index++]);
  170.         } else {
  171.           value = parseInt(argv[arg_index++]);
  172.           value %= 4294967296;
  173.         }
  174.       } else {
  175.         value = 0;
  176.       }
  177.       output += _dopr_fmtnum(value, -16,0, ljust, len, zpad);
  178.       break;
  179.  
  180.     case 's':
  181.       if (arg_index < argc) {
  182.         value = argv[arg_index++];
  183.         if(value == null)
  184.           value = "(null)";
  185.         else
  186.           value = value + "";   // toString
  187.       } else {
  188.         value = '';
  189.       }
  190.       output += _dopr_fmtstr(value, ljust, len, llen);
  191.       break;
  192.  
  193.     case 'c':
  194.       if (arg_index < argc) {
  195.         value = parseInt(argv[arg_index++]);
  196.       } else {
  197.         value = 0;
  198.       }
  199.       output += _dopr_fromCharCode(value);
  200.       break;
  201.  
  202.     case '%':
  203.       output += '%';
  204.       break;
  205.  
  206. /* Not supported
  207.     case 'f': case 'e': case 'E': case 'g': case 'G':
  208.       if (arg_index < argc) {
  209.         value = argv[arg_index++];
  210.       } else {
  211.         value = 0.0;
  212.       }
  213.       output += _dopr_fmtdouble(format.substr(p, format_index - p), value);
  214.       break;
  215. */
  216.  
  217.     default:
  218.       if(p + 1 == format_index) {
  219.         output += '%';
  220.         output += ch;
  221.       }
  222.       else {
  223.         // alert("format error: " + format);
  224.       }
  225.       break;
  226.     }
  227.     specin = false;
  228.       }
  229.     }
  230.   }
  231.   return output;
  232. }
  233.  
  234. // Private function
  235. function _dopr_fmtnum(value, base, dosign, ljust, len, zpad)
  236. {
  237.   var signvalue = '';
  238.   var uvalue;
  239.   var place = 0;
  240.   var padlen;       // amount to pad
  241.   var caps = 0;
  242.   var convert;
  243.   var output;
  244.  
  245.   convert = '';
  246.   output = '';
  247.  
  248.   if(value >= 0)
  249.     uvalue = value;
  250.   else
  251.     uvalue = (value % 4294967296) + 4294967296;
  252.  
  253.   if (dosign) {
  254.     if (value < 0) {
  255.       signvalue = '-';
  256.       uvalue = -value;
  257.     }
  258.   }
  259.  
  260.   if (base < 0) {
  261.     caps = 1;
  262.     base = -base;
  263.   }
  264.  
  265.   if(uvalue == 0) {
  266.     convert = '0';
  267.     place = 1;
  268.   } else {
  269.     while (uvalue) {
  270.       if(caps)
  271.     convert = '0123456789ABCDEF'.substr(uvalue % base, 1) + convert;
  272.       else
  273.     convert = '0123456789abcdef'.substr(uvalue % base, 1) + convert;
  274.       uvalue = parseInt(uvalue / base);
  275.       place++;
  276.     }
  277.   }
  278.  
  279.   padlen = len - place;
  280.   if (padlen < 0) padlen = 0;
  281.   if (ljust) padlen = -padlen;
  282.  
  283.   if (zpad && padlen > 0) {
  284.     if(signvalue) {
  285.       output += signvalue;
  286.       --padlen;
  287.       signvalue = 0;
  288.     }
  289.  
  290.     while (padlen > 0) {
  291.       output += '0';
  292.       --padlen;
  293.     }
  294.   }
  295.  
  296.   while (padlen > 0) {
  297.     output += ' ';
  298.     --padlen;
  299.   }
  300.   if (signvalue) {
  301.     output += signvalue;
  302.   }
  303.  
  304.   output += convert;
  305.        
  306.   while(padlen < 0) {
  307.     output += ' ';
  308.     ++padlen;
  309.   }
  310.   return output;
  311. }
  312.  
  313. // Private function
  314. function _dopr_fmtstr(value, ljust, field_len, llen)
  315. {
  316.   var padlen;           // amount to pad
  317.   var slen, truncstr = 0;
  318.   var output = '';
  319.  
  320.   slen = value.length;
  321.  
  322.   if (llen != -1) {
  323.     var rlen;
  324.  
  325.     rlen = field_len;
  326.     if (slen > rlen) {
  327.       truncstr = 1;
  328.       slen = rlen;
  329.     }
  330.     field_len = llen;
  331.   }
  332.   padlen = field_len - slen;
  333.        
  334.   if (padlen < 0)
  335.     padlen = 0;
  336.   if (ljust)
  337.     padlen = -padlen;
  338.   while (padlen > 0) {
  339.     output += ' ';
  340.     --padlen;
  341.   }
  342.   if (truncstr) {
  343.     output += value.substr(0, slen);
  344.   } else {
  345.     output += value;
  346.   }
  347.  
  348.   while (padlen < 0) {
  349.     output += ' ';
  350.     ++padlen;
  351.   }
  352.   return output;
  353. }
  354.  
  355. // Private function
  356. var _dopr_fromCharCode_chars = null;
  357. function _dopr_fromCharCode(code)
  358. {
  359.   if(String.fromCharCode)
  360.     return String.fromCharCode(code);
  361.   if(!_dopr_fromCharCode_chars)
  362.     _dopr_fromCharCode_chars =
  363.       "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020" +
  364.       "\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !\"#$%&" +
  365.       "'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghi" +
  366.       "jklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211" +
  367.       "\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232" +
  368.       "\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253" +
  369.       "\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274" +
  370.       "\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315" +
  371.       "\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336" +
  372.       "\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357" +
  373.       "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377";
  374.   if(code < 0)
  375.     return "";
  376.   if(code <= 255)
  377.     return _dopr_fromCharCode_chars.substr(code, 1);
  378.   return eval(sprintf("\"\\u%04x\"", code));
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement