comparison scripts/strings/substr.m @ 13297:42d5ff896e85

substr.m: Expand function to accept negative values for length argument. * substr.m: Expand function to accept negative values for length argument. Improve input validation and add more tests. Update docstring.
author Rik <octave@nomad.inbox5.com>
date Sat, 08 Oct 2011 11:08:50 -0700
parents 139f993936af
children db15303ee267
comparison
equal deleted inserted replaced
13296:27da11c63d98 13297:42d5ff896e85
15 ## You should have received a copy of the GNU General Public License 15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see 16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>. 17 ## <http://www.gnu.org/licenses/>.
18 18
19 ## -*- texinfo -*- 19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} substr (@var{s}, @var{offset}, @var{len}) 20 ## @deftypefn {Function File} {} substr (@var{s}, @var{offset})
21 ## @deftypefnx {Function File} {} substr (@var{s}, @var{offset}, @var{len})
21 ## Return the substring of @var{s} which starts at character number 22 ## Return the substring of @var{s} which starts at character number
22 ## @var{offset} and is @var{len} characters long. 23 ## @var{offset} and is @var{len} characters long.
23 ## 24 ##
24 ## If @var{offset} is negative, extraction starts that far from the end of 25 ## Position numbering for offsets begins with 1. If @var{offset} is negative,
25 ## the string. If @var{len} is omitted, the substring extends to the end 26 ## extraction starts that far from the end of the string.
26 ## of S. 27 ##
28 ## If @var{len} is omitted, the substring extends to the end of @var{S}. A
29 ## negative value for @var{len} extracts to within @var{len} characters of
30 ## the end of the string
27 ## 31 ##
28 ## For example: 32 ## Examples:
29 ## 33 ##
30 ## @example 34 ## @example
31 ## @group 35 ## @group
32 ## substr ("This is a test string", 6, 9) 36 ## substr ("This is a test string", 6, 9)
33 ## @result{} "is a test" 37 ## @result{} "is a test"
38 ## substr ("This is a test string", -11)
39 ## @result{} "test string"
40 ## substr ("This is a test string", -11, -7)
41 ## @result{} "test"
34 ## @end group 42 ## @end group
35 ## @end example 43 ## @end example
36 ## 44 ##
37 ## This function is patterned after AWK@. You can get the same result by 45 ## This function is patterned after the equivalent function in Perl.
38 ## @code{@var{s}(@var{offset} : (@var{offset} + @var{len} - 1))}.
39 ## @end deftypefn 46 ## @end deftypefn
40 47
41 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at> 48 ## Author: Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
42 ## Adapted-By: jwe 49 ## Adapted-By: jwe
43 50
45 52
46 if (nargin < 2 || nargin > 3) 53 if (nargin < 2 || nargin > 3)
47 print_usage (); 54 print_usage ();
48 endif 55 endif
49 56
50 if (ischar (s)) 57 if (! ischar (s))
51 nc = columns (s); 58 error ("substr: S must be a string or string array");
52 if (abs (offset) > 0 && abs (offset) <= nc) 59 elseif (! isscalar (offset) || (nargin == 3 && ! isscalar (len)))
53 if (offset <= 0) 60 error ("substr: OFFSET and LEN must be scalar integers");
54 offset += nc + 1; 61 endif
55 endif 62
56 if (nargin == 2) 63 offset = fix (offset);
57 eos = nc; 64 nc = columns (s);
58 else 65 if (abs (offset) > nc || offset == 0)
59 eos = offset + len - 1; 66 error ("substr: OFFSET = %d out of range", offset);
60 endif 67 endif
61 if (eos <= nc) 68
62 t = s (:, offset:eos); 69 if (offset <= 0)
63 else 70 offset += nc + 1;
64 error ("substr: length = %d out of range", len); 71 endif
65 endif 72
73 if (nargin == 2)
74 eos = nc;
75 else
76 len = fix (len);
77 if (len < 0)
78 eos = nc + len;
66 else 79 else
67 error ("substr: OFFSET = %d out of range", offset); 80 eos = offset + len - 1;
68 endif 81 endif
69 else
70 error ("substr: expecting string argument");
71 endif 82 endif
83
84 if (eos > nc)
85 error ("substr: length LEN = %d out of range", len);
86 elseif (offset > eos && len != 0)
87 error ("substr: No overlap with chosen values of OFFSET and LEN");
88 endif
89
90 t = s(:, offset:eos);
72 91
73 endfunction 92 endfunction
74 93
75 %!assert(strcmp (substr ("This is a test string", 6, 9), "is a test"));
76 94
77 %!error substr (); 95 %!assert (substr ("This is a test string", 6, 9), "is a test");
96 %!assert (substr ("This is a test string", -11), "test string");
97 %!assert (substr ("This is a test string", -11, 4), "test");
98 %!assert (substr ("This is a test string", -11, -7), "test");
99 %!assert (substr ("This is a test string", 1, -7), "This is a test");
100 %!assert (substr ("This is a test string", 1, 0), "");
78 101
79 %!error substr ("foo", 2, 3, 4); 102 %% Test input validation
103 %!error substr ()
104 %!error substr ("foo", 2, 3, 4)
105 %!error substr (ones (5, 1), 1, 1)
106 %!error substr ("foo", ones(2,2))
107 %!error substr ("foo", 1, ones(2,2))
108 %!error substr ("foo", 0)
109 %!error substr ("foo", 5)
110 %!error substr ("foo", 1, 5)
111 %!error substr ("foo", -1, 5)
112 %!error substr ("foo", 2, -5)
80 113