26 #if defined (HAVE_CONFIG_H)
91 int nargin = args.
length ();
97 args(0).is_dq_string () ?
'"' :
'\'');
104 std::queue<string_vector> args_as_strings;
106 for (
int i = 0; i < nargin; i++)
108 string_vector s = args(i).xstring_vector_value (
"char: unable to convert some args to strings");
111 n_elts += s.
numel ();
117 if (s_max_len > max_len)
120 args_as_strings.push (s);
127 for (
int i = 0; i < nargin; i++)
130 args_as_strings.pop ();
136 for (
int j = 0; j <
n; j++)
138 std::string t = s[j];
139 int t_len = t.length ();
142 t += std::string (max_len - t_len,
' ');
148 result[k++] = std::string (max_len,
' ');
178 DEFUN (strvcat, args, ,
214 int nargin = args.length ();
216 std::size_t max_len = 0;
217 std::queue<string_vector> args_as_strings;
219 for (
int i = 0; i < nargin; i++)
221 string_vector s = args(i).xstring_vector_value (
"strvcat: unable to convert some args to strings");
223 std::size_t
n = s.
numel ();
228 for (std::size_t j = 0; j <
n; j++)
237 if (s_max_len > max_len)
240 args_as_strings.push (s);
247 for (
int i = 0; i < nargin; i++)
250 args_as_strings.pop ();
252 std::size_t
n = s.
numel ();
256 for (std::size_t j = 0; j <
n; j++)
258 std::string t = s[j];
261 std::size_t t_len = t.length ();
264 t += std::string (max_len - t_len,
' ');
291 DEFUN (ischar, args, ,
298 if (args.length () != 1)
301 return ovl (args(0).is_string ());
326 bool (*str_op) (
const std::string&,
const std::string&,
327 std::string::size_type))
333 bool s1_cell = arg0.
iscell ();
335 bool s2_cell = arg1.
iscell ();
337 if (s1_string && s2_string)
339 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
358 if (
r == 0 ||
r == 1)
364 std::string s = (
r == 0 ?
"" : str[0]);
370 output(i) = str_op (cellstr(i), s,
n);
377 if (cell(i).is_string ())
378 output(i) = str_op (cell(i).string_value (), s,
n);
386 if (cell.
numel () == 1)
393 if (cell(0).is_string ())
395 const std::string str2 = cell(0).string_value ();
398 output(i) = str_op (str[i], str2,
n);
416 output(i) = str_op (str[i], cellstr(i),
n);
423 if (cell(i).is_string ())
424 output(i) = str_op (str[i],
425 cell(i).string_value (),
n);
436 else if (s1_cell && s2_cell)
468 if (cell2(0).is_string ())
470 const std::string str2 = cell2(0).string_value ();
476 output(i) = str_op (cellstr(i), str2,
n);
483 if (cell1(i).is_string ())
485 const std::string str1 = cell1(i).string_value ();
486 output(i) = str_op (str1, str2,
n);
495 error (
"%s: nonconformant cell arrays", fcn_name);
502 output (i) = str_op (cellstr1(i), cellstr2(i),
n);
509 if (cell1(i).is_string () && cell2(i).is_string ())
511 const std::string str1 = cell1(i).string_value ();
512 const std::string str2 = cell2(i).string_value ();
513 output(i) = str_op (str1, str2,
n);
531 template <
typename T,
typename T_
size_type>
533 strcmp_ignore_n (
const T& s1,
const T& s2, T_size_type)
536 template <
typename T,
typename T_
size_type>
538 strcmpi_ignore_n (
const T& s1,
const T& s2, T_size_type)
560 if (args.length () != 2)
563 return ovl (do_strcmp_fcn (args(0), args(1), 0,
"strcmp",
564 strcmp_ignore_n, strcmp_ignore_n));
648 if (args.length () != 3)
654 return ovl (do_strcmp_fcn (args(0), args(1),
n,
"strncmp",
658 error (
"strncmp: N must be greater than 0");
698 if (args.length () != 2)
701 return ovl (do_strcmp_fcn (args(0), args(1), 0,
"strcmpi",
702 strcmpi_ignore_n, strcmpi_ignore_n));
729 if (args.length () != 3)
735 return ovl (do_strcmp_fcn (args(0), args(1),
n,
"strncmpi",
739 error (
"strncmpi: N must be greater than 0");
795 if (args.length () != 1)
800 if (args(0).is_string ())
802 if (args(0).rows () == 0 || args(0).columns () == 0)
804 else if (args(0).rows () == 1 && args(0).ndims () == 2)
813 else if (args(0).iscell ())
815 const Cell cell = args(0).cell_value ();
821 if (cell(i).is_string ())
876 DEFUN (__native2unicode__, args, ,
884 if (args(0).is_string ())
885 return ovl (args(0));
887 std::string tmp = args(1).string_value ();
891 charNDArray native_bytes = args(0).char_array_value ();
893 const char *src = native_bytes.
data ();
894 std::size_t srclen = native_bytes.
numel ();
897 uint8_t *utf8_str =
nullptr;
904 error (
"native2unicode: iconv() is not supported. Installing GNU "
905 "libiconv and then re-compiling Octave could fix this.");
907 error (
"native2unicode: converting from codepage '%s' to UTF-8: %s",
908 codepage, std::strerror (errno));
918 retval.
xelem (i) = utf8_str[i];
923 DEFUN (__unicode2native__, args, ,
932 std::string tmp = args(1).string_value ();
936 charNDArray utf8_str = args(0).char_array_value ();
938 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (utf8_str.
data ());
939 std::size_t srclen = utf8_str.
numel ();
942 char *native_bytes =
nullptr;
949 error (
"unicode2native: iconv() is not supported. Installing GNU "
950 "libiconv and then re-compiling Octave could fix this.");
952 error (
"unicode2native: converting from UTF-8 to codepage '%s': %s",
953 codepage, std::strerror (errno));
963 retval.
xelem (i) = native_bytes[i];
968 DEFUN (__locale_charset__, , ,
976 std::string charset_str (charset);
977 return ovl (charset_str);
980 DEFUN (unicode_idx, args, ,
994 if (args.length () != 1)
997 charNDArray str = args(0).xchar_array_value (
"STR must be a string");
1000 if (str.
ndims () > 1)
1009 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (str_p.
data ());
1021 idx(i+j) = u8_char_num;
1032 DEFUN (__unicode_length__, args, ,
1050 if (args.length () != 1)
1053 bool arg_char = args(0).is_char_matrix ();
1055 if (! arg_char && ! args(0).iscellstr ())
1056 error (
"STR must be a character array or cell string.");
1064 if (str.
ndims () > 1)
1073 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (str.
data ());
1076 retval =
ovl (mbsnlen);
1081 NDArray output (args(0).dims (),
false);
1085 =
reinterpret_cast<const uint8_t *
> (cellstr(i).c_str ());
1089 retval =
ovl (output);
1102 DEFUN (__u8_validate__, args, ,
1114 int nargin = args.length ();
1116 if (nargin < 1 || nargin > 2)
1120 std::string in_str =
1121 args(0).xstring_value (
"__u8_validate__: IN_STR must be a string");
1123 std::string mode =
"replace";
1125 mode = args(1).xstring_value (
"__u8_validate__: MODE must be a string");
1128 if (mode ==
"replace")
1130 else if (mode ==
"unicode")
1133 error (R
"(__u8_validate__: MODE must be either "replace" or "unicode")");
1137 return ovl (in_str);
1140 DEFUN (newline, args, ,
1161 if (args.length () != 0)
1178 DEFUN (list_in_columns, args, ,
1214 int nargin = args.length ();
1216 if (nargin < 1 || nargin > 3)
1219 string_vector s = args(0).xstring_vector_value (
"list_in_columns: ARG must be a cellstr or char array");
1223 if (nargin > 1 && ! args(1).isempty ())
1224 width = args(1).xint_value (
"list_in_columns: WIDTH must be an integer");
1229 prefix = args(2).xstring_value (
"list_in_columns: PREFIX must be a string");
1231 std::ostringstream buf;
1235 return ovl (buf.str ());
1258 OCTAVE_END_NAMESPACE(
octave)
int ndims() const
Size of the specified dimension.
const T * data() const
Size of the specified dimension.
Array< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
const dim_vector & dims() const
Return a const-reference so that dims ()(i) works efficiently.
T & xelem(octave_idx_type n)
Size of the specified dimension.
octave_idx_type numel() const
Number of elements in the array.
MArray< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Vector representing the dimensions (size) of an Array.
charNDArray char_array_value(bool frc_str_conv=false) const
octave_idx_type numel() const
string_vector string_vector_value(bool pad=false) const
octave_idx_type length() const
octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
Array< std::string > cellstr_value() const
octave_idx_type max_length() const
std::ostream & list_in_columns(std::ostream &, int width=0, const std::string &prefix="") const
octave_idx_type numel() const
Array< U > map(F fcn) const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define DEFUN(name, args_name, nargout_name, doc)
Macro to define a builtin function.
#define DEFUNX(name, fname, args_name, nargout_name, doc)
Macro to define a builtin function with certain internal name.
void() error(const char *fmt,...)
const char * octave_locale_charset_wrapper(void)
std::complex< double > Complex
bool strncmp(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same.
bool strcmp(const T &str_a, const T &str_b)
Octave string utility functions.
Complex str2double(const std::string &str_arg)
bool strcmpi(const T &str_a, const T &str_b)
True if strings are the same, ignoring case.
unsigned int u8_validate(const std::string &who, std::string &in_string, const u8_fallback_type type=U8_REPLACEMENT_CHAR)
bool strncmpi(const T &str_a, const T &str_b, const typename T::size_type n)
True if the first N characters are the same, ignoring case.
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
octave_value_list Fstrcmpi(const octave_value_list &args, int)
octave_value_list Fstrncmpi(const octave_value_list &args, int)
uint8_t * octave_u8_conv_from_encoding(const char *fromcode, const char *src, size_t srclen, size_t *lengthp)
char * octave_u8_conv_to_encoding(const char *tocode, const uint8_t *src, size_t srclen, size_t *lengthp)
int octave_u8_strmblen_wrapper(const uint8_t *src)
size_t octave_u8_mbsnlen_wrapper(const uint8_t *src, size_t n)