]> Creatis software - CreaPhase.git/blob - octave_packages/m/strings/isstrprop.m
update packages
[CreaPhase.git] / octave_packages / m / strings / isstrprop.m
1 ## Copyright (C) 2008-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn {Function File} {} isstrprop (@var{str}, @var{prop})
21 ## Test character string properties.  For example:
22 ##
23 ## @example
24 ## @group
25 ## isstrprop ("abc123", "alpha")
26 ## @result{} [1, 1, 1, 0, 0, 0]
27 ## @end group
28 ## @end example
29 ##
30 ## If @var{str} is a cell array, @code{isstrpop} is applied recursively
31 ## to each element of the cell array.
32 ##
33 ## Numeric arrays are converted to character strings.
34 ##
35 ## The second argument @var{prop} must be one of
36 ##
37 ## @table @asis
38 ## @item "alpha"
39 ## True for characters that are alphabetic (letters).
40 ##
41 ## @item "alnum"
42 ## @itemx "alphanum"
43 ## True for characters that are alphabetic or digits.
44 ##
45 ## @item "lower"
46 ## True for lowercase letters.
47 ##
48 ## @item "upper"
49 ## True for uppercase letters.
50 ##
51 ## @item "digit"
52 ## True for decimal digits (0-9).
53 ##
54 ## @item "xdigit"
55 ## True for hexadecimal digits (@nospell{a-fA-F0-9}).
56 ##
57 ## @item "space"
58 ## @itemx "wspace"
59 ## True for whitespace characters (space, formfeed, newline, carriage
60 ## return, tab, vertical tab).
61 ##
62 ## @item "punct"
63 ## True for punctuation characters (printing characters except space
64 ## or letter or digit).
65 ##
66 ## @item "cntrl"
67 ## True for control characters.
68 ##
69 ## @item "graph"
70 ## @itemx "graphic"
71 ## True for printing characters except space.
72 ##
73 ## @item "print"
74 ## True for printing characters including space.
75 ##
76 ## @item "ascii"
77 ## True for characters that are in the range of ASCII encoding.
78 ##
79 ## @end table
80 ##
81 ## @seealso{isalpha, isalnum, islower, isupper, isdigit, isxdigit,
82 ## isspace, ispunct, iscntrl, isgraph, isprint, isascii}
83 ## @end deftypefn
84
85 function retval = isstrprop (str, prop)
86
87   if (nargin != 2)
88     print_usage ();
89   endif
90
91   switch (prop)
92     case "alpha"
93       retval = isalpha (str);
94     case {"alnum", "alphanum"}
95       retval = isalnum (str);
96     case "ascii"
97       retval = isascii (str);
98     case "cntrl"
99       retval = iscntrl (str);
100     case "digit"
101       retval = isdigit (str);
102     case {"graph", "graphic"}
103       retval = isgraph (str);
104     case "lower"
105       retval = islower (str);
106     case "print"
107       retval = isprint (str);
108     case "punct"
109       retval = ispunct (str);
110     case {"space", "wspace"}
111       retval = isspace (str);
112     case "upper"
113       retval = isupper (str);
114     case "xdigit"
115       retval = isxdigit (str);
116     otherwise
117       error ("isstrprop: invalid string property");
118   endswitch
119
120 endfunction
121
122
123 %!assert (isstrprop ("abc123", "alpha"), logical ([1, 1, 1, 0, 0, 0]))
124 %!assert (isstrprop ("abc123", "digit"), logical ([0, 0, 0, 1, 1, 1]))
125 %!assert (isstrprop ("Hello World", "wspace"), isspace ("Hello World"))
126 %!assert (isstrprop ("Hello World", "graphic"), isgraph ("Hello World"))
127 %!assert (isstrprop (char ("AbC", "123"), "upper"), logical ([1 0 1; 0 0 0]))
128 %!assert (isstrprop ({"AbC", "123"}, "lower"), {logical([0 1 0]), logical([0 0 0])})
129
130 %%Input Validation
131 %!error isstrprop ()
132 %!error isstrprop ("abc123")
133 %!error isstrprop ("abc123", "alpha", "alpha")
134 %!error <invalid string property> isstrprop ("abc123", "foo")
135