/* string_manipulation.js:  miscellaneous functions for strings

   Copyright (C) 2009 Black Mesa Technologies LLC

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program, in the Licenses subdirectory, in file
   GNU_GPL.
   
   If the license was not provided, or is missing, see 
   <http://www.gnu.org/licenses/>.

*/

/* Revisions:
   2009-03-14 : CMSMcQ : made first version (empty) of this file
*/


// 0 Random string generators

// random_minmax(n,m):  return a random number in the range
// <n, m> inclusive
function random_minmax(min, max) {
  var range = max - min + 1;
  if (range < 1) {
    return 0;
  } else {
    return min + Math.floor(Math.random() * range);
  } 
}

// random_asciichar:  return random printable-ASCII character number
function random_asciichar() {
  return random_minmax(32,127);
}

// random_octet(): return a random eight-bit value
function random_octet() {
  return random_minmax(0,255);
}

// random_ucschar(): return a random UCS character number
// i.e. something in the range U+0000 .. U+10FFFF
function random_ucschar() {
  var n = random_minmax(0,1114112);
  while ((0xD800 <= n && n <= 0xDBFF) || (0xDC00 <= n && n <= 0xDFFF)) {
    // if we got a surrogate code point, try again.
    n = random_minmax(0,1114112);
  }
  return n;
}
// 1114112 = (* 17 (expt 2 16))
// = (+ (expt 2 16) (expt 2 20))

// s_make_random(n,kind): return a string of n characters
// chosen at random from ASCII, or UCS
function s_make_random(n,kind) {
  var f;
  if (kind == "ucs") {
    f = random_ucschar;
  } else if (kind == "octets") {
    f = random_octet;
  } else {
    f = random_asciichar;
  }
  log("kind is " + kind + ", f is " + f);
  var s = "";
  for ( var i = 0; i < n; i++ ) {
    var num = f();
    var c = String.fromCharCode(num);
    log("c is " + num);
    s += c;
  }
  return s;    
}


function s_reverse_s(s) {
  var sT = "";
  for (var i = s.length - 1; i >= 0; i--) {
    sT += s.charAt(i);
  }
  return sT;
}

function s_copies_s_n(s,n) {
  var sT = "";
  if (typeof n != "number") {
    return "error: expected number, got " + n;
  }
  if (n < 0) {
    return s;
  }
  for (var i = 0; i < n; i++) {
    sT += s;
  }
  return sT;
}

function s_duplicate_s(s) {
  return s_copies_s_n(s,2);
}


// c2x(string,encoding):  translate from character to hex,
// using the named encoding.
// Currently no good way to find out what encodings are available,
// and no good error recovery ...
function c2x(s,enc) {
  if (enc == 'UTF-8') {
    return convertCP2UTF8(getCPfromChar(s));
  } else if (enc == 'UTF-16') {
    return convertCP2UTF16(getCPfromChar(s));
  } else {
    // out of options here, default to UTF-8
    return convertCP2UTF8(getCPfromChar(s));
  }
}

// x2c(string,encoding):  translate to character from hex,
// using the named encoding.
// Currently no good way to find out what encodings are available,
// and no good error recovery ...

function x2c(s,enc) {
  if (enc == 'UTF-8') {
    return convertCP2Char(getCPfromUTF8hex(s));
  } else if (enc == 'UTF-16') {
    return convertCP2Char(getCPfromUTF16hex(s));
  } else {
    // out of options here, default to octets
    return convertCP2Char(s);
  }
}

function s_center_s_n(s,n) {
  if (s.length >= n) {
    return s;
  }
  var w = n - s.length;
  var wL = Math.round(w / 2);
  var wR = w - wL;
  var padding = "                                                                                ";
  if (w > padding.length) {
    var sL = padding.substr(0,wL);
    var sR = padding.substr(0,wR);
  } else {
    // oh, the hell with it, just do it mechanically
    sL = s_copies_s_n(" ",wL);
    sR = s_copies_s_n(" ",wR);
  }
  return sL + s + sR;
}

function s_align_kw_s_n(kw,s,n) {
  if (s.length >= n) {
    return s;
  }  
  var w = n - s.length;
  var padding = "                                                                                ";
  if (w > padding.length) {
    var sT = padding.substr(0,w);
  } else {
    sT = s_copies_s_n(" ",w);
  }
  if (kw == 'left') {
    return s + sT;
  } else if (kw == 'right') {
    return sT + s;
  } else { 
    // Huh? 
    return kw + s + kw;
  }
}
function s_left_s_n(s,n) {
  return s_align_kw_s_n('left',s,n);
}
function s_right_s_n(s,n) {
  return s_align_kw_s_n('right',s,n);
}

function s_normalizespace_s(s) {
  return s.replace(/\s+/g,' ').replace(/^\s+/,'').replace(/\s+$/,'');
}
function s_strip_s(s,kw) {
  if (typeof kw == "undefined") {
    kw = "both";
  }
  switch (kw) {
  case "both":
    return s.replace(/^\s+/,'').replace(/\s+$/,'');
    break;
  case "left":
    return s.replace(/^\s+/,'');
    break;
  case "right":
    return s.replace(/\s+$/,'');
    break;
  default:
    return s;
  }
    
}

function run_process_1_and_show(eidOut, sIn) {
  var s = [];
  s[0] = sIn;
  s[1] = sIn.toLowerCase();
  s[2] = s[1].replace(/ab/,'AB');
  s[3] = s[2].replace(/Bc/,'QQQ');
  // alert("S has " + s.length + " items.");
  var target = $(eidOut);
  target.innerHTML = '';

  function make_item(sVal) {
    var eItem;
    
    eItem = document.createElement('li');
    eItem.textContent = sVal;
    target.appendChild(eItem);
  }
  for (i = 0; i < s.length; i++) {
    make_item(s[i]);
  }
}

/* some code from Ian Bicking, GPL'd */
if (typeof log == 'undefined') {
    if (typeof console != 'undefined' && typeof console.log != 'undefined') {
        log = console.log;
    } else {
        function log() {
            // FIXME: do something
        }
    }
}

if (typeof logDebug == 'undefined') {
    logDebug = log;
}

if (typeof logInfo == 'undefined') {
    logInfo = log;
}

