Skip to content
Snippets Groups Projects
Commit de71a763 authored by Henrik (Grubba) Grubbström's avatar Henrik (Grubba) Grubbström
Browse files

String.common_prefix: Some minor optimizations.

parent 7e17675b
No related branches found
No related tags found
No related merge requests found
...@@ -87,18 +87,22 @@ string common_prefix(array(string) strs) ...@@ -87,18 +87,22 @@ string common_prefix(array(string) strs)
if(!sizeof(strs)) if(!sizeof(strs))
return ""; return "";
strs = Array.uniq(strs);
if (sizeof(strs) == 1)
return strs[0];
string strs0 = strs[0]; string strs0 = strs[0];
int n, i; int n, i;
catch int sz = min(@map(strs, sizeof));
{
for(n = 0; n < sizeof(strs0); n++) for(n = 0; n < sz; n++)
for(i = 1; i < sizeof(strs); i++) for(i = 1; i < sizeof(strs); i++)
if(strs[i][n] != strs0[n]) if(strs[i][n] != strs0[n])
return strs0[0..n-1]; return strs0[0..n-1];
};
return strs0[0..n-1]; return strs0[..sz-1];
} }
// Do a fuzzy matching between two different strings and return a // Do a fuzzy matching between two different strings and return a
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment