Javascript
您现在的位置: 学网 >> 网页设计 >> JS教程 >> JS技巧 >> 正文
学网教程

js基础:Javascript 版本的 Sprintf 字符串格式化函数

[ 来源:js实例 | 作者:js基础 | 时间:2008-2-22 | 去论坛]

/*
** sprintf.js -- POSIX sprintf(3) style formatting function for JavaScript
** Copyright (c) 2006-2007 Ralf S. Engelschall <rse@engelschall.com>
** Partly based on Public Domain code by Jan Moesen <http://jan.moesen.nu/>
** Licensed under GPL <http://www.gnu.org/licenses/gpl.txt>
**
** $LastChangedDate$
** $LastChangedRevision$
*/

/* make sure the ECMAScript 3.0 Number.toFixed() method is available */
if (typeof Number.prototype.toFixed != "undefined") {
(function(){
/* see http://www.jibbering.com/faq/#FAQ4_6 for details */
function Stretch(Q, L, c) {
var S = Q
if (c.length > 0)
while (S.length < L)
S = c+S;
return S;
}
function StrU(X, M, N) { /* X >= 0.0 */
var T, S;
S = new String(Math.round(X * Number("1e"+N)));
if (S.search && S.search(/\D/) != -1)
return ''+X;
with (new String(Stretch(S, M+N, '0')))
return substring(0, T=(length-N)) + '.' + substring(T);
}
function Sign(X) {
return X < 0 ? '-' : '';
}
function StrS(X, M, N) {
return Sign(X)+StrU(Math.abs(X), M, N);
}
Number.prototype.toFixed = function (n) { return StrS(this, 1, n) };
})();
}

/* the sprintf() function */
sprintf = function () {
/* argument sanity checking */
if (!arguments || arguments.length < 1)
alert("sprintf:ERROR: not enough arguments");

/* initialize processing queue */
var argumentnum = 0;
var done = "", todo = arguments[argumentnum++];

/* parse still to be done format string */
var m;
while (m = /^([^%]*)%(\d+$)?([#0 +'-]+)?(\*|\d+)?(\.\*|\.\d+)?([%diouxXfFcs])(.*)$/.exec(todo)) {
var pProlog = m[1],
pAccess = m[2],
pFlags = m[3],
pMinLength = m[4],
pPrecision = m[5],
pType = m[6],
pEpilog = m[7];

/* determine substitution */
var subst;
if (pType == '%')
/* special case: escaped percent character */
subst = '%';
else {
/* parse padding and justify aspects of flags */
var padWith = ' ';
var justifyRight = true;
if (pFlags) {
if (pFlags.indexOf('0') >= 0)
padWith = '0';
if (pFlags.indexOf('-') >= 0) {
padWith = ' ';
justifyRight = false;
}
}
else
pFlags = "";

/* determine minimum length */
var minLength = -1;
if (pMinLength) {
if (pMinLength == "*") {
var access = argumentnum++;
if (access >= arguments.length)
alert("sprintf:ERROR: not enough arguments");
minLength = arguments[access];
}
else
minLength = parseInt(pMinLength, 10);
}

/* determine precision */
var precision = -1;
if (pPrecision) {
if (pPrecision == ".*") {
var access = argumentnum++;
if (access >= arguments.length)
alert("sprintf:ERROR: not enough arguments");
precision = arguments[access];
}
else
precision = parseInt(pPrecision.substring(1), 10);
}

/* determine how to fetch argument */
var access = argumentnum++;
if (pAccess)
access = parseInt(pAccess.substring(0, pAccess.length - 1), 10);
if (access >= arguments.length)
alert("sprintf:ERROR: not enough arguments");

/* dispatch into expansions according to type */
var prefix = "";
switch (pType) {
case 'd':
case 'i':
subst = arguments[access];
if (typeof subst != "number")
subst = 0;
subst = subst.toString(10);
if (pFlags.indexOf('#') >= 0 && subst >= 0)
subst = "+" + subst;
if (pFlags.indexOf(' ') >= 0 && subst >= 0)
subst = " " + subst;
break;
case 'o':
subst = arguments[access];
if (typeof subst != "number")
subst = 0;
subst = subst.toString(8);
break;
case 'u':
subst = arguments[access];
if (typeof subst != "number")
subst = 0;
subst = Math.abs(subst);
subst = subst.toString(10);
break;

1
学网·特别声明:
本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。本站所有文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题,请联系编辑人员Xababy#Gmail.com,我们尽快予以更正。
设为首页 - 收藏学网 - 关于学网 - RSS订阅 - 版权申明 - 友情链接 - 联系学网 - 网站地图 - 投稿学网
学网·2004-2008版权所有
© CopyRight 2004-2008 WwW.Xue5.CoM.Inc All Rights Reserved
合作、建议、联系::cainiaoo.cn#live.cn QQ:329700200,1103290

学网_致力于提供优质免费的电脑学习教程
陕ICP备05000834号