changeset 10018:fb8834c12035

fix & improve strjust
author Jaroslav Hajek <highegg@gmail.com>
date Wed, 23 Dec 2009 14:25:51 +0100
parents 1ce1ae448572
children 7ad32bf759c3
files scripts/ChangeLog scripts/strings/strjust.m
diffstat 2 files changed, 60 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/ChangeLog	Tue Dec 22 21:44:53 2009 +0100
+++ b/scripts/ChangeLog	Wed Dec 23 14:25:51 2009 +0100
@@ -1,3 +1,7 @@
+2009-12-23  Jaroslav Hajek  <highegg@gmail.com>
+
+	* strings/strjust.m: Special-case string and empty argument.
+
 2009-12-19  Rik <octave@nomad.inbox5.com>
 
 	* Makefile.am: Eliminate TOPDIR variable in favor of built-in automake
--- a/scripts/strings/strjust.m	Tue Dec 22 21:44:53 2009 +0100
+++ b/scripts/strings/strjust.m	Wed Dec 23 14:25:51 2009 +0100
@@ -43,35 +43,69 @@
 
   if (nargin == 1)
     just = "right";
+  else
+    just = tolower (just);
   endif
 
-  just = tolower (just);
+  if (ndims (x) != 2)
+    error ("needs a string or character matrix");
+  endif
 
-  ## convert nulls to blanks
-  idx = find (toascii (x) == 0);
-  if (! isempty (idx))
-    x(idx) = " ";
+  if (isempty (x))
+    return
   endif
 
-  ## For all cases, left, right and center, the algorithm is the same.
-  ## Find the number of blanks at the left/right end to determine the
-  ## shift, rotate the row index by using mod with that shift, then
-  ## translate the shifted row index into an array index.
-  [nr, nc] = size (x);
-  idx = (x' != " ");
-  if (strcmp (just, "right"))
-    [N, hi] = max (cumsum (idx));
-    shift = hi;
-  elseif (strcmp (just, "left"))
-    [N, lo] = max (cumsum (flipud (idx)));
-    shift = (nc - lo);
+  if (rows (x) == 1)
+    ## string case
+    nonbl = x != " " & x != char (0);
+    n = length (x);
+    switch (just)
+    case "right"
+      idx = find (nonbl, 1, "last");
+      if (idx < n) # false if idx is empty
+        x = [" "(1, ones (1, n-idx)), x(1:idx)];
+      endif
+    case "left"
+      idx = find (nonbl, 1, "first");
+      if (idx > 1) # false if idx is empty
+        x = [x(idx:n), " "(1, ones (1, idx))];
+      endif
+    case "center"
+      idx = find (nonbl, 1, "first");
+      jdx = find (nonbl, 1, "last");
+      if (idx > 1 || jdx < n)
+        nsp = ((idx - 1) + (n - jdx)) / 2;
+        lpad = " "(1, ones (1, floor (nsp)));
+        rpad = " "(1, ones (1, ceil (nsp)));
+        x = [lpad, x(idx:jdx), rpad];
+      endif
+    otherwise
+      error ("strjust: invalid justify type: %s", just);
+    endswitch
   else
-    [N, hi] = max (cumsum (idx));
-    [N, lo] = max (cumsum (flipud (idx)));
-    shift = ceil (nc - (lo-hi)/2);
+    ## char matrix case.
+
+    ## For all cases, left, right and center, the algorithm is the same.
+    ## Find the number of blanks at the left/right end to determine the
+    ## shift, rotate the row index by using mod with that shift, then
+    ## translate the shifted row index into an array index.
+    [nr, nc] = size (x);
+    idx = (x != " " & x != char (0)).';
+    if (strcmp (just, "right"))
+      [N, hi] = max (cumsum (idx));
+      shift = hi;
+    elseif (strcmp (just, "left"))
+      [N, lo] = max (cumsum (flipud (idx)));
+      shift = (nc - lo);
+    else
+      [N, hi] = max (cumsum (idx));
+      [N, lo] = max (cumsum (flipud (idx)));
+      shift = ceil (nc - (lo-hi)/2);
+    endif
+    idx = rem (ones(nr,1)*[0:nc-1] + shift'*ones(1,nc), nc);
+    x = x (idx*nr + [1:nr]'*ones(1,nc));
+
   endif
-  idx = rem (ones(nr,1)*[0:nc-1] + shift'*ones(1,nc), nc);
-  x = x (idx*nr + [1:nr]'*ones(1,nc));
 
 endfunction