UNPKG

@stdlib/string

Version:

String manipulation functions.

109 lines (102 loc) 2.67 kB
/** * @license Apache-2.0 * * Copyright (c) 2022 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'; // 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)`. * * @private * @param {string} str - string to repeat * @param {NonNegativeInteger} n - number of times to repeat the string * @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 ( str.length === 0 || n === 0 ) { return ''; } 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;