14#include "arcane/utils/ValueConvert.h"
16#include "arcane/utils/OStringStream.h"
17#include "arcane/utils/internal/ValueConvertInternal.h"
23#if defined(ARCANE_HAS_CXX20)
24#define ARCANE_USE_FROMCHARS
27#if defined(ARCANE_USE_FROMCHARS)
40 const char* _stringViewData(StringView s)
42 return reinterpret_cast<const char*
>(s.bytes().data());
50impl::StringViewInputStream::
51StringViewInputStream(StringView v)
56 char* begin_ptr =
const_cast<char*
>(
reinterpret_cast<const char*
>(b.data()));
57 char* end_ptr = begin_ptr + b.size();
58 setg(begin_ptr, begin_ptr, end_ptr);
66 Int32 global_value_convert_verbosity = 0;
67 bool global_use_from_chars =
true;
74arcaneSetIsValueConvertUseFromChars(
bool v)
76 global_use_from_chars = v;
80arcaneSetValueConvertVerbosity(
Int32 v)
82 global_value_convert_verbosity = v;
88#if defined(ARCANE_USE_FROMCHARS)
106 Int64 _getDoubleValue(
double& v, StringView s)
109 auto bytes = s.bytes();
110 Int64 size = bytes.size();
117 const char* orig_data =
reinterpret_cast<const char*
>(bytes.data());
118 const char* last_ptr =
nullptr;
119 std::chars_format fmt = std::chars_format::general;
120 const char* data = orig_data;
121 bool do_negatif =
false;
122 const bool is_verbose = global_value_convert_verbosity > 0;
129 if (size >= 3 && (bytes[0] ==
'-') && (bytes[1] ==
'0') && (bytes[2] ==
'x' || bytes[2] ==
'X')) {
130 fmt = std::chars_format::hex;
135 else if (size >= 2 && (bytes[0] ==
'0') && (bytes[1] ==
'x' || bytes[1] ==
'X')) {
136 fmt = std::chars_format::hex;
141 auto [ptr, ec] = std::from_chars(data, data + size, v, fmt);
144 std::cout <<
"FromChars:TRY GET_DOUBLE data=" << data <<
" v=" << v <<
" is_ok=" << (ec == std::errc()) <<
"\n";
145 if (ec != std::errc())
152 char* ptr2 =
nullptr;
153 double v2 = ::strtod(orig_data, &ptr2);
154 std::cout <<
"FromChars: COMPARE GET_DOUBLE via strtod v2=" << v2 <<
" pos=" << (ptr2 - orig_data) <<
"\n";
156 return (last_ptr - orig_data);
165template <> ARCANE_UTILS_EXPORT
bool
166builtInGetValue(
double& v, StringView s)
168#if defined(ARCANE_USE_FROMCHARS)
169 if (global_use_from_chars) {
170 Int64 p = _getDoubleValue(v, s);
171 return (p == (-1) || (p != s.size()));
175 const char* ptr = _stringViewData(s);
177 if (s ==
"infinity" || s ==
"inf") {
178 v = std::numeric_limits<double>::infinity();
182 char* ptr2 =
nullptr;
183 v = ::strtod(ptr, &ptr2);
184 return (ptr2 != (ptr + s.length()));
190template <> ARCANE_UTILS_EXPORT
bool
191builtInGetValue(BFloat16& v, StringView s)
194 bool r = builtInGetValue(z, s);
199template <> ARCANE_UTILS_EXPORT
bool
200builtInGetValue(Float16& v, StringView s)
203 bool r = builtInGetValue(z, s);
208template <> ARCANE_UTILS_EXPORT
bool
209builtInGetValue(Float128& v, StringView s)
215 bool r = builtInGetValue(z, s);
220template <> ARCANE_UTILS_EXPORT
bool
221builtInGetValue(
float& v, StringView s)
224 bool r = builtInGetValue(z, s);
229template <> ARCANE_UTILS_EXPORT
bool
230builtInGetValue(
long& v, StringView s)
232 const char* ptr = _stringViewData(s);
234 v = ::strtol(ptr, &ptr2, 0);
235 return (ptr2 != (ptr + s.length()));
238template <> ARCANE_UTILS_EXPORT
bool
239builtInGetValue(
int& v, StringView s)
242 bool r = builtInGetValue(z, s);
247template <> ARCANE_UTILS_EXPORT
bool
248builtInGetValue(
short& v, StringView s)
251 bool r = builtInGetValue(z, s);
256template <> ARCANE_UTILS_EXPORT
bool
257builtInGetValue(
unsigned long& v, StringView s)
259 const char* ptr = _stringViewData(s);
261 v = ::strtoul(ptr, &ptr2, 0);
262 return (ptr2 != (ptr + s.length()));
265template <> ARCANE_UTILS_EXPORT
bool
266builtInGetValue(
unsigned int& v, StringView s)
269 bool r = builtInGetValue(z, s);
274template <> ARCANE_UTILS_EXPORT
bool
275builtInGetValue(
unsigned short& v, StringView s)
278 bool r = builtInGetValue(z, s);
279 v = (
unsigned short)z;
283template <> ARCANE_UTILS_EXPORT
bool
284builtInGetValue(
long long& v, StringView s)
286 const char* ptr = _stringViewData(s);
288 v = ::strtoll(ptr, &ptr2, 0);
289 return (ptr2 != (ptr + s.length()));
292template <> ARCANE_UTILS_EXPORT
bool
293builtInGetValue(Real2& v, StringView s)
295 return impl::builtInGetValueGeneric(v, s);
297template <> ARCANE_UTILS_EXPORT
bool
298builtInGetValue(Real3& v, StringView s)
300 return impl::builtInGetValueGeneric(v, s);
303template <> ARCANE_UTILS_EXPORT
bool
304builtInGetValue(Real2x2& v, StringView s)
306 return impl::builtInGetValueGeneric(v, s);
309template <> ARCANE_UTILS_EXPORT
bool
310builtInGetValue(Real3x3& v, StringView s)
312 return impl::builtInGetValueGeneric(v, s);
315template <> ARCANE_UTILS_EXPORT
bool
316builtInGetValue(Int128& v, StringView s)
322 const char* ptr = _stringViewData(s);
324 v2 = ::strtoll(ptr, &ptr2, 0);
326 return (ptr2 != (ptr + s.length()));
329template <> ARCANE_UTILS_EXPORT
bool
330builtInGetValue(
unsigned long long& v, StringView s)
332 const char* ptr = _stringViewData(s);
334 v = ::strtoull(ptr, &ptr2, 0);
335 return (ptr2 != (ptr + s.length()));
341#ifdef ARCANE_REAL_NOT_BUILTIN
342template <> ARCANE_UTILS_EXPORT
bool
343builtInGetValue(
Real& v, StringView s)
347 if (builtInGetValue(vz,s))
350 cout <<
"** CONVERT DOUBLE TO REAL s=" << s <<
" vz=" << vz <<
" v=" << v <<
'\n';
354 if (builtInGetValue(vz, s))
356 v =
Real((
char*)s.localstr(), 1000);
357 cout <<
"** CONVERT DOUBLE TO REAL s=" << s <<
'\n';
366builtInGetValue(String& v, StringView s)
377 bool _builtInGetBoolArrayValue(BoolArray& v, StringView s)
386 if (builtInGetValue(sa, s))
388 for (Integer i = 0, is = sa.size(); i < is; ++i) {
389 bool read_val =
false;
390 if (builtInGetValue(read_val, sa[i]))
398 _builtInGetStringArrayValue(StringArray& v, StringView s)
401 String read_val = String();
402 impl::StringViewInputStream svis(s);
403 std::istream& sbuf = svis.stream();
404 while (!sbuf.eof()) {
411 read_val = StringView(s2.c_str());
418template <> ARCANE_UTILS_EXPORT
bool
419builtInGetValue(RealArray& v, StringView s)
421 return builtInGetArrayValue(v, s);
424template <> ARCANE_UTILS_EXPORT
bool
425builtInGetValue(Real2Array& v, StringView s)
427 return builtInGetArrayValue(v, s);
430template <> ARCANE_UTILS_EXPORT
bool
431builtInGetValue(Real3Array& v, StringView s)
433 return builtInGetArrayValue(v, s);
436template <> ARCANE_UTILS_EXPORT
bool
437builtInGetValue(Real2x2Array& v, StringView s)
439 return builtInGetArrayValue(v, s);
442template <> ARCANE_UTILS_EXPORT
bool
443builtInGetValue(Real3x3Array& v, StringView s)
445 return builtInGetArrayValue(v, s);
448template <> ARCANE_UTILS_EXPORT
bool
449builtInGetValue(Int8Array& v, StringView s)
451 return builtInGetArrayValue(v, s);
454template <> ARCANE_UTILS_EXPORT
bool
455builtInGetValue(Int16Array& v, StringView s)
457 return builtInGetArrayValue(v, s);
460template <> ARCANE_UTILS_EXPORT
bool
461builtInGetValue(Int32Array& v, StringView s)
463 return builtInGetArrayValue(v, s);
466template <> ARCANE_UTILS_EXPORT
bool
467builtInGetValue(Int64Array& v, StringView s)
469 return builtInGetArrayValue(v, s);
472template <> ARCANE_UTILS_EXPORT
bool
473builtInGetValue(BoolArray& v, StringView s)
475 return _builtInGetBoolArrayValue(v, s);
478template <> ARCANE_UTILS_EXPORT
bool
479builtInGetValue(StringArray& v, StringView s)
481 return _builtInGetStringArrayValue(v, s);
487template <> ARCANE_UTILS_EXPORT
bool
488builtInGetValue(RealUniqueArray& v, StringView s)
490 return builtInGetArrayValue(v, s);
493template <> ARCANE_UTILS_EXPORT
bool
494builtInGetValue(Real2UniqueArray& v, StringView s)
496 return builtInGetArrayValue(v, s);
499template <> ARCANE_UTILS_EXPORT
bool
500builtInGetValue(Real3UniqueArray& v, StringView s)
502 return builtInGetArrayValue(v, s);
505template <> ARCANE_UTILS_EXPORT
bool
506builtInGetValue(Real2x2UniqueArray& v, StringView s)
508 return builtInGetArrayValue(v, s);
511template <> ARCANE_UTILS_EXPORT
bool
512builtInGetValue(Real3x3UniqueArray& v, StringView s)
514 return builtInGetArrayValue(v, s);
517template <> ARCANE_UTILS_EXPORT
bool
518builtInGetValue(Int8UniqueArray& v, StringView s)
520 return builtInGetArrayValue(v, s);
523template <> ARCANE_UTILS_EXPORT
bool
524builtInGetValue(Int16UniqueArray& v, StringView s)
526 return builtInGetArrayValue(v, s);
529template <> ARCANE_UTILS_EXPORT
bool
530builtInGetValue(Int32UniqueArray& v, StringView s)
532 return builtInGetArrayValue(v, s);
535template <> ARCANE_UTILS_EXPORT
bool
536builtInGetValue(Int64UniqueArray& v, StringView s)
538 return builtInGetArrayValue(v, s);
541template <> ARCANE_UTILS_EXPORT
bool
542builtInGetValue(BoolUniqueArray& v, StringView s)
544 return _builtInGetBoolArrayValue(v, s);
547template <> ARCANE_UTILS_EXPORT
bool
548builtInGetValue(StringUniqueArray& v, StringView s)
550 return _builtInGetStringArrayValue(v, s);
556template <> ARCANE_UTILS_EXPORT
bool
557builtInGetValue(RealSharedArray& v, StringView s)
559 return builtInGetArrayValue(v, s);
562template <> ARCANE_UTILS_EXPORT
bool
563builtInGetValue(Real2SharedArray& v, StringView s)
565 return builtInGetArrayValue(v, s);
568template <> ARCANE_UTILS_EXPORT
bool
569builtInGetValue(Real3SharedArray& v, StringView s)
571 return builtInGetArrayValue(v, s);
574template <> ARCANE_UTILS_EXPORT
bool
575builtInGetValue(Real2x2SharedArray& v, StringView s)
577 return builtInGetArrayValue(v, s);
580template <> ARCANE_UTILS_EXPORT
bool
581builtInGetValue(Real3x3SharedArray& v, StringView s)
583 return builtInGetArrayValue(v, s);
586template <> ARCANE_UTILS_EXPORT
bool
587builtInGetValue(Int8SharedArray& v, StringView s)
589 return builtInGetArrayValue(v, s);
592template <> ARCANE_UTILS_EXPORT
bool
593builtInGetValue(Int16SharedArray& v, StringView s)
595 return builtInGetArrayValue(v, s);
598template <> ARCANE_UTILS_EXPORT
bool
599builtInGetValue(Int32SharedArray& v, StringView s)
601 return builtInGetArrayValue(v, s);
604template <> ARCANE_UTILS_EXPORT
bool
605builtInGetValue(Int64SharedArray& v, StringView s)
607 return builtInGetArrayValue(v, s);
610template <> ARCANE_UTILS_EXPORT
bool
611builtInGetValue(BoolSharedArray& v, StringView s)
613 return _builtInGetBoolArrayValue(v, s);
616template <> ARCANE_UTILS_EXPORT
bool
617builtInGetValue(StringSharedArray& v, StringView s)
619 return _builtInGetStringArrayValue(v, s);
627 template <
class T>
inline bool
628 _builtInPutValue(
const T& v, String& s)
632 if (ostr().fail() || ostr().bad())
637 template <
class T>
inline bool
638 _builtInPutArrayValue(Span<const T> v, String& s)
641 for (
Int64 i = 0, n = v.size(); i < n; ++i) {
646 if (ostr().fail() || ostr().bad())
660bool builtInPutValue(
double v, String& s)
662 return _builtInPutValue(v, s);
666 return _builtInPutValue(v, s);
670 return _builtInPutValue(v, s);
674 return _builtInPutValue(v, s);
678 return _builtInPutValue(v, s);
682 return _builtInPutValue(v, s);
686 return _builtInPutValue(v, s);
690 return _builtInPutValue(v, s);
694 return _builtInPutValue(v, s);
698 return _builtInPutValue(v, s);
700#ifdef ARCANE_REAL_NOT_BUILTIN
703 return _builtInPutValue(v, s);
708 return _builtInPutValue(v, s);
712 return _builtInPutValue(v, s);
716 return _builtInPutValue(v, s);
720 return _builtInPutValue(v, s);
725 return _builtInPutArrayValue(v, s);
729 return _builtInPutArrayValue(v, s);
733 return _builtInPutArrayValue(v, s);
737 return _builtInPutArrayValue(v, s);
741 return _builtInPutArrayValue(v, s);
745 return _builtInPutArrayValue(v, s);
749 return _builtInPutArrayValue(v, s);
753 return _builtInPutArrayValue(v, s);
757 return _builtInPutArrayValue(v, s);
761 return _builtInPutArrayValue(v, s);
Lecteur des fichiers de maillage via la bibliothèque LIMA.
void add(ConstReferenceType val)
Ajoute l'élément val à la fin du tableau.
Chaîne de caractères unicode.
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
bool builtInPutValue(const String &v, String &s)
Converti la valeur v dans la chaîne s.
UniqueArray< String > StringUniqueArray
Tableau dynamique à une dimension de chaînes de caractères.