[PATCH] Revised .trim patch

[email protected] (Ovid) Mon, 12 Jan 2009 14:02:02 -0800 (PST)
Newsgroups perl.perl6.internals
Message-ID <[email protected]>
Seems Larry's agreed to the .trim method.  There are bits that are not agreed upon, so this patch only implements what we've agreed upon.  It relies on the new S29-str/trim.t test in pugs.  I committed that earlier and updated t/spectest.data.

 
In other words, the patch is a tad clearer and now has tests.

Cheers,
Ovid
--
Buy the book         - http://www.oreilly.com/catalog/perlhks/
Tech blog            - http://use.perl.org/~Ovid/journal/
Twitter              - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6
trim.patch (application/octet-stream, 1.8 KB)
Index: languages/perl6/t/spectest.data
===================================================================
--- languages/perl6/t/spectest.data	(revision 35458)
+++ languages/perl6/t/spectest.data	(working copy)
@@ -286,6 +286,7 @@
 S29-str/split-simple.t
 S29-str/sprintf.t
 S29-str/substr.t
+S29-str/trim.t
 S29-str/ucfirst.t
 S29-str/uc.t
 S29-str/unpack.t
Index: languages/perl6/src/builtins/any-str.pir
===================================================================
--- languages/perl6/src/builtins/any-str.pir	(revision 35458)
+++ languages/perl6/src/builtins/any-str.pir	(working copy)
@@ -21,7 +21,7 @@
 .namespace []
 .sub 'onload' :anon :init :load
     $P0 = get_hll_namespace ['Any']
-    '!EXPORT'('capitalize,chop,chomp,chars,:d,:e,:f,index,lc,lcfirst,rindex,ord,substr,uc,ucfirst,unpack', 'from'=>$P0)
+    '!EXPORT'('capitalize,chop,chomp,chars,:d,:e,:f,index,lc,lcfirst,rindex,ord,substr,trim,uc,ucfirst,unpack', 'from'=>$P0)
 .end
 
 
@@ -128,6 +128,39 @@
        .return (retv)
 .end
 
+=item trim()
+
+Remove leading and trailing whitespace from a string.
+
+=cut
+
+.sub 'trim' :method :multi(_)
+    .local string s
+    .local int start, end, temp, len
+    .local int is_whitespace
+    s = self
+    start = 0
+    end = length s
+    if end == 0 goto donetrailing
+  trimleading:
+    is_whitespace = is_cclass 32, s, start
+    unless is_whitespace goto doneleading
+    inc start
+    goto trimleading
+  doneleading:
+    temp = end
+  trimtrailing:
+    dec temp
+    is_whitespace = is_cclass 32, s, temp
+    unless is_whitespace goto donetrailing
+    end = temp
+    goto trimtrailing
+  donetrailing:
+    len = end - start
+    s = substr s, start, len
+    .return(s)
+.end
+
 =item comb()
 
 Partial implementation for now, returns a list of strings