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