Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Found this script that implements sprintf - and that's exactly what I was looking for. Thanks to Manish Jethani!</p> <pre><code>/* sprintf(3) implementation in ActionScript 3.0. * * Author: Manish Jethani (manish.jethani@gmail.com) * Date: April 3, 2006 * Version: 0.1 * * Copyright (c) 2006 Manish Jethani * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ package { /* sprintf(3) implementation in ActionScript 3.0. * * http://www.die.net/doc/linux/man/man3/sprintf.3.html * * The following flags are supported: '#', '0', '-', '+' * * Field widths are fully supported. '*' is not supported. * * Precision is supported except one difference from the standard: for an * explicit precision of 0 and a result string of "0", the output is "0" * instead of an empty string. * * Length modifiers are not supported. * * The following conversion specifiers are supported: 'd', 'i', 'o', 'u', 'x', * 'X', 'f', 'F', 'c', 's', '%' * * Report bugs to manish.jethani@gmail.com */ public function sprintf(format:String, ... args):String { var result:String = ""; var length:int = format.length; for (var i:int = 0; i &lt; length; i++) { var c:String = format.charAt(i); if (c == "%") { var pastFieldWidth:Boolean = false; var pastFlags:Boolean = false; var flagAlternateForm:Boolean = false; var flagZeroPad:Boolean = false; var flagLeftJustify:Boolean = false; var flagSpace:Boolean = false; var flagSign:Boolean = false; var fieldWidth:String = ""; var precision:String = ""; c = format.charAt(++i); while (c != "d" &amp;&amp; c != "i" &amp;&amp; c != "o" &amp;&amp; c != "u" &amp;&amp; c != "x" &amp;&amp; c != "X" &amp;&amp; c != "f" &amp;&amp; c != "F" &amp;&amp; c != "c" &amp;&amp; c != "s" &amp;&amp; c != "%") { if (!pastFlags) { if (!flagAlternateForm &amp;&amp; c == "#") flagAlternateForm = true; else if (!flagZeroPad &amp;&amp; c == "0") flagZeroPad = true; else if (!flagLeftJustify &amp;&amp; c == "-") flagLeftJustify = true; else if (!flagSpace &amp;&amp; c == " ") flagSpace = true; else if (!flagSign &amp;&amp; c == "+") flagSign = true; else pastFlags = true; } if (!pastFieldWidth &amp;&amp; c == ".") { pastFlags = true; pastFieldWidth = true; c = format.charAt(++i); continue; } if (pastFlags) { if (!pastFieldWidth) fieldWidth += c; else precision += c; } c = format.charAt(++i); } switch (c) { case "d": case "i": var next:* = args.shift(); var str:String = String(Math.abs(int(next))); if (precision != "") str = leftPad(str, int(precision), "0"); if (int(next) &lt; 0) str = "-" + str; else if (flagSign &amp;&amp; int(next) &gt;= 0) str = "+" + str; if (fieldWidth != "") { if (flagLeftJustify) str = rightPad(str, int(fieldWidth)); else if (flagZeroPad &amp;&amp; precision == "") str = leftPad(str, int(fieldWidth), "0"); else str = leftPad(str, int(fieldWidth)); } result += str; break; case "o": var next:* = args.shift(); var str:String = uint(next).toString(8); if (flagAlternateForm &amp;&amp; str != "0") str = "0" + str; if (precision != "") str = leftPad(str, int(precision), "0"); if (fieldWidth != "") { if (flagLeftJustify) str = rightPad(str, int(fieldWidth)); else if (flagZeroPad &amp;&amp; precision == "") str = leftPad(str, int(fieldWidth), "0"); else str = leftPad(str, int(fieldWidth)); } result += str; break; case "u": var next:* = args.shift(); var str:String = uint(next).toString(10); if (precision != "") str = leftPad(str, int(precision), "0"); if (fieldWidth != "") { if (flagLeftJustify) str = rightPad(str, int(fieldWidth)); else if (flagZeroPad &amp;&amp; precision == "") str = leftPad(str, int(fieldWidth), "0"); else str = leftPad(str, int(fieldWidth)); } result += str; break; case "X": var capitalise:Boolean = true; case "x": var next:* = args.shift(); var str:String = uint(next).toString(16); if (precision != "") str = leftPad(str, int(precision), "0"); var prepend:Boolean = flagAlternateForm &amp;&amp; uint(next) != 0; if (fieldWidth != "" &amp;&amp; !flagLeftJustify &amp;&amp; flagZeroPad &amp;&amp; precision == "") str = leftPad(str, prepend ? int(fieldWidth) - 2 : int(fieldWidth), "0"); if (prepend) str = "0x" + str; if (fieldWidth != "") { if (flagLeftJustify) str = rightPad(str, int(fieldWidth)); else str = leftPad(str, int(fieldWidth)); } if (capitalise) str = str.toUpperCase(); result += str; break; case "f": case "F": var next:* = args.shift(); var str:String = Math.abs(Number(next)).toFixed( precision != "" ? int(precision) : 6); if (int(next) &lt; 0) str = "-" + str; else if (flagSign &amp;&amp; int(next) &gt;= 0) str = "+" + str; if (flagAlternateForm &amp;&amp; str.indexOf(".") == -1) str += "."; if (fieldWidth != "") { if (flagLeftJustify) str = rightPad(str, int(fieldWidth)); else if (flagZeroPad &amp;&amp; precision == "") str = leftPad(str, int(fieldWidth), "0"); else str = leftPad(str, int(fieldWidth)); } result += str; break; case "c": var next:* = args.shift(); var str:String = String.fromCharCode(int(next)); if (fieldWidth != "") { if (flagLeftJustify) str = rightPad(str, int(fieldWidth)); else str = leftPad(str, int(fieldWidth)); } result += str; break; case "s": var next:* = args.shift(); var str:String = String(next); if (precision != "") str = str.substring(0, int(precision)); if (fieldWidth != "") { if (flagLeftJustify) str = rightPad(str, int(fieldWidth)); else str = leftPad(str, int(fieldWidth)); } result += str; break; case "%": result += "%"; } } else { result += c; } } return result; } } // Private functions function leftPad(source:String, targetLength:int, padChar:String = " "):String { if (source.length &lt; targetLength) { var padding:String = ""; while (padding.length + source.length &lt; targetLength) padding += padChar; return padding + source; } return source; } function rightPad(source:String, targetLength:int, padChar:String = " "):String { while (source.length &lt; targetLength) source += padChar; return source; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload