From: miker Date: Tue, 28 Nov 2006 01:30:39 +0000 (+0000) Subject: adding sprintf stuff to support unicode character escaping in marcxml X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=8d84bbfaea64d8f1542c97656b727a9142ead1ba;p=Evergreen.git adding sprintf stuff to support unicode character escaping in marcxml git-svn-id: svn://svn.open-ils.org/ILS/trunk@6621 dcc99617-32d9-48b4-a31d-7c20da2025e4 --- diff --git a/Open-ILS/xul/staff_client/chrome/content/util/sprintf.js b/Open-ILS/xul/staff_client/chrome/content/util/sprintf.js new file mode 100644 index 0000000000..5a9894edb3 --- /dev/null +++ b/Open-ILS/xul/staff_client/chrome/content/util/sprintf.js @@ -0,0 +1,182 @@ +/* Copyright (c) 2005 Scott S. McCoy + * This was originally a non-object oriented interface +* Function printf(format_string,arguments...) + * Javascript emulation of the C printf function (modifiers and argument types + * "p" and "n" are not supported due to language restrictions) + * + * Copyright 2003 K&L Productions. All rights reserved + * http://www.klproductions.com + * + * Terms of use: This function can be used free of charge IF this header is not + * modified and remains with the function code. + * + * Legal: Use this code at your own risk. K&L Productions assumes NO resposibility + * for anything. + ********************************************************************************/ + +String.prototype.sprintf = function () { + var fstring = this.toString(); + + var pad = function(str,ch,len) { var ps=''; + for(var i=0; i0?str+ps:ps+str; + }; + var processFlags = function(flags,width,rs,arg) { + var pn = function(flags,arg,rs) { + if(arg>=0) { + if(flags.indexOf(' ')>=0) { + rs = ' ' + rs; + } else if(flags.indexOf('+')>=0) { + rs = '+' + rs; + } + } else { + rs = '-' + rs; + } + return rs; + }; + var iWidth = parseInt(width,10); + if(width.charAt(0) == '0') { + var ec=0; + if(flags.indexOf(' ')>=0 || flags.indexOf('+')>=0) { + ec++; + } + if(rs.length<(iWidth-ec)) { + rs = pad(rs,'0',rs.length-(iWidth-ec)); + } + return pn(flags,arg,rs); + } + rs = pn(flags,arg,rs); + if(rs.length=0) { + rs = rs.replace(/^(.*)(e.*)$/,'$1.$2'); + } + return processFlags(flags,width,rs,arg); + }; + converters.f = function(flags,width,precision,arg) { + iPrecision = parseInt(precision, 10); + if(isNaN(iPrecision)) { + iPrecision = 6; + } + rs = (Math.abs(arg)).toFixed(iPrecision); + if(rs.indexOf('.')<0 && flags.indexOf('#')>=0) { + rs = rs + '.'; + } + return processFlags(flags,width,rs,arg); + }; + converters.G = function(flags,width,precision,arg) { + return (converters.g(flags,width,precision,arg)).toUpperCase(); + }; + converters.g = function(flags,width,precision,arg) { + iPrecision = parseInt(precision, 10); + absArg = Math.abs(arg); + rse = absArg.toExponential(); + rsf = absArg.toFixed(6); + if(!isNaN(iPrecision)) { + rsep = absArg.toExponential(iPrecision); + rse = rsep.length < rse.length ? rsep : rse; + rsfp = absArg.toFixed(iPrecision); + rsf = rsfp.length < rsf.length ? rsfp : rsf; + } + if(rse.indexOf('.')<0 && flags.indexOf('#')>=0) { + rse = rse.replace(/^(.*)(e.*)$/,'$1.$2'); + } + if(rsf.indexOf('.')<0 && flags.indexOf('#')>=0) { + rsf = rsf + '.'; + } + rs = rse.length=0) { + rs='0'+rs; + } + return processFlags(flags,width,rs,arg); + }; + converters.X = function(flags,width,precision,arg) { + return (converters.x(flags,width,precision,arg)).toUpperCase(); + }; + converters.x = function(flags,width,precision,arg) { + var iPrecision=parseInt(precision, 10); + arg = Math.abs(arg); + var rs = Math.round(arg).toString(16); + if(rs.length=0) { + rs='0x'+rs; + } + return processFlags(flags,width,rs,arg); + }; + converters.s = function(flags,width,precision,arg) { + var iPrecision=parseInt(precision, 10); + var rs = arg; + if(rs.length > iPrecision) { + rs = rs.substring(0,iPrecision); + } + return processFlags(flags,width,rs,0); + }; + + farr = fstring.split('%'); + retstr = farr[0]; + fpRE = /^([-+ #]*)(?:(\d*)\$|)(\d*)\.?(\d*)([cdieEfFgGosuxX])(.*)$/; + for(var i = 1; i +