Posted By


aadsm on 01/12/10

Tagged


Statistics


Viewed 626 times
Favorited by 0 user(s)

ByteArray.replace


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. ByteArray.prototype.indexOf = Array.prototype.indexOf;
  2. ByteArray.prototype.replace = function(searchBytes, replaceBytes)
  3. {
  4. searchBytes = searchBytes instanceof Array ? searchBytes : [searchBytes];
  5. replaceBytes = replaceBytes instanceof Array ? replaceBytes : (replaceBytes === undefined ? [] : [replaceBytes]);
  6. if( searchBytes.length == 0 ) { return; }
  7.  
  8. var ix;
  9. var lastIndex = 0;
  10. var spliceArgs = [-1, searchBytes.length].concat(replaceBytes);
  11.  
  12. while( (ix = this.indexOf(searchBytes[0])) >= 0 )
  13. {
  14. var i = 0;
  15.  
  16. while( ++i < searchBytes.length && this[ix+i] == searchBytes[i] );
  17. if( i < searchBytes.length ) { lastIndex = ix+1; continue; }
  18.  
  19. // it's a match!!
  20. spliceArgs[0] = ix;
  21. Array.prototype.splice.apply( this, spliceArgs );
  22. ix += replaceBytes.length;
  23. }
  24.  
  25. return this;
  26. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.