//------- String object extensions in Javascript: -----

//------ trim(): -----------------------------
// Trims whitespace at the start and the end of a string

String.prototype.trim = function()
{
  return (this.replace(/^\s+|\s+$/g,""));
}

//------ ltrim(): ---------------------------
// Trims whitespace at the start of a string

String.prototype.ltrim = function()
{
  return (this.replace(/^\s+/,""));
}

//------ rtrim(): ---------------------------
// Trims whitespace at the end of a string

String.prototype.rtrim = function()
{
//------ insert(): ---------------------------
// Inserts a string [text] into a string object at position [i]

  return (this.replace(/\s+$/,""));
}

String.prototype.insert = function (i, text)
{
  return (this.substr (0, i) + text + this.substr (i));
}

//-------- String.prototype.cmp_wc (): -------------------------
/* Compares a string with a pattern that may contain wildcard characters.
   Input:     Pttrn = pattern (may include wildcards) with which the string is to be compared
   Output:    boolean true if Pttrn matches the complete string, otherwise false
   Wildcards: "?"     for one and only one character
              "*"     for zero or more characters
              "(xyz)" for zero or more times "xyz"
*/
String.prototype.cmp_wc = function(Pttrn)
{
  // Copy the string and replace spaces and hyphens by "-":
  var Str = this.replace (/\s/g, "-");              // spaces to hyphens
  Str = Str.replace (/-+/g, "-");                   // multiple hyphens to single hyphen

  // Replace spaces and hyphens in Pttrn by "-":
  Pttrn = Pttrn.replace (/\s/g, "-");               // spaces to hyphens
  Pttrn = Pttrn.replace (/-+/g, "-");               // multiple hyphens to single hyphen

  // Convert Pttrn to regular expression:
  Pttrn = Pttrn.replace (/\?/g, "\\S\?");           // "?": one and only one character
  Pttrn = Pttrn.replace (/\*/g, "\\S\*");           // "*": zero or more characters
  Pttrn = Pttrn.replace (/\((\S*?)\)/g, "\$1\?");   // (xyz): for zero or more times "xyz" ("*?" = "non-greedy")
                                                    // (must be the last one, otherwise "?" would beconverted again)
  Pttrn = "^" + Pttrn + "$";                        // enforce testing complete string

  // test with regexp:
  var i = Str.search (Pttrn);                       // >= 0 if found
  return (i >= 0);

}    // "strcmp_wc ()"

//----------------------------------------------------------------

