GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
oct-string.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2016-2025 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
62template <typename T>
63OCTAVE_API bool
64strcmp (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
76template <typename T>
77OCTAVE_API bool
78strcmp (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
88template <typename T>
89OCTAVE_API bool
90strcmpi (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
99template <typename T>
100OCTAVE_API bool
101strcmpi (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
111template <typename T>
112OCTAVE_API bool
113strncmp (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.
117template <typename T>
118OCTAVE_API bool
119strncmp (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
130template <typename T>
131OCTAVE_API bool
132strncmpi (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.
136template <typename T>
137OCTAVE_API bool
138strncmpi (const T& str_a, const typename T::value_type *str_b,
139 const typename T::size_type n);
140
141extern OCTAVE_API Complex
142str2double (const std::string& str_arg);
143
144extern OCTAVE_API std::string
145u8_to_encoding (const std::string& who, const std::string& u8_string,
146 const std::string& encoding);
147
148extern OCTAVE_API std::string
149u8_from_encoding (const std::string& who, const std::string& native_string,
150 const std::string& encoding);
151
157
158extern OCTAVE_API unsigned int
159u8_validate (const std::string& who, std::string& in_string,
161
162extern OCTAVE_API std::string
163u16_to_encoding (const std::string& who, const std::u16string& u16_string,
164 const std::string& encoding);
165
166extern OCTAVE_API std::vector<std::string>
168
169template<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
182class OCTAVE_API codecvt_u8 : public std::codecvt<char, char, std::mbstate_t>
183{
184public:
185
186 codecvt_u8 (const std::string& enc)
187 : m_enc (enc)
188 { }
189
190 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (codecvt_u8)
191
192 virtual ~codecvt_u8 () { }
193
194 typedef char InternT;
195 typedef char ExternT;
196 typedef std::mbstate_t StateT;
197
198private:
199
201 typename std::codecvt<InternT, ExternT, StateT>::result
202 do_out (StateT& state,
203 const InternT *from, const InternT *from_end, const InternT *&from_next,
204 ExternT *to, ExternT *to_end, ExternT *&to_next) const;
205
207 typename std::codecvt<InternT, ExternT, StateT>::result
208 do_in (StateT& state,
209 const ExternT *from, const ExternT *from_end, const ExternT *&from_next,
210 InternT *to, InternT *to_end, InternT *&to_next) const;
211
212 typename std::codecvt<InternT, ExternT, StateT>::result
213 do_unshift (StateT& /* state */, ExternT *to, ExternT * /* to_end */,
214 ExternT *&to_next) const
215 {
216 // FIXME: What is the correct thing to unshift?
217 // Just reset?
218 to_next = to;
219
220 return std::codecvt<InternT, ExternT, StateT>::ok;
221 }
222
223 int do_encoding () const throw ()
224 {
225 // return 0 because UTF-8 encoding is variable length
226 return 0;
227 }
228
229 bool do_always_noconv () const throw ()
230 {
231 // return false to indicate non-identity conversion
232 return false;
233 }
234
235 OCTAVE_API int
236 do_length (StateT& state, const ExternT *src, const ExternT *end,
237 std::size_t max) const;
238
239 int do_max_length() const throw ()
240 {
241 // For UTF-8, a maximum of 4 bytes are needed for one character.
242 return 4;
243 }
244
245 std::string m_enc;
246
247};
248
249OCTAVE_END_NAMESPACE(string)
250OCTAVE_END_NAMESPACE(octave)
251
252template <typename T>
253extern OCTAVE_API std::string
254rational_approx (T val, int len);
255
256#endif
charNDArray max(char d, const charNDArray &m)
Definition chNDArray.cc:230
std::mbstate_t StateT
Definition oct-string.h:196
codecvt_u8(const std::string &enc)
Definition oct-string.h:186
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
#define OCTAVE_API
Definition main.in.cc:55
std::complex< double > Complex
Definition oct-cmplx.h:33
octave::string::codecvt_u8::ExternT ExternT
octave::string::codecvt_u8::StateT StateT
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.
std::vector< std::string > get_encoding_list()
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.
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)
deletable_facet(Args &&...args)
Definition oct-string.h:175
deletable_facet()=delete
F77_RET_T len
Definition xerbla.cc:61