Skip to content

Commit

Permalink
Refactoring singularize/pluralize to use an in-memory array map for b…
Browse files Browse the repository at this point in the history
…etter performance.
  • Loading branch information
jperras committed Apr 10, 2009
1 parent dd55afe commit f466929
Showing 1 changed file with 29 additions and 19 deletions.
48 changes: 29 additions & 19 deletions cake/libs/inflector.php
Expand Up @@ -227,13 +227,14 @@ function &getInstance() {
function rules($type, $rules = array()) {
$_this =& Inflector::getInstance();

foreach ($rules as $rule => $pattern) {
if (is_array($pattern)) {
$_this->{$type}[$rule] = array_merge($pattern, $_this->{$type}[$rule]);
unset($rules[$rule], $_this->{$type}['cache' . ucfirst($rule)]);
foreach ($rules as $rule => $pattern) {
if (is_array($pattern)) {
$_this->{$type}[$rule] = array_merge($pattern, $_this->{$type}[$rule]);
unset($rules[$rule], $_this->{$type}['cache' . ucfirst($rule)], $_this->{$type}['merged'][$rule]);
}
}
}
$_this->{$type}['rules'] = array_merge($rules, $_this->{$type}['rules']);
$_this->{$type}['rules'] = array_merge($rules, $_this->{$type}['rules']);

}
/**
* Return $word in plural form.
Expand All @@ -251,16 +252,21 @@ function pluralize($word) {
return $_this->pluralized[$word];
}

extract($_this->plural);
$uninflected = array_merge($uninflected, $_this->uninflected);
if (!isset($_this->plural['merged']['irregular'])) {
$_this->plural['merged']['irregular'] = $_this->plural['irregular'];
}

if (!isset($_this->plural['merged']['uninflected'])) {
$_this->plural['merged']['uninflected'] = array_merge($_this->plural['uninflected'], $_this->uninflected);
}

if (!isset($_this->plural['cacheUninflected']) || !isset($_this->plural['cacheIrregular'])) {
$_this->plural['cacheUninflected'] = '(?:' . join( '|', $uninflected) . ')';
$_this->plural['cacheIrregular'] = '(?:' . join( '|', array_keys($irregular)) . ')';
$_this->plural['cacheUninflected'] = '(?:' . join( '|', $_this->plural['merged']['uninflected']) . ')';
$_this->plural['cacheIrregular'] = '(?:' . join( '|', array_keys($_this->plural['merged']['irregular'])) . ')';
}

if (preg_match('/(.*)\\b(' . $_this->plural['cacheIrregular'] . ')$/i', $word, $regs)) {
$_this->pluralized[$word] = $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
$_this->pluralized[$word] = $regs[1] . substr($word, 0, 1) . substr($_this->plural['merged']['irregular'][strtolower($regs[2])], 1);
return $_this->pluralized[$word];
}

Expand All @@ -269,7 +275,7 @@ function pluralize($word) {
return $word;
}

foreach ($rules as $rule => $replacement) {
foreach ($_this->plural['rules'] as $rule => $replacement) {
if (preg_match($rule, $word)) {
$_this->pluralized[$word] = preg_replace($rule, $replacement, $word);
return $_this->pluralized[$word];
Expand All @@ -293,17 +299,21 @@ function singularize($word) {
return $_this->singularized[$word];
}

extract($_this->singular);
$uninflected = array_merge($uninflected, $_this->uninflected);
$irregular = array_merge($irregular, array_flip($_this->plural['irregular']));
if (!isset($_this->singular['merged']['uninflected'])) {
$_this->singular['merged']['uninflected'] = array_merge($_this->singular['uninflected'], $_this->uninflected);
}

if (!isset($_this->singular['merged']['irregular'])) {
$_this->singular['merged']['irregular'] = array_merge($_this->singular['irregular'], array_flip($_this->plural['irregular']));
}

if (!isset($_this->singular['cacheUninflected']) || !isset($_this->singular['cacheIrregular'])) {
$_this->singular['cacheUninflected'] = '(?:' . join( '|', $uninflected) . ')';
$_this->singular['cacheIrregular'] = '(?:' . join( '|', array_keys($irregular)) . ')';
$_this->singular['cacheUninflected'] = '(?:' . join( '|', $_this->singular['merged']['uninflected']) . ')';
$_this->singular['cacheIrregular'] = '(?:' . join( '|', array_keys($_this->singular['merged']['irregular'])) . ')';
}

if (preg_match('/(.*)\\b(' . $_this->singular['cacheIrregular'] . ')$/i', $word, $regs)) {
$_this->singularized[$word] = $regs[1] . substr($word, 0, 1) . substr($irregular[strtolower($regs[2])], 1);
$_this->singularized[$word] = $regs[1] . substr($word, 0, 1) . substr($_this->singular['merged']['irregular'][strtolower($regs[2])], 1);
return $_this->singularized[$word];
}

Expand All @@ -312,7 +322,7 @@ function singularize($word) {
return $word;
}

foreach ($rules as $rule => $replacement) {
foreach ($_this->singular['rules'] as $rule => $replacement) {
if (preg_match($rule, $word)) {
$_this->singularized[$word] = preg_replace($rule, $replacement, $word);
return $_this->singularized[$word];
Expand Down

0 comments on commit f466929

Please sign in to comment.