Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
StringView.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2026 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* StringView.h (C) 2000-2025 */
9/* */
10/* View of a UTF-8 character string. */
11/*---------------------------------------------------------------------------*/
12#ifndef ARCCORE_BASE_STRINGVIEW_H
13#define ARCCORE_BASE_STRINGVIEW_H
14/*---------------------------------------------------------------------------*/
15/*---------------------------------------------------------------------------*/
16
18#include "arccore/base/Span.h"
19
20#include <string_view>
21
22/*---------------------------------------------------------------------------*/
23/*---------------------------------------------------------------------------*/
24
25namespace Arcane
26{
27
28/*---------------------------------------------------------------------------*/
29/*---------------------------------------------------------------------------*/
30
31/*!
32 * \brief View of a UTF-8 character string.
33 *
34 * This class is similar to std::string_view in C++17 in that it only holds
35 * a pointer to memory managed by another class.
36 * Therefore, instances of this class should not be kept. The
37 * main difference lies in the encoding, which must be UTF-8 with this class.
38 *
39 * \note Like the std::string_view class, the \a bytes() array does not
40 * necessarily contain a null terminator. This means, among other things,
41 * that this class should not be used to pass parameters to C functions.
42 */
43class ARCCORE_BASE_EXPORT StringView
44{
45 public:
46
47 //! Creates a view of an empty string
48 StringView() = default;
49 //! Creates a view from \a str encoded in UTF-8. \a str may be null.
50 StringView(const char* str) ARCCORE_NOEXCEPT
51 : StringView(str ? std::string_view(str) : std::string_view()) {}
52 //! Creates a string from \a str in UTF-8 encoding
53 StringView(std::string_view str) ARCCORE_NOEXCEPT
54 : m_v(reinterpret_cast<const Byte*>(str.data()), str.size()) {}
55 //! Creates a string from \a str in UTF-8 encoding
56 constexpr StringView(Span<const Byte> str) ARCCORE_NOEXCEPT
57 : m_v(str) {}
58 //! Copy constructor
59 constexpr StringView(const StringView& str) = default;
60 //! Copies the view \a str into this instance.
61 constexpr StringView& operator=(const StringView& str) = default;
62 //! Creates a view from \a str encoded in UTF-8
63 StringView& operator=(const char* str) ARCCORE_NOEXCEPT
64 {
65 operator=(str ? std::string_view(str) : std::string_view());
66 return (*this);
67 }
68 //! Creates a view from \a str encoded in UTF-8
69 StringView& operator=(std::string_view str) ARCCORE_NOEXCEPT
70 {
71 m_v = Span<const Byte>(reinterpret_cast<const Byte*>(str.data()), str.size());
72 return (*this);
73 }
74 //! Creates a view from \a str encoded in UTF-8
75 constexpr StringView& operator=(Span<const Byte> str) ARCCORE_NOEXCEPT
76 {
77 m_v = str;
78 return (*this);
79 }
80
81 ~StringView() = default; //!< Frees resources.
82
83 public:
84
85 /*!
86 * \brief Returns the conversion of the instance in UTF-8 encoding.
87 *
88 * \warning The returned instance does not contain a null terminator.
89 *
90 * \warning The instance remains the owner of the returned value, and this value
91 * is invalidated by any modification of this instance.
92 */
93 constexpr Span<const Byte> bytes() const ARCCORE_NOEXCEPT { return m_v; }
94
95 //! Length in bytes of the character string.
96 constexpr Int64 length() const ARCCORE_NOEXCEPT { return m_v.size(); }
97
98 //! Length in bytes of the character string.
99 constexpr Int64 size() const ARCCORE_NOEXCEPT { return m_v.size(); }
100
101 //! True if the string is null or empty.
102 constexpr bool empty() const ARCCORE_NOEXCEPT { return size() == 0; }
103
104 public:
105
106 /*!
107 * \brief Returns an STL view of the current view.
108 */
109 std::string_view toStdStringView() const ARCCORE_NOEXCEPT
110 {
111 return std::string_view(reinterpret_cast<const char*>(m_v.data()), m_v.size());
112 }
113
114 //! StringView output operator
115 friend ARCCORE_BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringView&);
116
117 /*!
118 * \brief Compares two views.
119 * \retval true if they are equal,
120 * \retval false otherwise.
121 */
122 friend ARCCORE_BASE_EXPORT bool operator==(const StringView& a, const StringView& b);
123
124 /*!
125 * \brief Compares two Unicode strings.
126 * \retval true if they are different,
127 * \retval false if they are equal.
128 * \relate String
129 */
130 friend inline bool operator!=(const StringView& a, const StringView& b)
131 {
132 return !operator==(a, b);
133 }
134
135 /*!
136 * \brief Compares two Unicode strings.
137 * \retval true if they are equal,
138 * \retval false otherwise.
139 * \relate String
140 */
141 friend ARCCORE_BASE_EXPORT bool operator==(const char* a, const StringView& b);
142
143 /*!
144 * \brief Compares two Unicode strings.
145 * \retval true if they are different,
146 * \retval false if they are equal.
147 * \relate String
148 */
149 friend bool operator!=(const char* a, const StringView& b) { return !operator==(a, b); }
150
151 /*!
152 * \brief Compares two Unicode strings.
153 * \retval true if they are equal,
154 * \retval false otherwise.
155 * \relate String
156 */
157 friend ARCCORE_BASE_EXPORT bool operator==(const StringView& a, const char* b);
158
159 /*!
160 * \brief Compares two Unicode strings.
161 * \retval true if they are different,
162 * \retval false if they are equal.
163 * \relate String
164 */
165 friend inline bool operator!=(const StringView& a, const char* b)
166 {
167 return !operator==(a, b);
168 }
169
170 /*!
171 * \brief Compares two Unicode strings.
172 * \retval true if a<b
173 * \retval false otherwise.
174 * \relate String
175 */
176 friend ARCCORE_BASE_EXPORT bool operator<(const StringView& a, const StringView& b);
177
178 public:
179
180 //! Writes the string in UTF-8 format to the stream \a o
181 void writeBytes(std::ostream& o) const;
182
183 //! Substring starting at position \a pos
184 StringView subView(Int64 pos) const;
185
186 //! Substring starting at position \a pos and of length \a len
187 StringView subView(Int64 pos, Int64 len) const;
188
189 private:
190
192};
193
194/*---------------------------------------------------------------------------*/
195/*---------------------------------------------------------------------------*/
196
197} // namespace Arcane
198
199/*---------------------------------------------------------------------------*/
200/*---------------------------------------------------------------------------*/
201
202#endif
Declarations of types for the 'base' component of Arccore.
Types and functions associated with the classes SpanImpl, SmallSpan and Span.
View of an array of elements of type T.
Definition Span.h:635
View of a UTF-8 character string.
Definition StringView.h:44
constexpr StringView & operator=(const StringView &str)=default
Copies the view str into this instance.
constexpr StringView(Span< const Byte > str) ARCCORE_NOEXCEPT
Creates a string from str in UTF-8 encoding.
Definition StringView.h:56
constexpr Int64 size() const ARCCORE_NOEXCEPT
Length in bytes of the character string.
Definition StringView.h:99
constexpr Span< const Byte > bytes() const ARCCORE_NOEXCEPT
Returns the conversion of the instance in UTF-8 encoding.
Definition StringView.h:93
friend bool operator!=(const char *a, const StringView &b)
Compares two Unicode strings.
Definition StringView.h:149
std::string_view toStdStringView() const ARCCORE_NOEXCEPT
Returns an STL view of the current view.
Definition StringView.h:109
~StringView()=default
Frees resources.
StringView(std::string_view str) ARCCORE_NOEXCEPT
Creates a string from str in UTF-8 encoding.
Definition StringView.h:53
constexpr Int64 length() const ARCCORE_NOEXCEPT
Length in bytes of the character string.
Definition StringView.h:96
friend bool operator==(const StringView &a, const StringView &b)
Compares two views.
Definition StringView.cc:66
StringView & operator=(std::string_view str) ARCCORE_NOEXCEPT
Creates a view from str encoded in UTF-8.
Definition StringView.h:69
friend bool operator!=(const StringView &a, const StringView &b)
Compares two Unicode strings.
Definition StringView.h:130
constexpr StringView & operator=(Span< const Byte > str) ARCCORE_NOEXCEPT
Creates a view from str encoded in UTF-8.
Definition StringView.h:75
StringView(const char *str) ARCCORE_NOEXCEPT
Creates a view from str encoded in UTF-8. str may be null.
Definition StringView.h:50
constexpr bool empty() const ARCCORE_NOEXCEPT
True if the string is null or empty.
Definition StringView.h:102
friend bool operator!=(const StringView &a, const char *b)
Compares two Unicode strings.
Definition StringView.h:165
StringView & operator=(const char *str) ARCCORE_NOEXCEPT
Creates a view from str encoded in UTF-8.
Definition StringView.h:63
StringView()=default
Creates a view of an empty string.
constexpr StringView(const StringView &str)=default
Copy constructor.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
std::int64_t Int64
Signed integer type of 64 bits.
bool operator<(const Item &item1, const Item &item2)
Compare two entities.
Definition Item.h:566
unsigned char Byte
Type of a byte.
Definition BaseTypes.h:43
std::ostream & operator<<(std::ostream &ostr, eItemKind item_kind)
Output operator for a stream.