tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

deString.js (3328B)


      1 /*-------------------------------------------------------------------------
      2 * drawElements Quality Program OpenGL ES Utilities
      3 * ------------------------------------------------
      4 *
      5 * Copyright 2014 The Android Open Source Project
      6 *
      7 * Licensed under the Apache License, Version 2.0 (the "License");
      8 * you may not use this file except in compliance with the License.
      9 * You may obtain a copy of the License at
     10 *
     11 *      http://www.apache.org/licenses/LICENSE-2.0
     12 *
     13 * Unless required by applicable law or agreed to in writing, software
     14 * distributed under the License is distributed on an "AS IS" BASIS,
     15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     16 * See the License for the specific language governing permissions and
     17 * limitations under the License.
     18 *
     19 */
     20 
     21 /**
     22 * This class allows one to create a random integer, floating point number or boolean (TODO, choose random items from a list and shuffle an array)
     23 */
     24 'use strict';
     25 goog.provide('framework.delibs.debase.deString');
     26 goog.require('framework.delibs.debase.deMath');
     27 
     28 goog.scope(function() {
     29 
     30 var deString = framework.delibs.debase.deString;
     31 var deMath = framework.delibs.debase.deMath;
     32 
     33    var DE_ASSERT = function(x) {
     34        if (!x)
     35            throw new Error('Assert failed');
     36    };
     37 
     38    /**
     39     * Compute hash from string.
     40     * @param {?string} str String to compute hash value for.
     41     * @return {number} Computed hash value.
     42     */
     43    deString.deStringHash = function(str) {
     44        /* \note [pyry] This hash is used in DT_GNU_HASH and is proven
     45        to be robust for symbol hashing. */
     46        /* \see http://sources.redhat.com/ml/binutils/2006-06/msg00418.html */
     47        /** @type {number} */ var hash = 5381;
     48        /** @type {number} */ var c;
     49 
     50        DE_ASSERT(str != undefined);
     51        if (str !== null) {
     52            var i = 0;
     53            while (i < str.length) { //(c = (unsigned int)*str++) != 0)
     54                c = str.charCodeAt(i); //trunc to 8-bit
     55                hash = (hash << 5) + hash + c;
     56                i++;
     57            }
     58        }
     59        return hash;
     60    };
     61 
     62    /**
     63     * Checks if a JS string is either empty or undefined
     64     * @param {string} str
     65     * @return {boolean}
     66     */
     67    deString.deIsStringEmpty = function(str) {
     68        if (str === undefined || str.length == 0)
     69            return true;
     70        return false;
     71    };
     72 
     73    /**
     74     * @private
     75     * @param {Object} enumType
     76     * @param {?} value
     77     * @return {string}
     78     */
     79    deString.getString = function(enumType, value) {
     80        for (var p in enumType)
     81            if (enumType[p] == value)
     82                return p;
     83 
     84        if (typeof value === 'undefined')
     85            return 'undefined';
     86 
     87        if (!value)
     88            return 'null';
     89 
     90        return value.toString(10);
     91    };
     92 
     93    /**
     94     * @param {Object} enumType
     95     * @param {?} value
     96     * @return {string}
     97     */
     98    deString.enumToString = function(enumType, value) {
     99        if (typeof deString.enumToString[enumType] === 'undefined')
    100            deString.enumToString[enumType] = {};
    101 
    102        var table = deString.enumToString[enumType];
    103        if (typeof table[value] === 'undefined') {
    104            var v = deString.getString(enumType, value);
    105            table[value] = v;
    106        }
    107 
    108        return table[value];
    109    };
    110 
    111 });