/**
 * String extensions.
 *
 * @author Jakob Kruse <kruse@kruse-net.dk>
 * @version $Id: StringX.js,v 1.1 2006/03/02 20:16:59 Jakob Kruse Exp $
 */

// Returns the rightmost x characters
String.prototype.right = function (intLength) {
  if (intLength >= this.length) return this;
  else return this.substr(this.length - intLength, intLength);
};

// Trims leading and trailing whitespace
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/, '');
};

// Assuming you have a byte-stream that was encoded in UTF-8, but interpreted
// as ISO-Latin-1, the stream might contain multi-character sequences that
// should have been one character. This function attempts to fix that by
// extracting Unicode character values from UTF-8 multibyte sequences and
// re-encoding them as a UTF-8 character.
String.prototype.fix_broken_utf8 = function() {
  var unicode = "", val1, val2, val3, val4;

  for (i = 0; i < this.length; i++) {
    val1 = this.charCodeAt(i);

    if (val1 < 0x80)
    {
      // 1-byte utf-8: maps directly to latin-1
      unicode += this.charAt(i);
    }
    else if (0xC0 <= val1 && val1 <= 0xDF)
    {
      // 2-byte utf-8: extract meaningful bits
      val2 = this.charCodeAt(++i);

      val1 &= 0x1F; // 5 bits
      val2 &= 0x3F; // 6 bits

      unicode += String.fromCharCode(val1 << 6 | val2);
    }
    else if (0xE0 <= val1 && val1 <= 0xEF)
    {
      // 3-byte utf-8: extract meaningful bits
      val2 = this.charCodeAt(++i);
      val3 = this.charCodeAt(++i);

      val1 &= 0x0F; // 4 bits
      val2 &= (val2 < 0xFF) ? 0x3F : 0x00; // 6 bits - if we get something big, ignore it
      val3 &= 0x3F; // 6 bits

      unicode += String.fromCharCode(val1 << 12 | val2 << 6 | val3);
    }
    else if (0xF0 <= val1 && val1 <= 0xF7)
    {
      // 4-byte utf-8: extract meaningful bits
      val2 = this.charCodeAt(++i);
      val3 = this.charCodeAt(++i);
      val4 = this.charCodeAt(++i);

      val1 &= 0x07; // 3 bits
      val2 &= 0x3F; // 6 bits
      val3 &= 0x3F; // 6 bits
      val4 &= 0x3F; // 6 bits

      unicode += String.fromCharCode(val1 << 18 | val2 << 12 | val3 << 6 | val4);
    }
  }
  return unicode;
};

// EOF
