/*====================================================
 * rapl_pkg_stringcmd.js "A Tcl like language implementation in Javascript named WebRAPL 
 * (Web Rapid Application Programming Language)"
 *
 * RAPL string commands implementation
 *
 * Released under BSD license.
 * (BSD license found at <http://www.tcl.tk/software/tcltk/license.html>)
 *
 * Arnulf Wiedemann    2011
 */

RP.add("rapl-pkg-string", function(R, name) {

function PkgString(interp) {
  R.log('constructor called', '2.life', 'PkgString', true);
  // kweight
  var strg = this;
  strg.interp = interp;
  var constructor = strg.constructor;
  PkgString.superclass.constructor.apply(strg, arguments);

  R.Base.pkg_string_oid++;
  strg.oid = R.Base.pkg_string_oid;

  /* ==================== command string first ===================================== */
  interp.registerSubCommand("::string", "first", function (interp, args) {
    var idx = 0;
    var needle_len;
    var haystack_len;
    var needle_str;
    var haystack_str;

    if (args.length != 3 && args.length != 4) {
      interp.wrongNumArgs(1, args, "subString string ?index?");
      return interp.ERROR;
    }
    needle_str = args[1].getString();
    haystack_str = args[2].getString();
    needle_len = args[1].length;
    haystack_len = args[2].length;
    if (args.length == 4) {
      var res = new Array();
      if (interp.index_obj_type.getIndex(args[3], res) != interp.OK) {
        return interp.ERROR;
      }
      idx = interp.index_obj_type.relToAbsIndex(haystack_len, res[0]);
    }
    interp.setResultInt(interp.string_obj_type.stringFirst(needle_str, needle_len, haystack_str, haystack_len, idx));
    return interp.OK;
  });

  /* ==================== command string equal ===================================== */
  interp.registerSubCommand("::string", "equal", function (interp, args) {
    var opt_case = 1;
    var idx = 1;

    if (args.length != 3 && (args.length != 4 || args[idx].getString() != "-nocase")) {
      interp.wrongNumArgs(2, args, "?-nocase? string1 string2");
      return interp.ERROR;
    }
    if (args.length == 4) {
      opt_case = 0;
      idx++;
    }
    if (!opt_case) {
      interp.setResultInt(args[idx].stringCompareObj(args[idx + 1], !opt_case));
    } else {
      interp.setResultBool(args[idx].stringEqObj(args[idx + 1]));
    }
    return interp.OK;
  });

  /* ==================== command string compare ===================================== */
  interp.registerSubCommand("::string", "compare", function (interp, args) {
    var opt_case = 1;
    var idx = 1;

    if (args.length != 3 && (args.length != 4 || args[idx].getString() != "-nocase")) {
      interp.wrongNumArgs(2, args, "?-nocase? string1 string2");
      return interp.ERROR;
    }
    if (args.length == 4) {
      opt_case = 0;
      idx++;
    }
    interp.setResultInt(args[idx].stringCompareObj(args[idx + 1], !opt_case));
    return interp.OK;
  });

  /* ==================== command string index ===================================== */
  interp.registerSubCommand("::string", "index", function (interp, args) {
    interp.setResultString("string index not yet reimplemented");
    return interp.ERROR;
    this.requireExactArgc(args, 3, "string index", "");
    var s = args[1].toString();
    try {
      return s.charAt(args[1].stringIndex(args[2]));
    } catch (e) {
      return "";
    }
  });

  /* ==================== command string last ===================================== */
  interp.registerSubCommand("::string", "last", function (interp, args) {
    var idx = 0;
    var needle_len;
    var haystack_len;
    var needle_str;
    var haystack_str;

    if (args.length != 3 && args.length != 4) {
      interp.wrongNumArgs(1, args, "subString string ?index?");
      return interp.ERROR;
    }
    needle_str = args[1].getString();
    haystack_str = args[2].getString();
    needle_len = args[1].length;
    haystack_len = args[2].length;
    if (args.length == 4) {
      var res = new Array();
      if (interp.index_obj_type.getIndex(args[3], res) != interp.OK) {
        return interp.ERROR;
      }
      idx = interp.index_obj_type.relToAbsIndex(haystack_len, res[0]);
    } else {
      idx = haystack_len;
    }
    interp.setResultInt(interp.string_obj_type.stringLast(needle_str, needle_len, haystack_str, haystack_len, idx));
    return interp.OK;
  });

  /* ==================== command string match ===================================== */
  interp.registerSubCommand("::string", "match", function (interp, args) {
    var opt_case = false;
    var i = 1;

    if (args.length != 3 && args.length != 4) {
      interp.wrongNumArgs(1, args, "?-nocase? pattern string");
      return interp.ERROR;
    }
    if (args[1].getString().charAt(0) == "-") {
      if (args[1].getString() != "-nocase") {
        interp.wrongNumArgs(1, args, "?-nocase? pattern string");
        return interp.ERROR;
      }
      opt_case  = true;
      i++;
    }
     ret_code = args[i].stringMatchObj(args[i+1], !opt_case);
    interp.setResultBool(ret_code == interp.OK);
    return interp.OK;
  });

  /* ==================== command string range ===================================== */
  interp.registerSubCommand("::string", "range", function (interp, args) {
    interp.setResultString("string range not yet reimplemented");
    return interp.ERROR;
    this.requireExactArgc(args, 4, "string range", "");
    var s = args[1];
    try {
      var b = s.stringIndex(args[2].toString());
      var e = s.stringIndex(args[3].toString());
      if (b > e) return "";
      return s.toString().substring(b, e + 1);
    } catch (e) {
      return "";
    }
  });

  /* ==================== command string tolower ===================================== */
  interp.registerSubCommand("::string", "tolower", function (interp, args) {
    var s;
    var first = 0;
    var last;
    var idx;
    var head = "";
    var tail = "";
    var mid;

    if (args.length < 2 || args.length > 4) {
      interp.wrongNumArgs(1, args, "string");
      return interp.ERROR;
    }
    s = args[1].getString();
    last = s.length;
    if (args.length > 2) {
      idx = new Array();
      if (interp.index_obj_type.getIndex(args[2], idx) != interp.OK) {
        return interp.ERROR;
      }
      first = idx[0];
      first = interp.relToAbsIndex(s.length, first);
    }
    if (args.length > 3) {
      if (interp.index_obj_type.getIndex(args[3], idx) != interp.OK) {
        return interp.ERROR;
      }
      last = idx[0];
      last = interp.relToAbsIndex(s.length, last);
    }
    if (first > last) {
      interp.setResultString("string tolower end index must be equal or greater the start index");
      return interp.ERROR;
    }
    if (first > 0) {
      head = s.substring(0, first);
    }
    mid = s.substring(first, last).toLowerCase();
    if (last < s.length) {
      tail = s.substring(last);
    }
    s = head+mid+tail;
    interp.setResultString(s);
    return interp.OK;
  });

  /* ==================== command string toupper ===================================== */
  interp.registerSubCommand("::string", "toupper", function (interp, args) {
    var s;
    var first = 0;
    var last;
    var idx;
    var head = "";
    var tail = "";
    var mid;

    if (args.length < 2 || args.length > 4) {
      interp.wrongNumArgs(1, args, "string ?first? ?last?");
      return interp.ERROR;
    }
    s = args[1].getString();
    last = s.length;
    if (args.length > 2) {
      idx = new Array();
      if (interp.index_obj_type.getIndex(args[2], idx) != interp.OK) {
        return interp.ERROR;
      }
      first = idx[0];
      first = interp.relToAbsIndex(s.length, first);
    }
    if (args.length > 3) {
      if (interp.index_obj_type.getIndex(args[3], idx) != interp.OK) {
        return interp.ERROR;
      }
      last = idx[0];
      last = interp.relToAbsIndex(s.length, last);
    }
    if (first > 0) {
      head = s.substring(0, first);
    }
    mid = s.substring(first, last).toUpperCase();
    if (last < s.length) {
      tail = s.substring(last);
    }
    s = head+mid+tail;
    interp.setResultString(s);
    return interp.OK;
  });

  /* ==================== command string trim ===================================== */
  interp.registerSubCommand("::string", "trim", function (interp, args) {
    if (args.length != 2 && args.length != 3) {
      interp.wrongNumArgs(1, args, "string ?trimchars?");
      return interp.ERROR;
    }
    var trim_chars = (args.length == 3 ? args[2] : null);
    var s = args[1];
    interp.setResult(interp.string_obj_type.stringTrim(s, trim_chars));
    return interp.OK;
  });


  /* ==================== command string trimleft ===================================== */
  interp.registerSubCommand("::string", "trimleft", function (interp, args) {
    if (args.length != 2 && args.length != 3) {
      interp.wrongNumArgs(1, args, "string ?trimchars?");
      return interp.ERROR;
    }
    var trim_chars = (args.length == 3 ? args[2] : null);
    var s = args[1];
    interp.setResult(interp.string_obj_type.stringTrimLeft(s, trim_chars));
    return interp.OK;
  });


  /* ==================== command string trimright ===================================== */
  interp.registerSubCommand("::string", "trimright", function (interp, args) {
    if (args.length != 2 && args.length != 3) {
      interp.wrongNumArgs(1, args, "string ?trimchars?");
      return interp.ERROR;
    }
    var trim_chars = (args.length == 3 ? args[2] : null);
    var s = args[1];
    interp.setResult(interp.string_obj_type.stringTrimRight(s, trim_chars));
    return interp.OK;
  });

  R.log('constructor end', '2.life', 'PkgString', true);
}

R.extend(PkgString, R.Token, {
  my_name: "PkgString",

});

PkgString.prototype.constructor = PkgString;

R.PkgString = PkgString;

}, "0.0.1", {});
