Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
StringBuilder.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2023 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/* StringBuilder.cc (C) 2000-2023 */
9/* */
10/* Constructeur de chaîne de caractère unicode. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arccore/base/StringBuilder.h"
15#include "arccore/base/internal/StringImpl.h"
16#include "arccore/base/String.h"
17#include "arccore/base/StringView.h"
18
19/*---------------------------------------------------------------------------*/
20/*---------------------------------------------------------------------------*/
21
22namespace Arccore
23{
24
25/*---------------------------------------------------------------------------*/
26/*---------------------------------------------------------------------------*/
27
29StringBuilder(const std::string& str)
30: m_p(new StringImpl(str))
31{
32 m_p->addReference();
33}
34
35/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
40: m_p(new StringImpl(ustr))
41{
42 m_p->addReference();
43}
44
45/*---------------------------------------------------------------------------*/
46/*---------------------------------------------------------------------------*/
47
50: m_p(new StringImpl(ustr))
51{
52 m_p->addReference();
53}
54
55/*---------------------------------------------------------------------------*/
56/*---------------------------------------------------------------------------*/
57
60: m_p(impl)
61{
62 if (m_p)
63 m_p->addReference();
64}
65
66/*---------------------------------------------------------------------------*/
67/*---------------------------------------------------------------------------*/
68
70StringBuilder(const char* str,Integer len)
71: m_p(new StringImpl(std::string_view(str,len)))
72{
73 m_p->addReference();
74}
75
76/*---------------------------------------------------------------------------*/
77/*---------------------------------------------------------------------------*/
78
80StringBuilder(const char* str)
81: m_p(0)
82{
83 const bool do_alloc = true;
84 if (do_alloc){
85 m_p = new StringImpl(str);
86 m_p->addReference();
87 }
88 else{
89 m_p = 0;
90 m_const_ptr = str;
91 }
92}
93
94/*---------------------------------------------------------------------------*/
95/*---------------------------------------------------------------------------*/
96
98StringBuilder(const String& str)
99: m_p(str.m_p)
100, m_const_ptr(str.m_const_ptr)
101{
102 if (m_p)
103 m_p->addReference();
104}
105
106/*---------------------------------------------------------------------------*/
107/*---------------------------------------------------------------------------*/
108
111: m_p(nullptr)
112, m_const_ptr(str.m_const_ptr)
113{
114 if (str.m_p){
115 m_p = new StringImpl(*str.m_p);
116 m_p->addReference();
117 }
118}
119
120/*---------------------------------------------------------------------------*/
121/*---------------------------------------------------------------------------*/
122
125{
126 if (m_p)
127 m_p->removeReference();
128}
129
130/*---------------------------------------------------------------------------*/
131/*---------------------------------------------------------------------------*/
132
134operator=(const String& str)
135{
136 if (str.m_p)
137 str.m_p->addReference();
138 if (m_p)
139 m_p->removeReference();
140 m_p = str.m_p;
141 m_const_ptr = str.m_const_ptr;
142 return (*this);
143}
144
145/*---------------------------------------------------------------------------*/
146/*---------------------------------------------------------------------------*/
147
149operator=(const char* str)
150{
151 if (m_p)
152 m_p->removeReference();
153 m_p = 0;
154 m_const_ptr = str;
155 return (*this);
156}
157
158/*---------------------------------------------------------------------------*/
159/*---------------------------------------------------------------------------*/
160
163operator=(const StringBuilder& str)
164{
165 if (str!=(*this)){
166 if (m_p)
167 m_p->removeReference();
168 m_const_ptr = str.m_const_ptr;
169 if (str.m_p){
170 m_p = new StringImpl(*str.m_p);
171 m_p->addReference();
172 }
173 }
174 return (*this);
175}
176
177/*---------------------------------------------------------------------------*/
178/*---------------------------------------------------------------------------*/
179
180void StringBuilder::
181_checkClone() const
182{
183 if (m_const_ptr || !m_p){
184 std::string_view sv;
185 if (m_const_ptr)
186 sv = std::string_view(m_const_ptr);
187 m_p = new StringImpl(sv);
188 m_p->addReference();
189 m_const_ptr = nullptr;
190 return;
191 }
192 if (m_p->nbReference()!=1){
193 StringImpl* old_p = m_p;
194 m_p = m_p->clone();
195 m_p->addReference();
196 old_p->removeReference();
197 }
198}
199
200/*---------------------------------------------------------------------------*/
201/*---------------------------------------------------------------------------*/
202
204clone() const
205{
206 if (m_p)
207 return StringBuilder(m_p->clone());
208 return StringBuilder(*this);
209}
210
211/*---------------------------------------------------------------------------*/
212/*---------------------------------------------------------------------------*/
213
215toString() const
216{
217 if (m_p)
218 return String(m_p->clone());
219 return String(StringView(m_const_ptr));
220}
221
222/*---------------------------------------------------------------------------*/
223/*---------------------------------------------------------------------------*/
224
225StringBuilder::
226operator String() const
227{
228 return toString();
229}
230
231/*---------------------------------------------------------------------------*/
232/*---------------------------------------------------------------------------*/
233
235append(const String& str)
236{
237 if (str.null())
238 return *this;
239 _checkClone();
240 if (str.m_const_ptr){
241 StringView sv{std::string_view(str.m_const_ptr,str.m_const_ptr_size)};
242 m_p = m_p->append(sv);
243 }
244 else
245 m_p = m_p->append(str.m_p);
246 return *this;
247}
248
249/*---------------------------------------------------------------------------*/
250/*---------------------------------------------------------------------------*/
251
254{
255 _checkClone();
256 m_p = m_p->replaceWhiteSpace();
257 return *this;
258}
259
260/*---------------------------------------------------------------------------*/
261/*---------------------------------------------------------------------------*/
262
265{
266 _checkClone();
267 m_p = m_p->collapseWhiteSpace();
268 return *this;
269}
270
271/*---------------------------------------------------------------------------*/
272/*---------------------------------------------------------------------------*/
273
275toUpper()
276{
277 _checkClone();
278 m_p = m_p->toUpper();
279 return *this;
280}
281
282/*---------------------------------------------------------------------------*/
283/*---------------------------------------------------------------------------*/
284
286toLower()
287{
288 _checkClone();
289 m_p = m_p->toLower();
290 return *this;
291}
292
293/*---------------------------------------------------------------------------*/
294/*---------------------------------------------------------------------------*/
295
296void StringBuilder::
297operator+=(const char* str)
298{
299 append(str);
300}
301
302void StringBuilder::
303operator+=(const String& str)
304{
305 append(str);
306}
307
308void StringBuilder::
309operator+=(char v)
310{
311 char buf[2];
312 buf[0] = v;
313 buf[1] = '\0';
314 append(buf);
315}
316
317void StringBuilder::
318operator+=(unsigned long v)
319{
320 append(String::fromNumber(v));
321}
322
323void StringBuilder::
324operator+=(unsigned int v)
325{
326 append(String::fromNumber(v));
327}
328
329void StringBuilder::
330operator+=(double v)
331{
332 append(String::fromNumber(v));
333}
334
335void StringBuilder::
336operator+=(long double v)
337{
338 append(String::fromNumber(v));
339}
340
341void StringBuilder::
342operator+=(int v)
343{
344 append(String::fromNumber(v));
345}
346
347void StringBuilder::
348operator+=(long v)
349{
350 append(String::fromNumber(v));
351}
352
353void StringBuilder::
354operator+=(unsigned long long v)
355{
356 append(String::fromNumber(v));
357}
358
359void StringBuilder::
360operator+=(long long v)
361{
362 append(String::fromNumber(v));
363}
364
365void StringBuilder::
366operator+=(const APReal& v)
367{
368 append(String::fromNumber(v));
369}
370
371/*---------------------------------------------------------------------------*/
372/*---------------------------------------------------------------------------*/
373
375internalDump(std::ostream& ostr) const
376{
377 ostr << "StringDump(m_const_ptr=" << (void*)m_const_ptr << ",m_p=" << m_p;
378 if (m_p){
379 ostr << ",";
380 m_p->internalDump(ostr);
381 }
382 ostr << ")";
383}
384
385/*---------------------------------------------------------------------------*/
386/*---------------------------------------------------------------------------*/
387
388/*---------------------------------------------------------------------------*/
389/*---------------------------------------------------------------------------*/
390
391std::ostream&
392operator<<(std::ostream& o,const StringBuilder& str)
393{
394 String s = str.toString();
395 o << s.localstr();
396 return o;
397}
398
399/*---------------------------------------------------------------------------*/
400/*---------------------------------------------------------------------------*/
401
402bool
403operator==(const StringBuilder& a,const StringBuilder& b)
404{
405 return a.toString()==b.toString();
406}
407
408/*---------------------------------------------------------------------------*/
409/*---------------------------------------------------------------------------*/
410
411} // End namespace Arccore
412
413/*---------------------------------------------------------------------------*/
414/*---------------------------------------------------------------------------*/
415
Vue constante d'un tableau de type T.
Constructeur de chaîne de caractère unicode.
StringImpl * m_p
Implémentation de la classe.
StringBuilder & toUpper()
Transforme tous les caracteres de la chaine en majuscules.
String toString() const
Retourne la chaîne de caractères construite.
StringBuilder & collapseWhiteSpace()
Effectue une normalisation des caractères espaces. Le comportement est identique à replaceWhiteSpace(...
~StringBuilder()
Libère les ressources.
StringBuilder & replaceWhiteSpace()
Effectue une normalisation des caractères espaces. Tous les caractères espaces sont remplacés par des...
void internalDump(std::ostream &ostr) const
Affiche les infos internes de la classe.
StringBuilder clone() const
Clone cette chaîne.
const StringBuilder & operator=(const String &str)
Copie str dans cette instance.
StringBuilder & append(const String &str)
Ajoute str.
StringBuilder & toLower()
Transforme tous les caracteres de la chaine en minuscules.
Vue sur une chaîne de caractères UTF-8.
Definition StringView.h:47
Chaîne de caractères unicode.
StringImpl * m_p
Implémentation de la classe.
Int64 m_const_ptr_size
Longueur de la chaîne si constante (-1 sinon)
const char * localstr() const
Retourne la conversion de l'instance dans l'encodage UTF-8.
Definition String.cc:227
bool null() const
Retourne true si la chaîne est nulle.
Definition String.cc:304
Espace de nom de Arccore.
Definition ArcaneTypes.h:24
Int32 Integer
Type représentant un entier.