Length-encode a string Suppose we use the following rules to pull a single string from another string, one containing only ASCII printable characters and called an *-string. If the string runs out before the process halts, that is an error, and the result of the process is undefined in that case:


Start with d=1, s=""
Whenever you encounter a *, multiply d by 2. Whenever you encounter another character, concatenate it to the end of s and subtract 1 from d. If now d=0, halt and return s


Defined Examples:

d-&gt;d
769-&gt;7
abcd56-&gt;a
*abcd56-&gt;ab
**abcd56-&gt;abcd
*7*690-&gt;769
***abcdefghij-&gt;abcdefgh


Undefined Examples: (note that the empty string would be one of these as well)

*7
**769
*7*
*a*b
*


Your job is to take a string and return the shortest *-string that produces that string.

Program Examples:

7-&gt;7
a-&gt;a
ab-&gt;*ab
abcd-&gt;**abcd
769-&gt;*7*69


Your program should handle any string containing at least one character and only non-* ASCII printable characters. You can never return strings for which the process is undefined, since by definition they cannot produce ANY strings.

Standard loopholes and I/O rules apply.
JavaScript (ES6), 61 bytes

f=(s,d=2)=&gt;s?d&gt;s.length?s[0]+f(s.slice(1),d-2):'*'+f(s,d*2):s


Recursive function that does the following:


If d is less than or equal to remaining string length divided by 2:

Append * to output and multiply d by 2
Else:

Shift the string and append to output, subtract 1 from d.


See it in action:



f=(s,d=2)=&gt;s?d&gt;s.length?s[0]+f(s.slice(1),d-2):'*'+f(s,d*2):s

input.oninput = e =&gt; output.innerHTML = f(input.value);
&lt;input id="input" type="text"/&gt;
&lt;p id="output"&gt;&lt;/p&gt;