Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 3 of 3 total
Thread Format function
Fri, Jun 14 2013 11:43 AMPermanent Link

Christian Kaufmann

Hi,

I have this function from Delphi. Since EWB only supports "%s", I have
to rewrite the method. Any suggestions how to do it?

I found this:
https://code.google.com/p/jsxt/source/browse/trunk/js/String.js
But I don't know, if it is possible, to integrate that as "External
Code" in EWB.

cu Christian

function BSSwimTimeToStr(ASwimTime: Integer; AShowNT: Boolean):
String;
var
 h,m,s,d : Integer;
begin
 if (ASwimTime >= 1) and (ASwimTime < 2147483647) then begin
   h := ASwimTime div 3600000;
   m := (ASwimTime mod 3600000) div 60000;
   s := (ASwimTime mod 60000) div 1000;
   d := (ASwimTime mod 1000) div 10;
   if h > 0
     then Result := Format('%d:%.2d:%.2d.%.2d', [h, m, s, d])
   else if m > 0
     then Result := Format('%d:%.2d.%.2d', [m, s, d])
     else Result := Format('%d.%.2d', [s, d]);
 end
 else if AShowNT
   then Result := 'NT'
   else Result := '';
end;
Fri, Jun 14 2013 2:08 PMPermanent Link

Christian Kaufmann

I just found some more JS samples for Format():

http://stackoverflow.com/questions/610406/javascript-equivalent-to-printf-string-format

There is one, that looks quite "compact" and complete to me:

String.form = function(str, arr) {
   var i = -1;
   function callback(exp, p0, p1, p2, p3, p4) {
       if (exp=='%%') return '%';
       if (arr[++i]===undefined) return undefined;
       var exp  = p2 ? parseInt(p2.substr(1)) : undefined;
       var base = p3 ? parseInt(p3.substr(1)) : undefined;
       var val;
       switch (p4) {
           case 's': val = arr[i]; break;
           case 'c': val = arr[i][0]; break;
           case 'f': val = parseFloat(arr[i]).toFixed(exp); break;
           case 'p': val = parseFloat(arr[i]).toPrecision(exp);
break;
           case 'e': val = parseFloat(arr[i]).toExponential(exp);
break;
           case 'x': val = parseInt(arr[i]).toString(base?base:16);
break;
           case 'd': val = parseFloat(parseInt(arr[i],
base?base:10).toPrecision(exp)).toFixed(0); break;
       }
       val = typeof(val)=='object' ? JSON.stringify(val) :
val.toString(base);
       var sz = parseInt(p1); /* padding size */
       var ch = p1 && p1[0]=='0' ? '0' : ' '; /* isnull? */
       while (val.length<sz) val = p0 !== undefined ? val+ch :
ch+val; /* isminus? */
      return val;
   }
   var regex =
/%(-)?(0?[0-9]+)?([.][0-9]+)?([#][0-9]+)?([scfpexd])/g;
   return str.replace(regex, callback);
}

String.prototype.$ = function() {
   return String.form(this, Array.prototype.slice.call(arguments));
}

Here are a few examples:

String.format("%s %s", [ "This is a string", 11 ]))
console.out("%s %s".$("This is a string", 11))
var arr = [ "12.3", 13.6 ]; console.out("Array: %s".$(arr));
var obj = { test:"test", id:12 }; console.out("Object: %s".$(obj));
console.out("%c", "Test");
console.out("%5d".$(12)); // '   12'
console.out("%05d".$(12)); // '00012'
console.out("%-5d".$(12)); // '12   '
console.out("%5.2d".$(123)); // '  120'
console.out("%5.2f".$(1.1)); // ' 1.10'
console.out("%10.2e".$(1.1)); // '   1.10e+0'
console.out("%5.3p".$(1.12345)); // ' 1.12'
console.out("%5x".$(45054)); // ' affe'
console.out("%20#2x".$("45054")); // '    1010111111111110'
console.out("%6#2d".$("111")); // '     7'
console.out("%6#16d".$("affe")); // ' 4505'

cu Christian
Fri, Jun 14 2013 2:17 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Christian,

<< I have this function from Delphi. Since EWB only supports "%s", I have to
rewrite the method. Any suggestions how to do it? >>

Look at how the FormatSettings in the WebCore unit uses the TParser for
parsing the date/time formats.  You'll have to do something similar for the
various Format formatting passed in.

Tim Young
Elevate Software
www.elevatesoft.com
Image