You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lib/node_modules/@stdlib/string/repeat/lib/repeat.js
+21-13Lines changed: 21 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -12,31 +12,39 @@ var MAX_SAFE_INTEGER = require( '@stdlib/constants/math/float64-max-safe-integer
12
12
/**
13
13
* Repeats a string a specified number of times and returns the concatenated result.
14
14
*
15
-
* #### Implementation
15
+
* ## Methods
16
16
*
17
17
* 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).
18
18
*
19
19
* For example,
20
20
*
21
-
* ```text
21
+
* ```text
22
22
* n = 10 => 1010 => 2^3 + 2^0 + 2^1 + 2^0
23
23
* ```
24
24
*
25
25
* We can produce a 10-repeat string by "adding" the results of a 8-repeat string and a 2-repeat string.
26
26
*
27
27
* The implementation is then as follows:
28
28
*
29
-
* 1. Let `s` be the string to be repeated and `o` be an output string.
30
-
* 2. Initialize an output string `o`.
31
-
* 3. Check the least significant bit to determine if the current `s` string should be "added" to the output "total".
32
-
* - if the bit is a one, add
33
-
* - otherwise, move on
34
-
* 4. Double the string `s` by adding `s` to `s`.
35
-
* 5. Right-shift the bits of `n`.
36
-
* 6. Check if we have shifted off all bits.
37
-
* - if yes, done.
38
-
* - otherwise, move on
39
-
* 7. Repeat 3-6.
29
+
* 1. Let `s` be the string to be repeated and `o` be an output string.
30
+
*
31
+
* 2. Initialize an output string `o`.
32
+
*
33
+
* 3. Check the least significant bit to determine if the current `s` string should be "added" to the output "total".
34
+
*
35
+
* - if the bit is a one, add
36
+
* - otherwise, move on
37
+
*
38
+
* 4. Double the string `s` by adding `s` to `s`.
39
+
*
40
+
* 5. Right-shift the bits of `n`.
41
+
*
42
+
* 6. Check if we have shifted off all bits.
43
+
*
44
+
* - if yes, done.
45
+
* - otherwise, move on
46
+
*
47
+
* 7. Repeat 3-6.
40
48
*
41
49
* 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".
0 commit comments