Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
StringView.cc
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.cc (C) 2000-2025 */
9/* */
10/* View of a UTF-8 character string. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/base/StringView.h"
15
16#include <iostream>
17
18/*---------------------------------------------------------------------------*/
19/*---------------------------------------------------------------------------*/
20
21namespace Arcane
22{
23
24/*---------------------------------------------------------------------------*/
25/*---------------------------------------------------------------------------*/
26
28writeBytes(std::ostream& o) const
29{
30 o.write((const char*)m_v.data(), m_v.size());
31}
32
33/*---------------------------------------------------------------------------*/
34/*---------------------------------------------------------------------------*/
35
37subView(Int64 pos) const
38{
39 return subView(pos, length() - pos);
40}
41
42/*---------------------------------------------------------------------------*/
43/*---------------------------------------------------------------------------*/
44
46subView(Int64 pos, Int64 len) const
47{
48 if (pos < 0)
49 pos = 0;
50 return { m_v.subspan(pos, len) };
51}
52
53/*---------------------------------------------------------------------------*/
54/*---------------------------------------------------------------------------*/
55
56std::ostream&
57operator<<(std::ostream& o, const StringView& str)
58{
59 str.writeBytes(o);
60 return o;
61}
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
65
66bool operator==(const StringView& a, const StringView& b)
67{
68 bool is_equal = (a.toStdStringView() == b.toStdStringView());
69 //std::cout << "COMPARE: a=" << a.length() << " '" << a << "'"
70 // << " b=" << b.length() << " '" << b << "' v=" << is_equal << '\n';
71 return is_equal;
72}
73
74/*---------------------------------------------------------------------------*/
75/*---------------------------------------------------------------------------*/
76
77bool operator==(const char* a, const StringView& b)
78{
79 return operator==(StringView(a), b);
80}
81
82/*---------------------------------------------------------------------------*/
83/*---------------------------------------------------------------------------*/
84
85bool operator==(const StringView& a, const char* b)
86{
87 return operator==(a, StringView(b));
88}
89
90/*---------------------------------------------------------------------------*/
91/*---------------------------------------------------------------------------*/
92
93bool operator<(const StringView& a, const StringView& b)
94{
95 return a.toStdStringView() < b.toStdStringView();
96}
97
98/*---------------------------------------------------------------------------*/
99/*---------------------------------------------------------------------------*/
100
101} // namespace Arcane
102
103/*---------------------------------------------------------------------------*/
104/*---------------------------------------------------------------------------*/
void writeBytes(std::ostream &o) const
Writes the string in UTF-8 format to the stream o.
Definition StringView.cc:28
std::string_view toStdStringView() const ARCCORE_NOEXCEPT
Returns an STL view of the current view.
Definition StringView.h:109
constexpr Int64 length() const ARCCORE_NOEXCEPT
Length in bytes of the character string.
Definition StringView.h:96
StringView subView(Int64 pos) const
Substring starting at position pos.
Definition StringView.cc:37
friend bool operator==(const StringView &a, const StringView &b)
Compares two views.
Definition StringView.cc:66
StringView()=default
Creates a view of an empty string.
-- 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
std::ostream & operator<<(std::ostream &ostr, eItemKind item_kind)
Output operator for a stream.