GNU Octave 11.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-2026 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
144//! True if any of the characters is outside the range of ASCII
145extern OCTAVE_API bool
146any_non_ascii_chars (const std::string &s);
147
148extern OCTAVE_API std::string
149u8_to_encoding (const std::string& who, const std::string& u8_string,
150 const std::string& encoding);
151
152extern OCTAVE_API std::string
153u8_from_encoding (const std::string& who, const std::string& native_string,
154 const std::string& encoding);
155
161
162extern OCTAVE_API unsigned int
163u8_validate (const std::string& who, std::string& in_string,
165
166extern OCTAVE_API std::string
167u16_to_encoding (const std::string& who, const std::u16string& u16_string,
168 const std::string& encoding);
169
170extern OCTAVE_API std::vector<std::string>
172
173template<class Facet>
175{
176 deletable_facet () = delete;
177
178 template<class ...Args>
179 deletable_facet (Args&& ...args)
180 : Facet (std::forward<Args> (args)...)
181 { }
182
183 OCTAVE_DEFAULT_COPY_MOVE_DELETE (deletable_facet)
184};
185
186class OCTAVE_API codecvt_u8 : public std::codecvt<char, char, std::mbstate_t>
187{
188public:
189
190 codecvt_u8 (const std::string& enc)
191 : m_enc (enc)
192 { }
193
194 OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (codecvt_u8)
195
196 virtual ~codecvt_u8 () { }
197
198 typedef char InternT;
199 typedef char ExternT;
200 typedef std::mbstate_t StateT;
201
202private:
203
205 typename std::codecvt<InternT, ExternT, StateT>::result
206 do_out (StateT& state,
207 const InternT *from, const InternT *from_end, const InternT *&from_next,
208 ExternT *to, ExternT *to_end, ExternT *&to_next) const;
209
211 typename std::codecvt<InternT, ExternT, StateT>::result
212 do_in (StateT& state,
213 const ExternT *from, const ExternT *from_end, const ExternT *&from_next,
214 InternT *to, InternT *to_end, InternT *&to_next) const;
215
216 typename std::codecvt<InternT, ExternT, StateT>::result
217 do_unshift (StateT& /* state */, ExternT *to, ExternT * /* to_end */,
218 ExternT *&to_next) const
219 {
220 // FIXME: What is the correct thing to unshift?
221 // Just reset?
222 to_next = to;
223
224 return std::codecvt<InternT, ExternT, StateT>::ok;
225 }
226
227 int do_encoding () const throw ()
228 {
229 // return 0 because UTF-8 encoding is variable length
230 return 0;
231 }
232
233 bool do_always_noconv () const throw ()
234 {
235 // return false to indicate non-identity conversion
236 return false;
237 }
238
239 OCTAVE_API int
240 do_length (StateT& state, const ExternT *src, const ExternT *end,
241 std::size_t max) const;
242
243 int do_max_length() const throw ()
244 {
245 // For UTF-8, a maximum of 4 bytes are needed for one character.
246 return 4;
247 }
248
249 std::string m_enc;
250
251};
252
253OCTAVE_END_NAMESPACE(string)
254OCTAVE_END_NAMESPACE(octave)
255
256template <typename T>
257extern OCTAVE_API std::string
258rational_approx (T val, int len);
259
260#endif
charNDArray max(char d, const charNDArray &m)
Definition chNDArray.cc:230
std::mbstate_t StateT
Definition oct-string.h:200
codecvt_u8(const std::string &enc)
Definition oct-string.h:190
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:157
@ U8_ISO_8859_1
Definition oct-string.h:159
@ U8_REPLACEMENT_CHAR
Definition oct-string.h:158
std::string u8_to_encoding(const std::string &who, const std::string &u8_string, const std::string &encoding)
bool any_non_ascii_chars(const std::string &s)
True if any of the characters is outside the range of ASCII.
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:179
deletable_facet()=delete
F77_RET_T len
Definition xerbla.cc:61