develooper Front page | perl.cvs.parrot | Postings from January 2009

[svn:parrot] r36091 - in trunk/languages/pipp: src/common t/php

From:
bernhard
Date:
January 28, 2009 06:34
Subject:
[svn:parrot] r36091 - in trunk/languages/pipp: src/common t/php
Message ID:
20090128143344.7CACECB9AE@x12.develooper.com
Author: bernhard
Date: Wed Jan 28 06:33:42 2009
New Revision: 36091

Modified:
   trunk/languages/pipp/src/common/php_string.pir
   trunk/languages/pipp/t/php/string.t

Log:
[Pipp] add implementation of 'chunk_split'

Modified: trunk/languages/pipp/src/common/php_string.pir
==============================================================================
--- trunk/languages/pipp/src/common/php_string.pir	(original)
+++ trunk/languages/pipp/src/common/php_string.pir	Wed Jan 28 06:33:42 2009
@@ -175,14 +175,68 @@
 
 =item C<string chunk_split(string str [, int chunklen [, string ending]])>
 
-Returns split line
+Inserts the string C<ending> every C<chunklen> characters into the string C<str>.
+Default for C<chunklen> is 76. Default for C<ending> is '\r\n'.
 
 NOT IMPLEMENTED.
 
 =cut
 
 .sub 'chunk_split'
-    not_implemented()
+    .param pmc args :slurpy
+
+    .local int argc
+    argc = args
+    unless argc > 3 goto L1
+    wrong_param_count()
+    unless argc == 0 goto L1
+    wrong_param_count()
+  L1:
+    .local string ending
+    ending = "\r\n"   # TODO: the windows case
+    unless argc == 3 goto L2
+    ending = pop args
+    argc = args
+  L2:
+    .local int chunk_len
+    chunk_len = 76
+    unless argc == 2 goto L3
+       chunk_len = pop args
+  L3:
+    .local string str
+    str = pop args
+    argc = args
+
+    # compute number of complete chunks and length of the rest
+    .local int str_len
+    str_len = length str
+    .local int num_chunks
+    num_chunks = str_len / chunk_len
+    .local int rest_len
+    rest_len = mod str_len, chunk_len
+
+    # assemble the output
+    .local string res, chunk
+    .local int i, cursor
+    i = 0
+    cursor = 0
+  L4:
+    unless i < num_chunks goto L5
+    chunk = substr str, cursor, chunk_len
+    concat res, chunk
+    concat res, ending
+    inc i
+    cursor = cursor + chunk_len
+    goto L4
+  L5:
+
+    unless rest_len > 0 goto L6
+    chunk = substr str, cursor
+    concat res, chunk
+    concat res, ending
+  L6:
+
+    .RETURN_STRING(res)
 .end
 
 =item C<mixed count_chars(string input [, int mode])>

Modified: trunk/languages/pipp/t/php/string.t
==============================================================================
--- trunk/languages/pipp/t/php/string.t	(original)
+++ trunk/languages/pipp/t/php/string.t	Wed Jan 28 06:33:42 2009
@@ -23,7 +23,7 @@
 use FindBin;
 use lib "$FindBin::Bin/../../../../lib", "$FindBin::Bin/../../lib";
 
-use Parrot::Test tests => 37;
+use Parrot::Test tests => 38;
 
 
 language_output_is( 'Pipp', <<'CODE', <<'OUT', 'bin2hex' );
@@ -488,6 +488,16 @@
 1 + 1 = 2
 OUT
 
+language_output_is( 'Pipp', <<'CODE', <<"OUT", 'chunk_split' );
+<?php
+
+echo chunk_split( '12345678901234567890', 3, '? ?'), "\n";
+
+?>
+CODE
+123? ?456? ?789? ?012? ?345? ?678? ?90? ?
+OUT
+
 # Local Variables:
 #   mode: cperl
 #   cperl-indent-level: 4



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About