﻿(function() {

    var STRINGS = sbs.register("sbs.strings");
	
	/*
	sbs.string.stringToHex
	
	args
	1 - string
	*required*
	and ascii encoded string
	
	returns
	hexidecimal representation of the inputted string
	*/
	var hexes = new Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f");
	STRINGS.stringToHex = function(string) {
		var len = string.length;
		var hexString = "0x";
		for (var i = 0; i < len; i++) {
			hexString += hexes[string.charCodeAt(i) >> 4] + hexes[string.charCodeAt(i) & 0xf];
		}
		return hexString;
	};

	/*
	sbs.string.hexToBytes()
	
	args
	1 - hexString
	*required*
	an ascii encoded hex string
	
	returns
	byte array
	*/
	var invHexes = { "0":0, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9
					, "A":10, "a":10, "B":11, "b":11, "C":12, "c":12, "D":13, "d":13, "E":14, "e":14, "F":15, "f":15 };	
	STRINGS.hexToBytes = function(hexString) {
		var hasZerox = (hexString.substring(0, 2) == "0x");
		var len = hexString.length;
		var h1, h2;
		var bytes = new Array(hasZerox ? len / 2 - 1 : len / 2);
		
		var j = 0;
		for (var i = hasZerox ? 2 : 0; i < len; i += 2) {
			h1 = invHexes[hexString[i]];
			if (typeof(h1) == 'undefined')
				throw ('hex character: ' + hexString[i] + ' is invalid');
			
			h2 = invHexes[hexString[i + 1]];
			if (typeof(h2) == 'undefined')
				throw ('hex character: ' + hexString[i + 1] + ' is invalid');
			
			bytes[j++] = h1 * 16 + h2;
		}
		
		return bytes;
	};
	
	/*
	sbs.string.hexToString()
	
	args
	1 - hexString
	*required*
	an ascii encoded hex string
	
	returns
	string whose characters are the character codes represented by the hex string
	*/
	STRINGS.hexToString = function(hexString) {
		var string = '';
		var bytes = STRINGS.hexToBytes(hexString);
		var len = bytes.length;
		
		for (var i = 0; i < len; i++) {
			string += String.fromCharCode(bytes[i]);
		}
		
		return string;
	};

    /*
    sbs.strings.trimAfter
    removes all text from a string after a character is found

    args
    1 - string
    *required*
    the string to parse
    
    2 - character
    *required*
    the character to trim all text after it is found from the string
    
    3 - startIndex
    *optional* (0)
    the index to begin searching at
    
    returns
    if character is found: shortened string
    else: original string
    */
    STRINGS.trimAfter = function(string, character, startIndex) {
        if (startIndex === null) startIndex = 0;

        var calcIndex = string.indexOf(character, startIndex);

        if (calcIndex == -1)
            return string;
        else
            return string.substring(0, calcIndex);
    };

    /*
    sbs.strings.trimBefore
    removes all text from a string before a character is found

    args
    1 - string
    *required*
    the string to parse
    
    2 - character
    *required*
    the character to trim all text after it is found from the string
    
    3 - startIndex
    *optional* (0)
    the index to begin searching at
    
    returns
    if character is found: shortened string
    else: original string
    */
    STRINGS.trimBefore = function(string, character, startIndex) {
        if (startIndex === null) startIndex = 0;

        return string.substring(string.indexOf(character, startIndex) + 1);
    };

})();