On the implementation of Str_repeat function in PHP
The question of generating a string of characters or strings in PHP has been discussed in the previous period, hehe. This sentence sounds too awkward. For example, a string that generates 10 a is aaaaaaaaaa. Of course a here can be ab or any other string.
?
A total of 5 methods are described in the blog "Generate a fixed-length character string PHP". The efficiency of the five methods is also compared, and the efficiency of the Str_repeat function in PHP is optimal, which is also expected and understandable.
Method 4,5 is a function of PHP itself to achieve the same function, although no str_repeat excellent, but its efficiency is better than Method 1, and Method 2.
?
The method is implemented by the PHP language itself, through the iterative concatenation of strings, the efficiency of course can not be compared with the Builtin method in PHP.
?
Today, I see the implementation of PHP in the Str_repeat function, a closer look at the original and their own method of implementation of the idea of 1 is "similar" ah. So what exactly is the 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 the where input string is 1 a 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 resulting string is computed first. and allocates the corresponding memory space.
Then copy the INPUT_STR to the beginning of the allocated memory space,
The copied content is then iterated to the remaining memory space.
From the code logic, it can be seen that the idea of replication is not a copy, but a copy, two copies, four copies, eight copies ...
Until the allocated memory space is filled, then the last one in the memory space is assigned a value of ' + '.
?
When you first see this code, you are 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. So if you just move back one more time to the original string length, you only need to exit the loop when the remaining length is less than the original string length, and then assign the last one to ' + '. No, it's okay?
?
If that's what you think, then you and I are just beginning to make the same mistake.
Because the copy of the original string is not performed in one copy. If you copy 7 copies (that is, Str_repeat ("abc", 7);), you only need to replicate 4 times (including the first one).
And the remaining unassigned memory space before the last copy is 3 times times +1 of the original string length, which is 3*3+1. Only the first 10 characters in the "ABCABCABCABC" string need to be copied. Thus, the last character of Result_str must be the first character of the original string, ' a '. This ' a ' is not meant to be.
?
Simply:
Result[result_len] = ' + ';
is actually the last string in the unwanted ' a ' replaced with a string of Terminator '.
?
So the Str_repeat function is done.
?
****************************************************************
In addition
Blog "Generate a fixed-length character string PHP" Method 1, although the ideological and str_repeat consistent, but the use of string stitching, and each splicing to produce a new string, memory space is also a great waste, the following example:
?
If you want to generate a string of 8 ' a ', first generate ' AA ', then ' aaaa ', then ' aaaaaaaa '. The number of cycles and str_repeat is the same, but the middle generates ' AA ', ' aaaa ' two useless strings, and the longer the target string, the more garbage. Furthermore, the use of string concatenation in the loop is not efficient.
?
?
Str_repeat, however, has no unnecessary allocation and deallocation of unused space.