GNU Octave  8.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-string.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2016-2023 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 #if ! defined (octave_oct_string_h)
27 #define octave_oct_string_h 1
28 
29 #include "octave-config.h"
30 
31 #include <locale>
32 
33 #include "oct-cmplx.h"
34 
36 
37 //! Octave string utility functions.
38 //!
39 //! This functions provide a C++ interface to most string functions
40 //! available in the Octave interpreter.
41 //!
42 //! Specializations for Array may consider its dimensions in addition
43 //! to the actual string contents.
44 //!
45 //! @attention
46 //! Octave's string comparison functions return true when strings are
47 //! are equal, just the opposite of the corresponding C library functions.
48 //! In addition, Octave's function only return bool and do not check
49 //! lexicographical order.
50 
52 
53 //! True if strings are the same.
54 //!
55 //! ## Specialization for Array<char>
56 //!
57 //! When comparing whole Array of chars, the actual Array dimensions
58 //! are significant. A column vector and row vector with the same
59 //! char array, will still return false.
60 
61 template <typename T>
62 OCTAVE_API bool
63 strcmp (const T& str_a, const T& str_b);
64 
65 //! True if string is the same as character sequence.
66 //!
67 //! Compares a string to the null-terminated character sequence
68 //! beginning at the character pointed to by str_b.
69 //!
70 //! ## Specialization for Array<char>
71 //!
72 //! For purposes of comparison of dimensions, the character sequence
73 //! is considered to be a row vector.
74 
75 template <typename T>
76 OCTAVE_API bool
77 strcmp (const T& str_a, const typename T::value_type *str_b);
78 
79 //! True if strings are the same, ignoring case.
80 //!
81 //! ## Specialization for Array<char>
82 //!
83 //! When comparing whole Array of chars, the actual Array dimensions
84 //! are significant. A column vector and row vector with the same
85 //! char array, will still return false.
86 
87 template <typename T>
88 OCTAVE_API bool
89 strcmpi (const T& str_a, const T& str_b);
90 
91 //! True if string is the same as character sequence, ignoring case.
92 //!
93 //! ## Specialization for Array<char>
94 //!
95 //! For purposes of comparison of dimensions, the character sequence
96 //! is considered to be a row vector.
97 
98 template <typename T>
99 OCTAVE_API bool
100 strcmpi (const T& str_a, const typename T::value_type *str_b);
101 
102 //! True if the first N characters are the same.
103 //!
104 //! ## Specialization for Array<char>
105 //!
106 //! The comparison is done in the first N characters, the actual
107 //! dimensions of the Array are irrelevant. A row vector and
108 //! a column vector of the same still return true.
109 
110 template <typename T>
111 OCTAVE_API bool
112 strncmp (const T& str_a, const T& str_b,
113  const typename T::size_type n);
114 
115 //! True if the first N characters are the same.
116 template <typename T>
117 OCTAVE_API bool
118 strncmp (const T& str_a, const typename T::value_type *str_b,
119  const typename T::size_type n);
120 
121 //! True if the first N characters are the same, ignoring case.
122 //!
123 //! ## Specialization for Array<char>
124 //!
125 //! The comparison is done in the first N characters, the actual
126 //! dimensions of the Array are irrelevant. A row vector and
127 //! a column vector of the same still return true.
128 
129 template <typename T>
130 OCTAVE_API bool
131 strncmpi (const T& str_a, const T& str_b,
132  const typename T::size_type n);
133 
134 //! True if the first N characters are the same, ignoring case.
135 template <typename T>
136 OCTAVE_API bool
137 strncmpi (const T& str_a, const typename T::value_type *str_b,
138  const typename T::size_type n);
139 
140 extern OCTAVE_API Complex
141 str2double (const std::string& str_arg);
142 
143 extern OCTAVE_API std::string
144 u8_to_encoding (const std::string& who, const std::string& u8_string,
145  const std::string& encoding);
146 
147 extern OCTAVE_API std::string
148 u8_from_encoding (const std::string& who, const std::string& native_string,
149  const std::string& encoding);
150 
152 {
155 };
156 
157 extern OCTAVE_API unsigned int
158 u8_validate (const std::string& who, std::string& in_string,
160 
161 
162 template<class Facet>
163 struct
165 {
166  template<class ...Args>
167  deletable_facet (Args&& ...args)
168  : Facet (std::forward<Args> (args)...)
169  { }
170 
171  // destructor needs to be public
173 };
174 
175 class
177 codecvt_u8 : public std::codecvt<char, char, std::mbstate_t>
178 {
179 public:
180 
181  // No copying!
182 
183  codecvt_u8 (codecvt_u8&) = delete;
184 
185  codecvt_u8& operator = (codecvt_u8&) = delete;
186 
187  codecvt_u8 (const std::string& enc)
188  : m_enc (enc)
189  { }
190 
191  virtual ~codecvt_u8 () { }
192 
193  typedef char InternT;
194  typedef char ExternT;
195  typedef std::mbstate_t StateT;
196 
197 private:
198 
199  OCTAVE_API
200  typename std::codecvt<InternT, ExternT, StateT>::result
202  const InternT *from, const InternT *from_end, const InternT *&from_next,
203  ExternT *to, ExternT *to_end, ExternT *&to_next) const;
204 
205  OCTAVE_API
206  typename std::codecvt<InternT, ExternT, StateT>::result
208  const ExternT *from, const ExternT *from_end, const ExternT *&from_next,
209  InternT *to, InternT *to_end, InternT *&to_next) const;
210 
211  typename std::codecvt<InternT, ExternT, StateT>::result
212  do_unshift (StateT& /* state */, ExternT *to, ExternT * /* to_end */,
213  ExternT *&to_next) const
214  {
215  // FIXME: What is the correct thing to unshift?
216  // Just reset?
217  to_next = to;
218 
219  return std::codecvt<InternT, ExternT, StateT>::ok;
220  }
221 
222  int do_encoding () const throw ()
223  {
224  // return 0 because UTF-8 encoding is variable length
225  return 0;
226  }
227 
228  bool do_always_noconv () const throw ()
229  {
230  // return false to indicate non-identity conversion
231  return false;
232  }
233 
234  OCTAVE_API int
235  do_length (StateT& state, const ExternT *src, const ExternT *end,
236  std::size_t max) const;
237 
238  int do_max_length() const throw ()
239  {
240  // For UTF-8, a maximum of 4 bytes are needed for one character.
241  return 4;
242  }
243 
244  std::string m_enc;
245 
246 };
247 
248 OCTAVE_END_NAMESPACE(string)
250 
251 template <typename T>
252 extern OCTAVE_API std::string
253 rational_approx (T val, int len);
254 
255 #endif
OCTAVE_END_NAMESPACE(octave)
charNDArray max(char d, const charNDArray &m)
Definition: chNDArray.cc:230
OCTAVE_API int do_length(StateT &state, const ExternT *src, const ExternT *end, std::size_t max) const
OCTAVE_API std::codecvt< InternT, ExternT, StateT >::result do_in(StateT &state, const ExternT *from, const ExternT *from_end, const ExternT *&from_next, InternT *to, InternT *to_end, InternT *&to_next) const
bool do_always_noconv() const
Definition: oct-string.h:228
codecvt_u8(codecvt_u8 &)=delete
char ExternT
Definition: oct-string.h:194
int do_max_length() const
Definition: oct-string.h:238
std::codecvt< InternT, ExternT, StateT >::result do_unshift(StateT &, ExternT *to, ExternT *, ExternT *&to_next) const
Definition: oct-string.h:212
std::string m_enc
Definition: oct-string.h:244
int do_encoding() const
Definition: oct-string.h:222
char InternT
Definition: oct-string.h:193
std::mbstate_t StateT
Definition: oct-string.h:195
codecvt_u8(const std::string &enc)
Definition: oct-string.h:187
OCTAVE_API std::codecvt< InternT, ExternT, StateT >::result do_out(StateT &state, const InternT *from, const InternT *from_end, const InternT *&from_next, ExternT *to, ExternT *to_end, ExternT *&to_next) const
virtual ~codecvt_u8()
Definition: oct-string.h:191
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition: main.in.cc:55
octave_idx_type n
Definition: mx-inlines.cc:753
std::complex< double > Complex
Definition: oct-cmplx.h:33
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.
u8_fallback_type
Definition: oct-string.h:152
@ U8_ISO_8859_1
Definition: oct-string.h:154
@ U8_REPLACEMENT_CHAR
Definition: oct-string.h:153
OCTAVE_API Complex str2double(const std::string &str_arg)
OCTAVE_API std::string rational_approx(T val, int len)
Definition: oct-string.cc:696
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.
static uint32_t state[624]
Definition: randmtzig.cc:193
deletable_facet(Args &&...args)
Definition: oct-string.h:167
F77_RET_T len
Definition: xerbla.cc:61