<commit_before>/*
 *  Copyright 2012 Rackspace
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

var spawn = require('child_process').spawn;

var sprintf = require('sprintf').sprintf;


/**
 * Regular expression object cache.
 * @type {Object}.
 */
var REGEXP_CACHE = {};


/**
 * Cache of the emitted 'ready' events.
 * @type {Object}.
 */
var EMIT_READY_CACHE = {};


/**
 * How long to wait for stderr output.
 * @type {Number}.
 */
var STDERR_TIMEOUT = 2000;


/**
 * Figure the boolean XOR function, since JavaScript doesn't have it.
 * @param {Boolean} a First param.
 * @param {Boolean} b Second param.
 * @return {Boolean} Logical XOR.
 */
exports.logicalXOR = function(a, b) {
  return ((a || b) && !(a && b));
};


/**
 * Generate a random number between lower and upper bound.
 *
 * @param {Number} min Lower bound.
 * @param {Number} max Upper bound.
 * @return {Number} Random number between lower and upper bound.
 */
exports.getRandomInt = function(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
};


/**
 * Generate a random string of upper lower case letters and decimal digits.
 *
 * @param {Number} len  The length of the string to return;.
 * @return {String} Random string.
 */
exports.randstr = function(len) {
  var chars, r, x;

  chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  r = [];

  for (x = 0; x < len; x++) {
    r.push(chars[exports.getRandomInt(0, chars.length - 1)]);
  }

  return r.join('');
};


/**
 * Trim leading and trailing whitespace from a string.
 *
 * @param {String} text Original String.
 * @return {string} String with trimmed whitespace.
 */
exports.trim = function trim(text) {
  return (text || '').replace(/^\s+|\s+$/g, '');
};<commit_msg>Use crypto to provide raw bytes for randstr.<commit_after>/*
 *  Copyright 2012 Rackspace
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

var spawn = require('child_process').spawn;

var sprintf = require('sprintf').sprintf;


/**
 * Regular expression object cache.
 * @type {Object}.
 */
var REGEXP_CACHE = {};


/**
 * Cache of the emitted 'ready' events.
 * @type {Object}.
 */
var EMIT_READY_CACHE = {};


/**
 * How long to wait for stderr output.
 * @type {Number}.
 */
var STDERR_TIMEOUT = 2000;


/**
 * Figure the boolean XOR function, since JavaScript doesn't have it.
 * @param {Boolean} a First param.
 * @param {Boolean} b Second param.
 * @return {Boolean} Logical XOR.
 */
exports.logicalXOR = function(a, b) {
  return ((a || b) && !(a && b));
};


/**
 * Generate a random number between lower and upper bound.
 *
 * @param {Number} min Lower bound.
 * @param {Number} max Upper bound.
 * @return {Number} Random number between lower and upper bound.
 */
exports.getRandomInt = function(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
};


/**
 * Generate a random string of upper lower case letters and decimal digits.
 *
 * @param {Number} len  The length of the string to return;.
 * @return {String} Random string.
 */
exports.randstr = function(len) {
  var chars, r, i, bytes;

  chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  r = [];

  bytes = crypto.pseudoRandomBytes(len);

  for (i = 0; i < len; i++) {
    r.push(chars[bytes[i] % chars.length]);
  }

  return r.join('');
};


/**
 * Trim leading and trailing whitespace from a string.
 *
 * @param {String} text Original String.
 * @return {string} String with trimmed whitespace.
 */
exports.trim = function trim(text) {
  return (text || '').replace(/^\s+|\s+$/g, '');
};
{'fraction_non_alphanumeric': 0.135258358662614, 'fraction_numerical': 0.010855405992184108, 'mean_word_length': 3.4298076923076923, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5027388', 'n_tokens_mistral': 1494, 'n_tokens_neox': 1429, 'n_words': 525}