From 34e0fdb725a46bc43ed4f147131c445287f20e1a Mon Sep 17 00:00:00 2001 From: Ted Reed Date: Fri, 7 Aug 2009 11:31:32 -0700 Subject: [PATCH] Fix String.each to actually work as it should, also have it respect $/. Fixes #17. --- src/classes/String.pir | 26 ++++++++++++++++++++++---- t/string/block.t | 2 -- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/classes/String.pir b/src/classes/String.pir index 5c95bd3..3c30c68 100644 --- a/src/classes/String.pir +++ b/src/classes/String.pir @@ -156,11 +156,29 @@ Returns the characters in C in reverse order. Destructive update. .end .sub 'each' :method :multi('CardinalString',_) - .param pmc delim + .param pmc delim :optional + .param int delim_flag :opt_flag .param pmc block :named('!BLOCK') - .local pmc list - list = self.'split'(delim) - list.'each'(block) + .local pmc str, iterator + if delim_flag goto have_delim + delim = get_hll_global '$/' + have_delim: + iterator = iter self + str = new 'CardinalString' + main_loop: + unless iterator goto loop_end + $P0 = shift iterator + str.'concat'($P0) + unless $P0 == delim goto main_loop + block(str) + str = new 'CardinalString' + goto main_loop + loop_end: + $P0 = str.'empty?'() + if $P0 goto done + block(str) + done: + .return (self) .end .sub 'empty?' :method diff --git a/t/string/block.t b/t/string/block.t index 3159dc3..aff30df 100644 --- a/t/string/block.t +++ b/t/string/block.t @@ -24,11 +24,9 @@ parrot = 'parrot' i = 0 parrot.each('r') do |split| if i == 0 - todo "Fix String.each(char)", "17" is split, 'par', '.each(char) for String' end if i == 1 - todo "Fix String.each(char)", "17" is split, 'r', '.each(char) for String' end if i == 2