Author: particle
Date: Fri Feb  2 12:34:55 2007
New Revision: 16868

Added:
   trunk/runtime/parrot/library/String/
   trunk/runtime/parrot/library/String/Utils.pir
Modified:
   trunk/MANIFEST
   trunk/config/gen/makefiles/root.in

Log:
[library]: add string utilities library with 'chomp' routine

Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST      (original)
+++ trunk/MANIFEST      Fri Feb  2 12:34:55 2007
@@ -2281,6 +2281,7 @@
 runtime/parrot/library/Stream/Replay.pir                    [library]
 runtime/parrot/library/Stream/Sub.pir                       [library]
 runtime/parrot/library/Stream/Writer.pir                    [library]
+runtime/parrot/library/String/Utils.pir                     [library]
 runtime/parrot/library/Test/Builder.pir                     [library]
 runtime/parrot/library/Test/Builder/Output.pir              [library]
 runtime/parrot/library/Test/Builder/Test.pir                [library]

Modified: trunk/config/gen/makefiles/root.in
==============================================================================
--- trunk/config/gen/makefiles/root.in  (original)
+++ trunk/config/gen/makefiles/root.in  Fri Feb  2 12:34:55 2007
@@ -324,6 +324,7 @@
     $(LIBRARY_DIR)/Stream/Replay.pbc \
     $(LIBRARY_DIR)/Stream/Sub.pbc \
     $(LIBRARY_DIR)/Stream/Writer.pbc \
+    $(LIBRARY_DIR)/String/Utils.pbc \
     $(LIBRARY_DIR)/YAML/Parser/Syck.pbc \
     $(LIBRARY_DIR)/STM.pbc \
     $(LIBRARY_DIR)/libpcre.pbc \

Added: trunk/runtime/parrot/library/String/Utils.pir
==============================================================================
--- (empty file)
+++ trunk/runtime/parrot/library/String/Utils.pir       Fri Feb  2 12:34:55 2007
@@ -0,0 +1,70 @@
+# Copyright (C) 2007, The Perl Foundation.
+# $Id$
+
+.namespace ['String';'Utils']
+
+=head1 NAME
+
+['String';'Utils'] - Utilities for string processing
+
+=head1 SYNOPSIS
+
+    load_bytecode 'String/Utils.pbc'
+
+    .local pmc chomp
+               chomp = get_global ['String';'Utils'], 'chomp'
+
+    $S0 = chomp($S0)      # use default record separator ("\n")
+    $S0 = chomp($S0, $S1) # use custom record separator
+
+=head1 Functions
+
+=over
+
+=item chomp
+
+    $S0 = chomp( $S1 )
+    $S0 = chomp( $S1, $S2 )
+
+Remove all trailing record separator C<$S2> from tail of input string C<$S1>
+and return in C<$S0>. If C<$S2> is not specified, the default C<\n> is used.
+
+=cut
+
+.sub 'chomp'
+    .param string str     # read-only
+    .param string sep     :optional
+    .param int    has_sep :opt_flag
+
+    .local string res
+                  res = str
+
+    if has_sep goto chomp
+    sep = "\n"
+
+    .local int strl
+    .local int sepl
+
+  chomp:
+    strl = length res
+    sepl = length sep
+
+    if sep > res goto return
+    $I0 = strl - sepl
+    $I1 = index res, sep, $I0
+    unless $I1 == $I0 goto return
+    chopn res, sepl
+    goto chomp
+
+  return:
+    .return ( res )
+.end
+
+
+=back
+
+=head1 AUTHOR
+
+Jerry Gay a.k.a. particle
+
+=cut

Reply via email to