26#if defined (HAVE_CONFIG_H)
93 int nargin = args.
length ();
99 args(0).is_dq_string () ?
'"' :
'\'');
106 std::queue<string_vector> args_as_strings;
108 for (
int i = 0; i < nargin; i++)
110 string_vector s = args(i).xstring_vector_value (
"char: unable to convert some args to strings");
113 n_elts += s.
numel ();
119 if (s_max_len > max_len)
122 args_as_strings.push (s);
129 for (
int i = 0; i < nargin; i++)
132 args_as_strings.pop ();
138 for (
int j = 0; j < n; j++)
140 std::string t = s[j];
141 int t_len = t.length ();
144 t += std::string (max_len - t_len,
' ');
150 result[k++] = std::string (max_len,
' ');
180DEFUN (strvcat, args, ,
216 int nargin = args.
length ();
218 std::size_t max_len = 0;
219 std::queue<string_vector> args_as_strings;
221 for (
int i = 0; i < nargin; i++)
223 string_vector s = args(i).xstring_vector_value (
"strvcat: unable to convert some args to strings");
225 std::size_t n = s.
numel ();
230 for (std::size_t j = 0; j < n; j++)
239 if (s_max_len > max_len)
242 args_as_strings.push (s);
249 for (
int i = 0; i < nargin; i++)
252 args_as_strings.pop ();
254 std::size_t n = s.
numel ();
258 for (std::size_t j = 0; j < n; j++)
260 std::string t = s[j];
263 std::size_t t_len = t.length ();
266 t += std::string (max_len - t_len,
' ');
293DEFUN (ischar, args, ,
300 if (args.length () != 1)
303 return ovl (args(0).is_string ());
328 bool (*str_op) (
const std::string&,
const std::string&,
329 std::string::size_type))
335 bool s1_cell = arg0.
iscell ();
337 bool s2_cell = arg1.
iscell ();
339 if (s1_string && s2_string)
341 else if ((s1_string && s2_cell) || (s1_cell && s2_string))
360 if (r == 0 || r == 1)
366 std::string s = (r == 0 ?
"" : str[0]);
372 output(i) = str_op (cellstr(i), s, n);
379 if (cell(i).is_string ())
380 output(i) = str_op (cell(i).string_value (), s, n);
388 if (cell.
numel () == 1)
395 if (cell(0).is_string ())
397 const std::string str2 = cell(0).string_value ();
400 output(i) = str_op (str[i], str2, n);
411 if (cell.
numel () == r)
418 output(i) = str_op (str[i], cellstr(i), n);
425 if (cell(i).is_string ())
426 output(i) = str_op (str[i],
427 cell(i).string_value (), n);
438 else if (s1_cell && s2_cell)
470 if (cell2(0).is_string ())
472 const std::string str2 = cell2(0).string_value ();
478 output(i) = str_op (cellstr(i), str2, n);
485 if (cell1(i).is_string ())
487 const std::string str1 = cell1(i).string_value ();
488 output(i) = str_op (str1, str2, n);
497 error (
"%s: nonconformant cell arrays", fcn_name);
504 output (i) = str_op (cellstr1(i), cellstr2(i), n);
511 if (cell1(i).is_string () && cell2(i).is_string ())
513 const std::string str1 = cell1(i).string_value ();
514 const std::string str2 = cell2(i).string_value ();
515 output(i) = str_op (str1, str2, n);
533template <
typename T,
typename T_
size_type>
535strcmp_ignore_n (
const T& s1,
const T& s2, T_size_type)
536{
return string::strcmp (s1, s2); }
538template <
typename T,
typename T_
size_type>
540strcmpi_ignore_n (
const T& s1,
const T& s2, T_size_type)
541{
return string::strcmpi (s1, s2); }
562 if (args.length () != 2)
565 return ovl (do_strcmp_fcn (args(0), args(1), 0,
"strcmp",
566 strcmp_ignore_n, strcmp_ignore_n));
656 if (args.length () != 3)
662 return ovl (do_strcmp_fcn (args(0), args(1), n,
"strncmp",
666 error (
"strncmp: N must be greater than or equal to 0");
710 if (args.length () != 2)
713 return ovl (do_strcmp_fcn (args(0), args(1), 0,
"strcmpi",
714 strcmpi_ignore_n, strcmpi_ignore_n));
746 if (args.length () != 3)
752 return ovl (do_strcmp_fcn (args(0), args(1), n,
"strncmpi",
756 error (
"strncmpi: N must be greater than or equal to 0");
814 if (args.length () != 1)
819 if (args(0).is_string ())
821 if (args(0).rows () == 0 || args(0).columns () == 0)
822 retval =
Matrix (1, 1, numeric_limits<double>::NaN ());
823 else if (args(0).rows () == 1 && args(0).ndims () == 2)
824 retval = string::str2double (args(0).string_value ());
829 retval = sv.
map<
Complex> (string::str2double);
832 else if (args(0).iscell ())
834 const Cell cell = args(0).cell_value ();
840 if (cell(i).is_string ())
841 output(i) = string::str2double (cell(i).string_value ());
846 retval =
Matrix (1, 1, numeric_limits<double>::NaN ());
898DEFUN (__native2unicode__, args, ,
906 if (args(0).is_string ())
907 return ovl (args(0));
909 std::string tmp = args(1).string_value ();
913 charNDArray native_bytes = args(0).char_array_value ();
915 const char *src = native_bytes.
data ();
916 std::size_t srclen = native_bytes.
numel ();
919 uint8_t *utf8_str =
nullptr;
926 error (
"native2unicode: iconv() is not supported. Installing GNU "
927 "libiconv and then re-compiling Octave could fix this.");
929 error (
"native2unicode: converting from codepage '%s' to UTF-8: %s",
930 codepage, std::strerror (errno));
940 retval.
xelem (i) = utf8_str[i];
945DEFUN (__unicode2native__, args, ,
954 std::string tmp = args(1).string_value ();
958 charNDArray utf8_str = args(0).char_array_value ();
960 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (utf8_str.
data ());
961 std::size_t srclen = utf8_str.
numel ();
964 char *native_bytes =
nullptr;
971 error (
"unicode2native: iconv() is not supported. Installing GNU "
972 "libiconv and then re-compiling Octave could fix this.");
974 error (
"unicode2native: converting from UTF-8 to codepage '%s': %s",
975 codepage, std::strerror (errno));
985 retval.
xelem (i) = native_bytes[i];
990DEFUN (__locale_charset__, , ,
998 std::string charset_str (charset);
999 return ovl (charset_str);
1002DEFUN (unicode_idx, args, ,
1016 if (args.length () != 1)
1019 charNDArray str = args(0).xchar_array_value (
"STR must be a string");
1022 if (str.
ndims () > 1)
1031 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (str_p.
data ());
1043 idx(i+j) = u8_char_num;
1054DEFUN (__unicode_length__, args, ,
1072 if (args.length () != 1)
1075 bool arg_char = args(0).is_char_matrix ();
1077 if (! arg_char && ! args(0).iscellstr ())
1078 error (
"STR must be a character array or cell string.");
1086 if (str.
ndims () > 1)
1095 const uint8_t *src =
reinterpret_cast<const uint8_t *
> (str.
data ());
1098 retval =
ovl (mbsnlen);
1103 NDArray output (args(0).dims (),
false);
1107 =
reinterpret_cast<const uint8_t *
> (cellstr(i).c_str ());
1111 retval =
ovl (output);
1124DEFUN (__u8_validate__, args, ,
1136 int nargin = args.
length ();
1138 if (nargin < 1 || nargin > 2)
1142 std::string in_str =
1143 args(0).xstring_value (
"__u8_validate__: IN_STR must be a string");
1145 std::string mode =
"replace";
1147 mode = args(1).xstring_value (
"__u8_validate__: MODE must be a string");
1149 string::u8_fallback_type fb_type;
1150 if (mode ==
"replace")
1151 fb_type = string::U8_REPLACEMENT_CHAR;
1152 else if (mode ==
"unicode")
1153 fb_type = string::U8_ISO_8859_1;
1155 error (R
"(__u8_validate__: MODE must be either "replace" or "unicode")");
1157 string::u8_validate ("__u8_validate__", in_str, fb_type);
1159 return ovl (in_str);
1162DEFUN (newline, args, ,
1183 if (args.length () != 0)
1200DEFUN (list_in_columns, args, ,
1236 int nargin = args.
length ();
1238 if (nargin < 1 || nargin > 3)
1241 string_vector s = args(0).xstring_vector_value (
"list_in_columns: ARG must be a cellstr or char array");
1245 if (nargin > 1 && ! args(1).isempty ())
1246 width = args(1).strict_int_value (
"list_in_columns: WIDTH must be an integer");
1251 prefix = args(2).xstring_value (
"list_in_columns: PREFIX must be a string");
1253 std::ostringstream buf;
1257 return ovl (buf.str ());
1280OCTAVE_END_NAMESPACE(octave)
N Dimensional Array with copy-on-write semantics.
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.
int ndims() 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 T * data() const
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.
octave_idx_type length() const
Array< std::string > cellstr_value() const
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
octave_idx_type max_length() 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 numel() 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)
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)
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)