time-to-botec/js/node_modules/@stdlib/string/repeat/lib/repeat.js

130 lines
3.7 KiB
JavaScript
Raw Normal View History

/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* 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.
*/
'use strict';
// MODULES //
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive;
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' );
var format = require( './../../format' );
// MAIN //
/**
* Repeats a string a specified number of times and returns the concatenated result.
*
* ## Method
*
* The algorithmic trick used in the implementation is to treat string concatenation the same as binary addition (i.e., any natural number (nonnegative integer) can be expressed as a sum of powers of two).
*
* For example,
*
* ```text
* n = 10 => 1010 => 2^3 + 2^0 + 2^1 + 2^0
* ```
*
* We can produce a 10-repeat string by "adding" the results of a 8-repeat string and a 2-repeat string.
*
* The implementation is then as follows:
*
* 1. Let `s` be the string to be repeated and `o` be an output string.
*
* 2. Initialize an output string `o`.
*
* 3. Check the least significant bit to determine if the current `s` string should be "added" to the output "total".
*
* - if the bit is a one, add
* - otherwise, move on
*
* 4. Double the string `s` by adding `s` to `s`.
*
* 5. Right-shift the bits of `n`.
*
* 6. Check if we have shifted off all bits.
*
* - if yes, done.
* - otherwise, move on
*
* 7. Repeat 3-6.
*
* The result is that, as the string is repeated, we continually check to see if the doubled string is one which we want to add to our "total".
*
* The algorithm runs in `O(log_2(n))` compared to `O(n)`.
*
*
* @param {string} str - string to repeat
* @param {NonNegativeInteger} n - number of times to repeat the string
* @throws {TypeError} first argument must be a string
* @throws {TypeError} second argument must be a nonnegative integer
* @throws {RangeError} output string length must not exceed maximum allowed string length
* @returns {string} repeated string
*
* @example
* var str = repeat( 'a', 5 );
* // returns 'aaaaa'
*
* @example
* var str = repeat( '', 100 );
* // returns ''
*
* @example
* var str = repeat( 'beep', 0 );
* // returns ''
*/
function repeat( str, n ) {
var rpt;
var cnt;
if ( !isString( str ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
}
if ( !isNonNegativeInteger( n ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) );
}
if ( str.length === 0 || n === 0 ) {
return '';
}
// Check that output string will not exceed the maximum string length:
if ( str.length * n > FLOAT64_MAX_SAFE_INTEGER ) {
throw new RangeError( format( 'invalid argument. Output string length exceeds maximum allowed string length. Value: `%u`.', str.length * n ) );
}
rpt = '';
cnt = n;
for ( ; ; ) {
// If the count is odd, append the current concatenated string:
if ( (cnt&1) === 1 ) {
rpt += str;
}
// Right-shift the bits:
cnt >>>= 1;
if ( cnt === 0 ) {
break;
}
// Double the string:
str += str;
}
return rpt;
}
// EXPORTS //
module.exports = repeat;