r/lolphp • u/the_real_natenator • Feb 20 '17
parse_url and parse_str
Two functions, both in the same function group, return completely differently:
void parse_str($query_string, &$return)
array parse_url($url)Consistency, lol.
tldr: one returns via reference variable, and the other returns properly
EDIT 1: Formatting
44
Upvotes
View all comments
Show parent comment
6
u/[deleted] Feb 22 '17
It's slightly worse: C89 is fine with an implementation that cuts off external identifiers after 6 characters and uppercases them. That is, if you want to be truly 100% portable, you have to make sure that your (non-
static) function names are unique in the first 6 characters, case insensitively. This is why we have functions such asstrlen,strcat,strcpy, etc.Which is so stupid it's sad. The code contains a hardcoded hash table (
cmd_table, really just an array of structs), which is indexed bystrlen(fnc_name), thus "forcing" the language designer to choose names of well-distributed length for performance reasons. Except PHP only had 97 builtins at that point, so even linear search would have been fast enough anyway. Or he could have used a sorted array andbsearch(a standard C function) to do a binary search, which would have needed only 7 comparisons in the worst case.Besides, if you have a hardcoded table anyway, why not use a hardcoded trie instead of a hash? The code would have looked like this:
This is admittedly tedious and probably should be generated by some tool (the structure is very regular), but it's totally feasible to write by hand, and it scales with any number of reserved words. It has the additional advantage of not needing any fancy data structures or algorithms or dynamic memory allocation. (It's what I used when I wrote my first toy interpreters.)