home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Nave / sky-chopper.swf / scripts / __Packages / mx / utils / StringFormatter.as < prev    next >
Encoding:
Text File  |  2005-09-29  |  2.3 KB  |  82 lines

  1. class mx.utils.StringFormatter
  2. {
  3.    var __extractToken;
  4.    var __infuseToken;
  5.    var __tokenInfo;
  6.    var __format;
  7.    function StringFormatter(format, tokens, extractTokenFunc, infuseTokenFunc)
  8.    {
  9.       this.setFormat(format,tokens);
  10.       this.__extractToken = extractTokenFunc;
  11.       this.__infuseToken = infuseTokenFunc;
  12.    }
  13.    function extractValue(formattedData, result)
  14.    {
  15.       var _loc3_ = null;
  16.       var _loc2_ = 0;
  17.       while(_loc2_ < this.__tokenInfo.length)
  18.       {
  19.          _loc3_ = this.__tokenInfo[_loc2_];
  20.          this.__infuseToken(formattedData.substring(_loc3_.begin,_loc3_.end),_loc3_,result);
  21.          _loc2_ = _loc2_ + 1;
  22.       }
  23.    }
  24.    function formatValue(value)
  25.    {
  26.       var _loc3_ = this.__tokenInfo[0];
  27.       var _loc5_ = this.__format.substring(0,_loc3_.begin) + this.__extractToken(value,_loc3_);
  28.       var _loc4_ = _loc3_;
  29.       var _loc2_ = 1;
  30.       while(_loc2_ < this.__tokenInfo.length)
  31.       {
  32.          _loc3_ = this.__tokenInfo[_loc2_];
  33.          _loc5_ += this.__format.substring(_loc4_.end,_loc3_.begin) + this.__extractToken(value,_loc3_);
  34.          _loc4_ = _loc3_;
  35.          _loc2_ = _loc2_ + 1;
  36.       }
  37.       return _loc5_;
  38.    }
  39.    function getFormat()
  40.    {
  41.       return this.__format;
  42.    }
  43.    function setFormat(format, tokens)
  44.    {
  45.       function compareValues(a, b)
  46.       {
  47.          if(a.begin < b.begin)
  48.          {
  49.             return -1;
  50.          }
  51.          if(a.begin > b.begin)
  52.          {
  53.             return 1;
  54.          }
  55.          return 0;
  56.       }
  57.       if(format != this.__format)
  58.       {
  59.          this.__format = format;
  60.          var _loc5_ = tokens.split(",");
  61.          this.__tokenInfo = new Array();
  62.          var _loc4_ = 0;
  63.          var _loc3_ = 0;
  64.          var _loc7_ = 0;
  65.          var _loc2_ = 0;
  66.          while(_loc2_ < _loc5_.length)
  67.          {
  68.             _loc4_ = format.indexOf(_loc5_[_loc2_]);
  69.             if(_loc4_ >= 0 && _loc4_ < format.length)
  70.             {
  71.                _loc3_ = format.lastIndexOf(_loc5_[_loc2_]);
  72.                _loc3_ = _loc3_ < 0 ? _loc4_ + 1 : _loc3_ + 1;
  73.                this.__tokenInfo.splice(_loc7_,0,{token:_loc5_[_loc2_],begin:_loc4_,end:_loc3_});
  74.                _loc7_ = _loc7_ + 1;
  75.             }
  76.             _loc2_ = _loc2_ + 1;
  77.          }
  78.          this.__tokenInfo.sort(compareValues);
  79.       }
  80.    }
  81. }
  82.