[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
deblank and empty arguments
From: |
John W. Eaton |
Subject: |
deblank and empty arguments |
Date: |
Wed, 30 Jan 2008 02:37:42 -0500 |
On 29-Jan-2008, H. Wziontek wrote:
| Hello,
|
| I'd like to propose, that the function deblank should return
| the argument itself instead of an error message if an empty
| value is provided. Sometimes it can happen, that a value is
| not defined, but for some reason, all values are "deblanked"
| inside a loop. A script should not stop in such a case with
| an error. Below my suggestion to change the code.
|
| Hartmut
|
|
| *** deblank.m.orig Tue Jan 22 17:54:13 2008
| --- deblank.m Tue Jan 29 09:16:10 2008
| *************** function s = deblank (s)
| *** 34,39 ****
| --- 34,41 ----
| print_usage ();
| endif
|
| + if (!isempty (s))
| +
| if (ischar (s))
|
| k = find (! isspace (s) & s != "\0");
| *************** function s = deblank (s)
| *** 51,54 ****
| --- 53,58 ----
| error ("deblank: expecting string argument");
| endif
|
| + endif
| +
| endfunction
I checked in the following change instead.
It's best to report bugs to the address@hidden list.
Thanks,
jwe
scripts/ChangeLog:
2008-01-30 John W. Eaton <address@hidden>
* strings/deblank.m: Improve compatibility.
Index: scripts/strings/deblank.m
===================================================================
RCS file: /cvs/octave/scripts/strings/deblank.m,v
retrieving revision 1.25
diff -u -u -r1.25 deblank.m
--- scripts/strings/deblank.m 22 Jan 2008 21:52:26 -0000 1.25
+++ scripts/strings/deblank.m 30 Jan 2008 07:36:11 -0000
@@ -34,13 +34,23 @@
print_usage ();
endif
- if (ischar (s))
+ char_arg = ischar (s);
- k = find (! isspace (s) & s != "\0");
- if (isempty (s) || isempty (k))
- s = "";
- else
- s = s(:,1:ceil (max (k) / rows (s)));
+ if (char_arg || isnumeric (s))
+
+ if (! isempty (s))
+ if (char_arg)
+ k = find (! isspace (s) & s != "\0");
+ else
+ warning ("deblank: expecting character string argument")
+ k = find (s != 0);
+ endif
+
+ if (isempty (k))
+ s = resize (s, 0, 0);
+ else
+ s = s(:,1:ceil (max (k) / rows (s)));
+ endif
endif
elseif (iscell(s))
@@ -48,16 +58,29 @@
s = cellfun (@deblank, s, "UniformOutput", false);
else
- error ("deblank: expecting string argument");
+ error ("deblank: expecting character string argument");
endif
endfunction
-%!assert(strcmp (deblank (" f o o "), " f o o"));
+%!assert (strcmp (deblank (" f o o "), " f o o"));
-%!error deblank ();
+%!assert (deblank ([]), [])
+%!assert (deblank ({}), {})
+%!assert (deblank (""), "")
-%!error deblank ("foo", "bar");
+%!assert (deblank ([0,0,0]), [])
+%!assert (deblank (' '), '')
+%!assert (deblank (" "), "")
+
+%!assert (typeinfo (deblank (" ")), "string")
+%!assert (typeinfo (deblank (' ')), "sq_string")
-%!error deblank (1);
+%!assert (deblank ([1,2,0]), [1,2])
+%!assert (deblank ([1,2,0,32]), [1,2,0,32])
+%!assert (deblank (int8 ([1,2,0])), int8 ([1,2]))
+
+%!error deblank ();
+
+%!error deblank ("foo", "bar");