Permalink
Browse files

Implemented string-join.

  • Loading branch information...
1 parent 9671b74 commit 9817491f8adc331e195f57c637f2bc1693e55398 @dahlia committed Nov 28, 2009
Showing with 25 additions and 0 deletions.
  1. +1 −0 Lisphp/Environment.php
  2. +16 −0 Lisphp/Runtime/String.php
  3. +2 −0 Lisphp/Test/EnvironmentTest.php
  4. +6 −0 Lisphp/Test/RuntimeTest.php
@@ -46,6 +46,7 @@ static function sandbox() {
$scope['/'] = new Lisphp_Runtime_Arithmetic_Division;
$scope['%'] = $scope['mod'] =new Lisphp_Runtime_Arithmetic_Modulus;
$scope['.'] = $scope['concat'] =new Lisphp_Runtime_String_Concat;
+ $scope['string-join'] =new Lisphp_Runtime_String_StringJoin;
$scope['not'] = new Lisphp_Runtime_Logical_Not;
$scope['and'] = new Lisphp_Runtime_Logical_And;
$scope['or'] = new Lisphp_Runtime_Logical_Or;
@@ -9,3 +9,19 @@ protected function execute(array $arguments) {
}
}
+final class Lisphp_Runtime_String_StringJoin
+ extends Lisphp_Runtime_BuiltinFunction {
+ protected function execute(array $arguments) {
+ list($strs, $sep) = $arguments;
+ if (is_array($strs)) return join($sep, $strs);
+ foreach ($strs as $s) {
+ if (isset($result)) {
+ $result .= $sep . $s;
+ } else {
+ $result = $s;
+ }
+ }
+ return $result;
+ }
+}
+
@@ -68,6 +68,8 @@ function testSandbox($scope = null) {
$this->assertType('Lisphp_Runtime_Arithmetic_Modulus', $scope['mod']);
$this->assertType('Lisphp_Runtime_String_Concat', $scope['.']);
$this->assertType('Lisphp_Runtime_String_Concat', $scope['concat']);
+ $this->assertType('Lisphp_Runtime_String_StringJoin',
+ $scope['string-join']);
$this->assertType('Lisphp_Runtime_Logical_Not', $scope['not']);
$this->assertType('Lisphp_Runtime_Logical_And', $scope['and']);
$this->assertType('Lisphp_Runtime_Logical_Or', $scope['or']);
@@ -643,6 +643,12 @@ function testConcat() {
}
}
+ function testStringJoin() {
+ $join = new Lisphp_Runtime_String_StringJoin;
+ $this->assertFunction('one two three', $join,
+ array('one', 'two', 'three'), ' ');
+ }
+
function methodTest($a) {
return array($this, $a);
}

0 comments on commit 9817491

Please sign in to comment.