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