<?php
function find_words($in) {
// stateful stream functions
$len = strlen($in);
if ($len === 0) {
return array(); // nothing to lex
}
$pos = 0;
$char = $in[0];
$peek = function() use ($in, &$pos, $len) {
if ($pos+1 >= $len) {
return null;
}
return $in[$pos+1];
};
$next = function() use ($peek, &$pos, &$char) {
$char = $peek();
$pos++;
return $char !== null;
};
// stateless helper functions
$upper = function ($c) {
return 'A' <= $c && $c <= 'Z';
};
$lower = function ($c) {
return 'a' <= $c && $c <= 'z';
};
$alpha = function ($c) use ($upper, $lower) {
return $upper($c) || $lower($c);
};
$num = function ($c) {
return '0' <= $c && $c <= '9';
};
$alpha_num = function ($c) use ($alpha, $num) {
return $alpha($c) || $num($c);
};
// lexer states do action and return next lexer state
$out = array();
$find_word = null; // cyclic dependency, declare first and pass by reference to dependents
$upr_word = function() use (&$out, &$char, $next, $peek, $upper, $lower, &$find_word) {
$word = $char;
while ($next() && $upper($char)) {
if ($lower($peek())) { // start of capitalized word (e.g. FOOBar at B)
break;
}
$word .= $char;
}
$out[] = $word;
return $find_word;
};
$std_word = function() use (&$out, &$char, $next, $lower, &$find_word) {
$word = $char;
while ($next() && $lower($char)) {
$word .= $char;
}
$out[] = $word;
return $find_word;
};
$num_word = function() use (&$out, &$char, $next, $num, &$find_word) {
$word = $char;
while ($next() && $num($char)) {
$word .= $char;
}
$out[] = $word;
return $find_word;
};
$find_word = function () use (&$char, $alpha_num, $upper, $lower, $next, $peek, $std_word, $upr_word, $num_word) {
// consume all non-alphanumeric characters
while (!$alpha_num($char)) {
if (!$next()) {
return null; // nothing left
}
}
if ($upper($char)) {
if ($upper($peek())) { // uppercase word
return $upr_word;
}
return $std_word; // capitalized word
}
if ($lower($char)) {
return $std_word; // lowercase word
}
return $num_word; // number
};
// churn through states
$state = $find_word;
while ($state !== null) {
$state = $state();
}
return $out;
}
$samples = array(
'FooBar123',
'FooBAR123',
'FOOBar123',
'foo_bar_123_Baz',
'FOO_bar_123BAZ',
'FOO_Bar_123baz',
'foo_BAR_123_baz',
'foo_Bar_123_Baz',
'Foo_bar_123_BAZ',
);
print_r(array_map(function ($name) {
return array(
'name' => $name,
'words' => find_words($name),
);
}, $samples));
- Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- Array
(
[0] => Array
(
[name] => FooBar123
[words] => Array
(
[0] => Foo
[1] => Bar
[2] => 123
)
)
[1] => Array
(
[name] => FooBAR123
[words] => Array
(
[0] => Foo
[1] => BAR
[2] => 123
)
)
[2] => Array
(
[name] => FOOBar123
[words] => Array
(
[0] => FOO
[1] => Bar
[2] => 123
)
)
[3] => Array
(
[name] => foo_bar_123_Baz
[words] => Array
(
[0] => foo
[1] => bar
[2] => 123
[3] => Baz
)
)
[4] => Array
(
[name] => FOO_bar_123BAZ
[words] => Array
(
[0] => FOO
[1] => bar
[2] => 123
[3] => BAZ
)
)
[5] => Array
(
[name] => FOO_Bar_123baz
[words] => Array
(
[0] => FOO
[1] => Bar
[2] => 123
[3] => baz
)
)
[6] => Array
(
[name] => foo_BAR_123_baz
[words] => Array
(
[0] => foo
[1] => BAR
[2] => 123
[3] => baz
)
)
[7] => Array
(
[name] => foo_Bar_123_Baz
[words] => Array
(
[0] => foo
[1] => Bar
[2] => 123
[3] => Baz
)
)
[8] => Array
(
[name] => Foo_bar_123_BAZ
[words] => Array
(
[0] => Foo
[1] => bar
[2] => 123
[3] => BAZ
)
)
)
- Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
- Parse error: syntax error, unexpected T_FUNCTION in /in/DDZ7l on line 12
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- Parse error: parse error, unexpected T_FUNCTION in /in/DDZ7l on line 12
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- Parse error: parse error in /in/DDZ7l on line 12
Process exited with code 255.
preferences:
328.78 ms | 401 KiB | 459 Q