Let's talk about the str_repeat function implementation in PHP. some time ago we discussed the problem of generating strings of a certain character or string in PHP. This sentence sounds awkward. For example, the string that generates 10 a is aaaaaaaaaa. Of course, here a can be AB or any other string .? In the blog "generate a fixed-length character string PHP", we described a total of five methods. The efficiency of these five methods is also compared. let's talk about the str_repeat function implementation in PHP.
Some time ago, I discussed the problem of generating strings of a certain character or string in PHP. This sentence sounds awkward. For example, the string that generates 10 a is aaaaaaaaaa. Of course, here a can be AB or any other string.
?
In the blog "generate a fixed-length character string PHP", we described a total of five methods. The efficiency of these five methods is also compared. the str_repeat function in PHP is the most efficient, which is expected and reasonable.
Methods 4 and 5 are both set up for PHP functions to achieve the same function. although str_repeat is not good, its efficiency is better than method 1 and method 2.
?
Method 1 and 2 are implemented by the PHP language and are implemented through iterative string concatenation. the efficiency cannot be compared with builtin method in PHP.
?
Today, I saw the implementation of the str_repeat function in PHP. taking a closer look, the idea in method 1 was "the same as it was. How exactly does str_repeat in PHP? The core logic code is as follows:
?
Result_len = input_len * mult; result = (char *) safe_emalloc (input_len, mult, 1 ); /* Heavy optimization for situations where input string is 1 byte long */if (input_len = 1) {memset (result, * (input_str), mult );} else {char * s, * e, * ee; int l = 0; memcpy (result, input_str, input_len); s = result; e = result + input_len; ee = result + result_len; while (e
< (ee-e) ? (e-s) : (ee-e); memmove(e, s, l); e +="l;" } result[result_len]="\0" ;< pre>
The length of the result string is calculated first. The corresponding memory space is allocated.
Copy input_str to the very beginning of the memory allocation,
Then, the copied content is iterated to the remaining memory space.
From the code logic, we can see that the idea of copying is not one copy, but one copy, two, four, eight copies ......
Until the allocated memory space is filled, the last bit of the prize memory space is assigned '\ 0 '.
?
When I first saw this code, I was puzzled by the following code logic:
?
While (e
< (ee-e) ? (e-s) : (ee-e); memmove(e, s, l); e +="l;" }< pre>
The allocated memory space must be an integer multiple of input_str + 1. Therefore, if you only remove the length of the original string to the backend, you only need to exit the loop when the remaining length is less than the length of the original string, and then assign the last bit to '\ 0 '. Is that all right?
?
If you think so too, you and I made the same mistake at the beginning.
Because the original string is not copied in one copy. If you copy 7 copies (that is, str_repeat ("abc", 7);), you only need to copy four times (including the first time ).
In addition, the remaining unassigned memory space before the last copy is 3 times + 1 of the original string length, that is, 3*3 + 1. You only need to copy the first 10 characters in the "abcabcabcabc" string. In this way, the last character of result_str must be the first character of the original string, 'A '. This 'A' is not required.
?
Simple direct:
result[result_len] = '\0';
In fact, the undesired 'A' in the final string is replaced with the string terminator '\ 0 '.
?
This completes the str_repeat function.
?
**************************************** ************************
In addition:
Method 1 in the blog "generate a character string of a fixed length in PHP". Although the idea is the same as that of str_repeat, it adopts the string concatenation method and generates a new string for each concatenation, memory space is also a huge waste, as shown in the following example:
?
If you want to generate eight 'A' strings, 'AAA' is first generated, then 'aaaaa', and then 'aaaaaaa '. The number of loops is the same as that of str_repeat, but two useless strings 'AAA' and 'aaaaa' are generated in the middle. Moreover, the longer the target string, the more garbage. In addition, the loop uses string concatenation, which is inefficient.
?
?
Str_repeat does not allocate and release unnecessary space.