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,
' ');
178DEFUN (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,
' ');
291DEFUN (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);
409 if (cell.
numel () == r)
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);
531template <
typename T,
typename T_
size_type>
536template <
typename T,
typename T_
size_type>
560 if (args.length () != 2)
648 if (args.length () != 3)
658 error (
"strncmp: N must be greater than 0");
697 if (args.length () != 2)
728 if (args.length () != 3)
738 error (
"strncmpi: N must be greater than 0");
794 if (args.length () != 1)
799 if (args(0).is_string ())
801 if (args(0).rows () == 0 || args(0).columns () == 0)
803 else if (args(0).rows () == 1 && args(0).ndims () == 2)
812 else if (args(0).iscell ())
814 const Cell cell = args(0).cell_value ();
820 if (cell(i).is_string ())
878DEFUN (__native2unicode__, args, ,
886 if (args(0).is_string ())
887 return ovl (args(0));
889 std::string tmp = args(1).string_value ();
893 charNDArray native_bytes = args(0).char_array_value ();
895 const char *src = native_bytes.
data ();
896 std::size_t srclen = native_bytes.
numel ();
899 uint8_t *utf8_str =
nullptr;
906 error (
"native2unicode: iconv() is not supported. Installing GNU "
907 "libiconv and then re-compiling Octave could fix this.");
909 error (
"native2unicode: converting from codepage '%s' to UTF-8: %s",
910 codepage, std::strerror (errno));
913 unwind_action free_utf8_str ([=] () {
::free (utf8_str); });
920 retval.
xelem (i) = utf8_str[i];
925DEFUN (__unicode2native__, args, ,
934 std::string tmp = args(1).string_value ();
938 charNDArray utf8_str = args(0).char_array_value ();
940 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (utf8_str.
data ());
941 std::size_t srclen = utf8_str.
numel ();
944 char *native_bytes =
nullptr;
951 error (
"unicode2native: iconv() is not supported. Installing GNU "
952 "libiconv and then re-compiling Octave could fix this.");
954 error (
"unicode2native: converting from UTF-8 to codepage '%s': %s",
955 codepage, std::strerror (errno));
958 unwind_action free_native_bytes ([=] () {
::free (native_bytes); });
965 retval.
xelem (i) = native_bytes[i];
970DEFUN (__locale_charset__, , ,
978 std::string charset_str (charset);
979 return ovl (charset_str);
982DEFUN (unicode_idx, args, ,
996 if (args.length () != 1)
999 charNDArray str = args(0).xchar_array_value (
"STR must be a string");
1002 if (str.
ndims () > 1)
1011 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (str_p.
data ());
1023 idx(i+j) = u8_char_num;
1034DEFUN (__unicode_length__, args, ,
1052 if (args.length () != 1)
1055 bool arg_char = args(0).is_char_matrix ();
1057 if (! arg_char && ! args(0).iscellstr ())
1058 error (
"STR must be a character array or cell string.");
1066 if (str.
ndims () > 1)
1075 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (str.
data ());
1078 retval =
ovl (mbsnlen);
1083 NDArray output (args(0).dims (),
false);
1087 =
reinterpret_cast<const uint8_t *
> (cellstr(i).c_str ());
1091 retval =
ovl (output);
1104DEFUN (__u8_validate__, args, ,
1116 int nargin = args.length ();
1118 if (nargin < 1 || nargin > 2)
1122 std::string in_str =
1123 args(0).xstring_value (
"__u8_validate__: IN_STR must be a string");
1125 std::string mode =
"replace";
1127 mode = args(1).xstring_value (
"__u8_validate__: MODE must be a string");
1130 if (mode ==
"replace")
1132 else if (mode ==
"unicode")
1135 error (R
"(__u8_validate__: MODE must be either "replace" or "unicode")");
1139 return ovl (in_str);
1142DEFUN (newline, args, ,
1163 if (args.length () != 0)
1180DEFUN (list_in_columns, args, ,
1216 int nargin = args.length ();
1218 if (nargin < 1 || nargin > 3)
1221 string_vector s = args(0).xstring_vector_value (
"list_in_columns: ARG must be a cellstr or char array");
1225 if (nargin > 1 && ! args(1).isempty ())
1226 width = args(1).xint_value (
"list_in_columns: WIDTH must be an integer");
1231 prefix = args(2).xstring_value (
"list_in_columns: PREFIX must be a string");
1233 std::ostringstream buf;
1237 return ovl (buf.str ());
T & xelem(octave_idx_type n)
Size of the specified dimension.
octave_idx_type numel(void) const
Number of elements in the array.
const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
const T * data(void) const
Size of the specified dimension.
OCTARRAY_API Array< T, Alloc > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Size of the specified dimension.
int ndims(void) const
Size of the specified dimension.
bool iscellstr(void) const
MArray< T > permute(const Array< octave_idx_type > &vec, bool inv=false) const
Vector representing the dimensions (size) of an Array.
bool iscellstr(void) const
octave_idx_type numel(void) const
bool is_string(void) const
charNDArray char_array_value(bool frc_str_conv=false) const
Cell cell_value(void) const
Array< std::string > cellstr_value(void) const
string_vector string_vector_value(bool pad=false) const
octave_value convert_to_str(bool pad=false, bool force=false, char type='\'') const
OCTINTERP_API octave_idx_type length(void) const
dim_vector dims(void) const
std::ostream & list_in_columns(std::ostream &, int width=0, const std::string &prefix="") const
Array< U > map(F fcn) const
octave_idx_type max_length(void) const
octave_idx_type numel(void) const
OCTINTERP_API void print_usage(void)
#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)
OCTAVE_API Complex str2double(const std::string &str_arg)
OCTAVE_API 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.
OCTAVE_API unsigned int u8_validate(const std::string &who, std::string &in_string, const u8_fallback_type type=U8_REPLACEMENT_CHAR)
OCTAVE_API 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.
OCTAVE_API bool strcmp(const T &str_a, const T &str_b)
True if strings are the same.
OCTAVE_API bool strcmpi(const T &str_a, const T &str_b)
True if strings are the same, ignoring case.
std::complex< double > Complex
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_EXPORT octave_value_list Fstrncmpi(const octave_value_list &args, int)
static bool strcmpi_ignore_n(const T &s1, const T &s2, T_size_type)
OCTAVE_EXPORT octave_value_list Fstrcmpi(const octave_value_list &args, int)
static octave_value do_strcmp_fun(const octave_value &arg0, const octave_value &arg1, octave_idx_type n, const char *fcn_name, bool(*array_op)(const Array< char > &, const Array< char > &, octave_idx_type), bool(*str_op)(const std::string &, const std::string &, std::string::size_type))
static bool strcmp_ignore_n(const T &s1, const T &s2, T_size_type)
char * octave_u8_conv_to_encoding(const char *tocode, const uint8_t *src, size_t srclen, size_t *lengthp)
uint8_t * octave_u8_conv_from_encoding(const char *fromcode, const char *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)