26 #if defined (HAVE_CONFIG_H)
47 str_data_cmp (
const typename T::value_type *a,
const typename T::value_type *b,
48 const typename T::size_type
n)
50 for (
typename T::size_type i = 0; i <
n; ++i)
58 str_data_cmpi (
const typename T::value_type *a,
const typename T::value_type *b,
59 const typename T::size_type
n)
61 for (
typename T::size_type i = 0; i <
n; ++i)
62 if (std::tolower (a[i]) != std::tolower (b[i]))
85 strlen (
const typename T::value_type *str)
94 return str_a.size () == str_b.size ();
101 return str_a.
dims () == str_b.
dims ();
104 template <
typename T>
106 sizes_cmp (
const T& str_a,
const typename T::value_type *str_b)
108 return str_a.size () == strlen<T> (str_b);
125 && str_data_cmp<T> (str_a.data (), str_b.data (),
numel (str_a)));
133 && str_data_cmp<T> (str_a.data (), str_b,
numel (str_a)));
142 && str_data_cmpi<T> (str_a.data (), str_b.data (),
numel (str_a)));
150 && str_data_cmpi<T> (str_a.data (), str_b,
numel (str_a)));
157 const typename T::size_type
n)
159 typename T::size_type neff;
160 auto len_a =
numel (str_a);
161 auto len_b =
numel (str_b);
164 return (len_a >= neff && len_b >= neff
165 && str_data_cmp<T> (str_a.data (), str_b.data (), neff));
171 const typename T::size_type
n)
173 typename T::size_type neff;
174 auto len_a =
numel (str_a);
175 auto len_b = strlen<T> (str_b);
178 return (len_a >= neff && len_b >= neff
179 && str_data_cmp<T> (str_a.data (), str_b, neff));
186 const typename T::size_type
n)
188 typename T::size_type neff;
189 auto len_a =
numel (str_a);
190 auto len_b =
numel (str_b);
193 return (len_a >= neff && len_b >= neff
194 && str_data_cmpi<T> (str_a.data (), str_b.data (), neff));
200 const typename T::size_type
n)
202 typename T::size_type neff;
203 auto len_a =
numel (str_a);
204 auto len_b = strlen<T> (str_b);
207 return (len_a >= neff && len_b >= neff
208 && str_data_cmpi<T> (str_a.data (), str_b, neff));
213 #define INSTANTIATE_OCTAVE_STRING(T, API) \
214 template API bool octave::string::strcmp<T> (const T&, const T&); \
216 octave::string::strcmp<T> (const T&, const typename T::value_type*); \
217 template API bool octave::string::strcmpi<T> (const T&, const T&); \
219 octave::string::strcmpi<T> (const T&, const typename T::value_type*); \
221 octave::string::strncmp<T> (const T&, const T&, \
222 const typename T::size_type); \
224 octave::string::strncmp<T> (const T&, const typename T::value_type*, \
225 const typename T::size_type); \
227 octave::string::strncmpi<T> (const T&, const T&, \
228 const typename T::size_type n); \
230 octave::string::strncmpi<T> (const T&, const typename T::value_type*, \
231 const typename T::size_type);
238 #undef INSTANTIATE_OCTAVE_STRING
242 {
return c ==
'i' || c ==
'j'; }
258 if (std::toupper (c) ==
'I')
264 if (std::tolower (c1) ==
'n' && std::tolower (c2) ==
'f')
270 is.setstate (std::ios::failbit);
285 if (c1 ==
'a' && c2 ==
'N')
291 is.setstate (std::ios::failbit);
300 static std::istringstream&
303 have_sign =
imag =
false;
314 bool negative =
false;
317 if (c ==
'+' || c ==
'-')
343 num = (negative ? -1.0 : 1.0);
348 if (std::tolower (c) !=
'n')
413 is.setstate (std::ios::failbit);
443 #if defined (HAVE_CXX_COMPLEX_SETTERS)
448 #elif defined (HAVE_CXX_COMPLEX_REFERENCE_ACCESSORS)
466 std::string str = str_arg;
470 str.erase (std::remove (str.begin (), str.end(),
','), str.end ());
471 std::istringstream is (str);
486 if (!
extract_num (is, num, i2, s2) || i1 == i2 || ! s2)
498 const std::string& u8_string,
499 const std::string& encoding)
501 const uint8_t *src =
reinterpret_cast<const uint8_t *
>
502 (u8_string.c_str ());
503 std::size_t srclen = u8_string.length ();
512 (*current_liboctave_error_handler)
513 (
"%s: iconv() is not supported. Installing GNU libiconv and then "
514 "re-compiling Octave could fix this.", who.c_str ());
517 (
"%s: converting from UTF-8 to codepage '%s' failed: %s",
518 who.c_str (), encoding.c_str (), std::strerror (errno));
521 octave::unwind_action free_native_str ([=] () {
::free (native_str); });
523 std::string retval = std::string (native_str, length);
530 const std::string& native_string,
531 const std::string& encoding)
533 const char *src = native_string.c_str ();
534 std::size_t srclen = native_string.length ();
542 (*current_liboctave_error_handler)
543 (
"%s: iconv() is not supported. Installing GNU libiconv and then "
544 "re-compiling Octave could fix this.", who.c_str ());
547 (
"%s: converting from codepage '%s' to UTF-8 failed: %s",
548 who.c_str (), encoding.c_str (), std::strerror (errno));
551 octave::unwind_action free_utf8_str ([=] () {
::free (utf8_str); });
553 std::string retval = std::string (
reinterpret_cast<char *
> (utf8_str), length);
565 unsigned int num_replacements = 0;
566 const char *in_chr = in_str.c_str ();
567 const char *inv_utf8 = in_chr;
568 const char *
const in_end = in_chr + in_str.length ();
569 while (inv_utf8 && in_chr < in_end)
571 inv_utf8 =
reinterpret_cast<const char *
>
575 if (inv_utf8 ==
nullptr)
576 out_str.append (in_chr, in_end - in_chr);
580 out_str.append (in_chr, inv_utf8 - in_chr);
581 in_chr = inv_utf8 + 1;
584 out_str.append (
"\xef\xbf\xbd");
587 std::string fallback =
"iso-8859-1";
590 (fallback.c_str (), inv_utf8, 1, &lengthp);
593 (*current_liboctave_error_handler)
594 (
"%s: converting from codepage '%s' to UTF-8 failed: %s",
595 who.c_str (), fallback.c_str (), std::strerror (errno));
597 octave::unwind_action free_val_utf8
598 ([=] () {
::free (val_utf8); });
600 out_str.append (
reinterpret_cast<const char *
> (val_utf8),
607 return num_replacements;
614 typename std::codecvt<InternT, ExternT, StateT>::result
615 octave::string::codecvt_u8::do_out
621 return std::codecvt<InternT, ExternT, StateT>::noconv;
624 std::size_t srclen = (from_end-from) *
sizeof (
InternT);
625 std::size_t lengthp = (to_end-to) *
sizeof (
ExternT);
626 const uint8_t *u8_str =
reinterpret_cast<const uint8_t *
> (from);
630 size_t max = to_end - to;
636 std::copy_n (enc_str,
max, to);
639 from_next = from + srclen;
642 return std::codecvt<InternT, ExternT, StateT>::ok;
645 typename std::codecvt<InternT, ExternT, StateT>::result
646 octave::string::codecvt_u8::do_in
652 std::size_t srclen = (from_end-from) *
sizeof (
ExternT);
653 std::size_t lengthp = (to_end-to) *
sizeof (
InternT);
654 const char *enc_str =
reinterpret_cast<const char *
> (from);
656 enc_str, srclen, &lengthp);
658 std::size_t
max = to_end - to;
663 std::copy_n (u8_str,
max, to);
666 from_next = from + srclen;
669 return std::codecvt<InternT, ExternT, StateT>::ok;
672 int octave::string::codecvt_u8::do_length
674 std::size_t
max)
const
677 std::size_t srclen = end-src;
678 std::size_t offsets[srclen];
679 std::size_t lengthp =
max;
682 std::size_t ext_char;
683 for (ext_char = 0; ext_char < srclen; ext_char++)
685 if (offsets[ext_char] !=
static_cast<size_t> (-1)
686 && offsets[ext_char] >=
max)
694 template <
typename T>
703 static const T out_of_range_top
705 static const T out_of_range_bottom
716 else if (val <= out_of_range_bottom || val >= out_of_range_top
719 std::ostringstream buf;
720 buf.flags (std::ios::fixed);
732 std::ostringstream init_buf;
733 init_buf.flags (std::ios::fixed);
734 init_buf << std::setprecision (0) << static_cast<int> (
n);
745 if (
std::abs (flip) > out_of_range_top)
753 n = step *
n + lastn;
754 d = step *
d + lastd;
758 std::ostringstream buf;
759 buf.flags (std::ios::fixed);
760 buf << std::setprecision (0) << static_cast<int> (
n)
761 <<
'/' <<
static_cast<int> (
d);
766 if (buf.str ().length () >
static_cast<unsigned int> (
len + 2))
771 if (buf.str ().length () >
static_cast<unsigned int> (
len))
787 std::ostringstream buf;
788 buf.flags (std::ios::fixed);
789 buf << std::setprecision (0) << static_cast<int> (lastn)
790 <<
'/' <<
static_cast<int> (lastd);
charNDArray max(char d, const charNDArray &m)
charNDArray min(char d, const charNDArray &m)
OCTARRAY_OVERRIDABLE_FUNC_API bool isvector(void) const
Size of the specified dimension.
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type numel(void) const
Number of elements in the array.
OCTARRAY_OVERRIDABLE_FUNC_API const dim_vector & dims(void) const
Return a const-reference so that dims ()(i) works efficiently.
OCTARRAY_OVERRIDABLE_FUNC_API octave_idx_type rows(void) const
ColumnVector imag(const ComplexColumnVector &a)
OCTAVE_NORETURN liboctave_error_handler current_liboctave_error_handler
F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE * d
std::complex< double > Complex
static void set_component(Complex &c, double num, bool imag)
octave::string::codecvt_u8::ExternT ExternT
static double single_num(std::istringstream &is)
static bool str_data_cmp(const typename T::value_type *a, const typename T::value_type *b, const typename T::size_type n)
static bool is_imag_unit(int c)
#define INSTANTIATE_OCTAVE_STRING(T, API)
static std::istringstream & extract_num(std::istringstream &is, double &num, bool &imag, bool &have_sign)
octave::string::codecvt_u8::InternT InternT
static bool str_data_cmpi(const typename T::value_type *a, const typename T::value_type *b, const typename T::size_type n)
octave::string::codecvt_u8::StateT StateT
template OCTAVE_API std::string rational_approx< float >(float val, int len)
T::size_type numel(const T &str)
bool sizes_cmp(const T &str_a, const T &str_b)
T::size_type strlen(const typename T::value_type *str)
template OCTAVE_API std::string rational_approx< double >(double val, int len)
std::string rational_approx(T val, int len)
OCTAVE_API unsigned int u8_validate(const std::string &who, std::string &in_string, const u8_fallback_type type=U8_REPLACEMENT_CHAR)
OCTAVE_API std::string u8_to_encoding(const std::string &who, const std::string &u8_string, const std::string &encoding)
OCTAVE_API bool strcmpi(const T &str_a, const T &str_b)
True if strings are the same, ignoring case.
OCTAVE_API bool strcmp(const T &str_a, const T &str_b)
Octave string utility functions.
OCTAVE_API Complex str2double(const std::string &str_arg)
OCTAVE_API std::string u8_from_encoding(const std::string &who, const std::string &native_string, const std::string &encoding)
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 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.
uint8_t * octave_u8_conv_from_encoding_offsets(const char *fromcode, const char *src, size_t srclen, size_t *offsets, size_t *lengthp)
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)
const uint8_t * octave_u8_check_wrapper(const uint8_t *src, size_t n)