[limb-svn] r5992 - in 3.x/trunk/limb/core: . tests/cases
svn at limb-project.com
svn at limb-project.com
Thu Jun 14 15:46:30 MSD 2007
Author: pachanga
Date: 2007-06-14 15:46:30 +0400 (Thu, 14 Jun 2007)
New Revision: 5992
URL: http://fisheye.limb-project.com/changelog/limb/?cs=5992
Added:
3.x/trunk/limb/core/tests/cases/lmbCoreUtilsTest.class.php
Modified:
3.x/trunk/limb/core/common.inc.php
Log:
-- a bit more optimal versions of lmb_camel_case, lmb_under_scores added with tests
Modified: 3.x/trunk/limb/core/common.inc.php
===================================================================
--- 3.x/trunk/limb/core/common.inc.php 2007-06-13 14:00:24 UTC (rev 5991)
+++ 3.x/trunk/limb/core/common.inc.php 2007-06-14 11:46:30 UTC (rev 5992)
@@ -143,16 +143,40 @@
function lmb_camel_case($str, $ucfirst = true)
{
- $res = preg_replace('~([a-zA-Z])?_([a-zA-Z])~e',
- "'\\1'.strtoupper('\\2')",
- $str);
+ $items = explode('_', $str);
+ $len = sizeof($items);
+ $first = true;
+ $res = '';
+ for($i=0;$i<$len;$i++)
+ {
+ $item = $items[$i];
+ if($item)
+ {
+ $res .= ($first && !$ucfirst ? $item : ucfirst($item));
+ $first = false;
+ //skipping next "_" if it's not last
+ if($i+1 < $len-1 && !$items[$i+1])
+ $i++;
+ }
+ else
+ $res .= '_';
+ }
+
return ($ucfirst) ? ucfirst($res) : $res;
}
function lmb_under_scores($str)
{
- return ltrim(preg_replace('~([a-z])?([A-Z])([a-z])~e', "'\\1_'.strtolower('\\2').'\\3'", $str),
- '_');
+ $len = strlen($str);
+ $res = '';
+ for($i=0;$i<$len;$i++)
+ {
+ if(ctype_upper($str{$i}))
+ $res .= (($i-1 > 0 && $str{$i-1} != '') ? '_' : '') . strtolower($str{$i});
+ else
+ $res .= $str{$i};
+ }
+ return $res;
}
function lmb_humanize($str)
@@ -164,4 +188,4 @@
spl_autoload_register('lmb_autoload');
-?>
\ No newline at end of file
+?>
Added: 3.x/trunk/limb/core/tests/cases/lmbCoreUtilsTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbCoreUtilsTest.class.php (rev 0)
+++ 3.x/trunk/limb/core/tests/cases/lmbCoreUtilsTest.class.php 2007-06-14 11:46:30 UTC (rev 5992)
@@ -0,0 +1,45 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+class lmbCoreUtilsTest extends UnitTestCase
+{
+ function testCamelCaseUcfirst()
+ {
+ $this->assertEqual(lmb_camel_case('foo'), 'Foo');
+ $this->assertEqual(lmb_camel_case('foo_bar'), 'FooBar');
+ $this->assertEqual(lmb_camel_case('foo168_bar'), 'Foo168Bar');
+ $this->assertEqual(lmb_camel_case('foo_bar_hey_wow'), 'FooBarHeyWow');
+ $this->assertEqual(lmb_camel_case('_foo_bar'), '_FooBar');
+ $this->assertEqual(lmb_camel_case('_foo_bar_'), '_FooBar_');
+ $this->assertEqual(lmb_camel_case('___foo___bar'), '___Foo_Bar');
+ $this->assertEqual(lmb_camel_case('___foo___bar_hey'), '___Foo_BarHey');
+ }
+
+ function testCamelCaseDontUcfirst()
+ {
+ $this->assertEqual(lmb_camel_case('foo', false), 'foo');
+ $this->assertEqual(lmb_camel_case('foo_bar', false), 'fooBar');
+ $this->assertEqual(lmb_camel_case('foo168_bar', false), 'foo168Bar');
+ $this->assertEqual(lmb_camel_case('foo_bar_hey_wow', false), 'fooBarHeyWow');
+ $this->assertEqual(lmb_camel_case('_foo_bar', false), '_fooBar');
+ $this->assertEqual(lmb_camel_case('_foo_bar_', false), '_fooBar_');
+ $this->assertEqual(lmb_camel_case('___foo___bar', false), '___foo_Bar');
+ $this->assertEqual(lmb_camel_case('___foo___bar_hey', false), '___foo_BarHey');
+ }
+
+ function testUnderScores()
+ {
+ $this->assertEqual(lmb_under_scores('FooBar'), 'foo_bar');
+ $this->assertEqual(lmb_under_scores('Foo168Bar'), 'foo168_bar');
+ $this->assertEqual(lmb_under_scores('FooBarZoo'), 'foo_bar_zoo');
+ $this->assertEqual(lmb_under_scores('_FooBarZoo'), '_foo_bar_zoo');
+ $this->assertEqual(lmb_under_scores('_FooBarZoo_'), '_foo_bar_zoo_');
+ }
+}
+?>
More information about the limb-svn
mailing list