Arcane  4.1.12.0
Developer 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
43class ARCCORE_BASE_EXPORT StringView
44{
45 public:
46
48 StringView() = default;
50 StringView(const char* str) ARCCORE_NOEXCEPT
51 : StringView(str ? std::string_view(str) : std::string_view()) {}
52
53 StringView(std::string_view str) ARCCORE_NOEXCEPT
54 : m_v(reinterpret_cast<const Byte*>(str.data()), str.size()) {}
55
56 constexpr StringView(Span<const Byte> str) ARCCORE_NOEXCEPT
57 : m_v(str) {}
58
59 constexpr StringView(const StringView& str) = default;
61 constexpr StringView& operator=(const StringView& str) = default;
63 StringView& operator=(const char* str) ARCCORE_NOEXCEPT
64 {
65 operator=(str ? std::string_view(str) : std::string_view());
66 return (*this);
67 }
68
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
75 constexpr StringView& operator=(Span<const Byte> str) ARCCORE_NOEXCEPT
76 {
77 m_v = str;
78 return (*this);
79 }
80
81 ~StringView() = default;
82
83 public:
84
93 constexpr Span<const Byte> bytes() const ARCCORE_NOEXCEPT { return m_v; }
94
96 constexpr Int64 length() const ARCCORE_NOEXCEPT { return m_v.size(); }
97
99 constexpr Int64 size() const ARCCORE_NOEXCEPT { return m_v.size(); }
100
102 constexpr bool empty() const ARCCORE_NOEXCEPT { return size() == 0; }
103
104 public:
105
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
115 friend ARCCORE_BASE_EXPORT std::ostream& operator<<(std::ostream& o, const StringView&);
116
122 friend ARCCORE_BASE_EXPORT bool operator==(const StringView& a, const StringView& b);
123
130 friend inline bool operator!=(const StringView& a, const StringView& b)
131 {
132 return !operator==(a, b);
133 }
134
141 friend ARCCORE_BASE_EXPORT bool operator==(const char* a, const StringView& b);
142
149 friend bool operator!=(const char* a, const StringView& b) { return !operator==(a, b); }
150
157 friend ARCCORE_BASE_EXPORT bool operator==(const StringView& a, const char* b);
158
165 friend inline bool operator!=(const StringView& a, const char* b)
166 {
167 return !operator==(a, b);
168 }
169
176 friend ARCCORE_BASE_EXPORT bool operator<(const StringView& a, const StringView& b);
177
178 public:
179
181 void writeBytes(std::ostream& o) const;
182
184 StringView subView(Int64 pos) const;
185
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