14#include "arccore/base/String.h"
15#include "arccore/base/StringBuilder.h"
16#include "arccore/base/CStringUtils.h"
17#include "arccore/base/APReal.h"
18#include "arccore/base/TraceInfo.h"
19#include "arccore/base/FatalErrorException.h"
20#include "arccore/base/NotSupportedException.h"
21#include "arccore/base/StringView.h"
24#include "arccore/base/internal/StringImpl.h"
31#define A_FASTLOCK(ptr)
49String(
const std::string& str)
50: m_p(new StringImpl(str))
60String(std::string_view str)
61: m_p(new StringImpl(str))
84: m_p(new StringImpl(ustr))
95: m_p(new StringImpl(ustr))
107, m_const_ptr_size(-1)
118: m_p(new StringImpl(std::string_view(str,
len)))
119, m_const_ptr_size(-1)
129: m_p(new StringImpl(str))
130, m_const_ptr_size(-1)
139String(
const char* str,
bool do_alloc)
141, m_const_ptr(nullptr)
142, m_const_ptr_size(-1)
145 m_p =
new StringImpl(str);
151 m_const_ptr_size = std::strlen(str);
161, m_const_ptr(str.m_const_ptr)
162, m_const_ptr_size(str.m_const_ptr_size)
175 str.m_p->addReference();
176 _removeReferenceIfNeeded();
187 _removeReferenceIfNeeded();
234 return m_p->toStdStringView().data();
242_removeImplReference()
244 m_p->removeReference();
250ConstArrayView<UChar> String::
251_internalUtf16BE()
const
261 return ConstArrayView<UChar>();
270 return _internalUtf16BE();
280 return m_p->largeUtf8().smallView();
282 Int64 ts = m_const_ptr_size + 1;
320 return m_const_ptr[0] ==
'\0';
354 Int64 xlen = std::strlen(m_const_ptr);
355 if (xlen != m_const_ptr_size)
356 ARCCORE_FATAL(
"Bad length (computed={0} stored={1})", xlen, m_const_ptr_size);
358 return _viewFromConstChar();
361 return m_p->toStdStringView();
362 return std::string_view();
392 if (m_const_ptr || !m_p) {
393 std::string_view sview;
395 sview = _viewFromConstChar();
396 m_p =
new StringImpl(sview);
398 m_const_ptr =
nullptr;
399 m_const_ptr_size = -1;
402 if (m_p->nbReference() != 1) {
403 StringImpl* old_p = m_p;
406 old_p->removeReference();
417 return String(m_p->clone());
430 if (str.m_const_ptr) {
431 m_p = m_p->append(str._viewFromConstChar());
434 m_p = m_p->append(str.m_p);
446 s2.m_p = s2.m_p->replaceWhiteSpace();
460 s2.m_p = s2.m_p->collapseWhiteSpace();
472 s2.m_p = s2.m_p->toUpper();
484 s2.m_p = s2.m_p->toLower();
497 return s2._append(str);
509 return s2._append(str);
519 return s2._append(str);
526fromNumber(
unsigned long v)
532fromNumber(
unsigned int v)
534 return StringFormatterArg(v).value();
540 return StringFormatterArg(v).value();
544fromNumber(
long double v)
546 return StringFormatterArg(v).value();
552 return StringFormatterArg(v).value();
558 return StringFormatterArg(v).value();
562fromNumber(
unsigned long long v)
564 return StringFormatterArg(v).value();
568fromNumber(
long long v)
570 return StringFormatterArg(v).value();
574fromNumber(
const APReal& v)
576 return StringFormatterArg(v).value();
580fromNumber(
double v,
Integer nb_digit_after_point)
583 for (
Integer z = 0; z < nb_digit_after_point; ++z)
586 Int64 after_digit = p % mulp;
587 Int64 before_digit = p / mulp;
588 StringBuilder s(String::fromNumber(before_digit) +
".");
591 Int64 mv = mulp / 10;
592 for (
Integer i = 0; i < (nb_digit_after_point - 1); ++i) {
593 if (after_digit >= mv) {
599 for (
Integer i = 0; i < nb_zero; ++i)
602 s += String::fromNumber(after_digit);
613 return s2._append(String::fromNumber(v));
620 return s2._append(String::fromNumber(v));
627 return s2._append(String::fromNumber(v));
634 return s2._append(String::fromNumber(v));
641 return s2._append(String::fromNumber(v));
648 return s2._append(String::fromNumber(v));
655 return s2._append(String::fromNumber(v));
662 return s2._append(String::fromNumber(v));
669 return s2._append(String::fromNumber(v));
676isLess(
const String& b)
const
682 if (b.m_p->isEqual(m_const_ptr))
684 return !(b.m_p->isLessThan(m_const_ptr));
692 return m_p->isLessThan(b.m_const_ptr);
699 return m_p->isLessThan(b.m_p);
705 return b.m_p ==
nullptr;
723 ostr <<
"StringDump(m_const_ptr=" << (
void*)m_const_ptr <<
",m_p=" << m_p;
726 m_p->internalDump(ostr);
740 for (
Int64 i = 0; i < n; ++i) {
741 h = (h << 5) - h + s[i];
749void StringFormatterArg::
750_formatReal(
Real avalue)
752 std::ostringstream ostr;
753 ostr.precision(std::numeric_limits<Real>::digits10);
755 m_str_value = ostr.str();
765 StringFormatter(
const String& format)
772 void addArg(
const String& ostr)
781 std::cerr <<
"Too many args (maximum is 100)";
786 buf[0] = (char)(
'0' + (z / 10));
787 buf[1] = (char)(
'0' + (z % 10));
791 buf[0] = (char)(
'0' + z);
797 std::string str = m_format.
localstr();
798 const char* local_str = str.c_str();
800 const Int64 slen = str.length();
801 for (
Int64 i = 0; i < slen; ++i) {
802 if (local_str[i] ==
'{') {
803 if (i + nb_z >= slen)
806 for (
Integer j = 0; j < nb_z; ++j)
807 if (local_str[i + 1 + j] != buf[j]) {
812 std::string str1(local_str, local_str + i);
813 std::string str2(local_str + i + 1 + nb_z);
814 m_format =
String(str1) + ostr + str2;
825 const String& value()
const {
return m_format; }
848format(
const String& str,
const StringFormatterArg& arg1)
850 StringFormatter sf(str);
851 sf.addArg(arg1.value());
862 StringFormatter sf(str);
863 sf.addArg(arg1.value());
864 sf.addArg(arg2.value());
876 StringFormatter sf(str);
877 sf.addArg(arg1.value());
878 sf.addArg(arg2.value());
879 sf.addArg(arg3.value());
892 StringFormatter sf(str);
893 sf.addArg(arg1.value());
894 sf.addArg(arg2.value());
895 sf.addArg(arg3.value());
896 sf.addArg(arg4.value());
910 StringFormatter sf(str);
911 sf.addArg(arg1.value());
912 sf.addArg(arg2.value());
913 sf.addArg(arg3.value());
914 sf.addArg(arg4.value());
915 sf.addArg(arg5.value());
930 StringFormatter sf(str);
931 sf.addArg(arg1.value());
932 sf.addArg(arg2.value());
933 sf.addArg(arg3.value());
934 sf.addArg(arg4.value());
935 sf.addArg(arg5.value());
936 sf.addArg(arg6.value());
952 StringFormatter sf(str);
953 sf.addArg(arg1.value());
954 sf.addArg(arg2.value());
955 sf.addArg(arg3.value());
956 sf.addArg(arg4.value());
957 sf.addArg(arg5.value());
958 sf.addArg(arg6.value());
959 sf.addArg(arg7.value());
976 StringFormatter sf(str);
977 sf.addArg(arg1.value());
978 sf.addArg(arg2.value());
979 sf.addArg(arg3.value());
980 sf.addArg(arg4.value());
981 sf.addArg(arg5.value());
982 sf.addArg(arg6.value());
983 sf.addArg(arg7.value());
984 sf.addArg(arg8.value());
1002 StringFormatter sf(str);
1003 sf.addArg(arg1.value());
1004 sf.addArg(arg2.value());
1005 sf.addArg(arg3.value());
1006 sf.addArg(arg4.value());
1007 sf.addArg(arg5.value());
1008 sf.addArg(arg6.value());
1009 sf.addArg(arg7.value());
1010 sf.addArg(arg8.value());
1011 sf.addArg(arg9.value());
1021 return arg1.value();
1028 return arg1.value() + arg2.value();
1036 return arg1.value() + arg2.value() + arg3.value();
1045 return arg1.value() + arg2.value() + arg3.value() + arg4.value();
1062 return (a > 0) ? a : (-a);
1070 return String::concat(n,
" ", ((_abs(n) > 1) ? str2 : str));
1072 return ((_abs(n) > 1) ? str2 : str);
1088 return a.find(b) != std::string_view::npos;
1101 if (ref_size > v_size)
1103 const Byte* v_begin = &v[v_size - ref_size];
1104 return std::memcmp(v_begin, ref.
data(), ref_size) == 0;
1117 if (ref_size > v_size)
1119 return memcmp(v.
data(), ref.
data(), ref_size) == 0;
1141 String s2(StringImpl::substring(m_p, pos,
len));
1152 for (
Integer i = 0, n = strs.
size(); i < n; ++i) {
1167 if (a.m_const_ptr) {
1171 return b.m_p->isEqual(a.m_const_ptr);
1176 if (b.m_const_ptr) {
1178 return a.m_p->isEqual(b.m_const_ptr);
1183 return a.m_p->isEqual(b.m_p);
1189 return b.m_p ==
nullptr;
1207 bool v = a.isLess(b);
1229 bool global_write_utf8 =
false;
1235 if (global_write_utf8)
1251 o.write((
const char*)v.
data(), vlen);
1273 static std::vector<UChar>
1274 toUtf16BE(
const String& str)
1289std::vector<UChar> StringUtils::
1290asUtf16BE(
const String& str)
1292 return StringUtilsImpl::toUtf16BE(str);
1298std::wstring StringUtils::
1299convertToStdWString([[maybe_unused]]
const String& str)
1301#ifdef ARCCORE_OS_WIN32
1303 const wchar_t* wdata =
reinterpret_cast<const wchar_t*
>(utf16.
data());
1304 const size_t wlen = utf16.
size();
1305 std::wstring_view wstr_view(wdata, wlen);
1306 return std::wstring(wstr_view);
1316convertToArcaneString([[maybe_unused]]
const std::wstring_view& wstr)
1318#ifdef ARCCORE_OS_WIN32
1319 const UChar* ux =
reinterpret_cast<const UChar*
>(wstr.data());
#define ARCCORE_FATAL(...)
Macro throwing a FatalErrorException.
#define ARCCORE_THROW(exception_class,...)
Macro to throw an exception with formatting.
Utility functions for character strings.
Emulation of real number in arbitrary precision.
Constant view of an array of type T.
constexpr const_pointer data() const noexcept
Pointer to the allocated memory.
constexpr const_iterator begin() const noexcept
Iterator over the first element of the array.
constexpr Integer size() const noexcept
Number of elements in the array.
constexpr __host__ __device__ pointer data() const noexcept
Pointer to the start of the view.
constexpr __host__ __device__ SizeType size() const noexcept
Returns the size of the array.
View of an array of elements of type T.
Unicode character string constructor.
String toString() const
Returns the constructed character string.
View of a UTF-8 character string.
Unicode character string.
bool startsWith(const String &s) const
Indicates if the string starts with the characters of s.
bool null() const
Returns true if the string is null.
static String plural(const Integer n, const String &str, const bool with_number=true)
Standard plural form by adding an 's'.
String upper() const
Transforms all characters in the string to uppercase.
Int64 length() const
Returns the length of the string.
String clone() const
Clones this string.
String lower() const
Transforms all characters in the string to lowercase.
void writeBytes(std::ostream &o) const
Writes the string in UTF-8 format to the stream o.
void internalDump(std::ostream &ostr) const
Displays the internal information of the class.
const char * localstr() const
Returns the conversion of the instance into UTF-8 encoding.
ByteConstArrayView utf8() const
Returns the conversion of the instance into UTF-8 encoding.
friend bool operator==(const String &a, const String &b)
Compares two unicode strings.
friend String operator+(const char *a, const String &b)
Adds two strings.
String()
Creates a null string.
Int32 hashCode() const
Calculates a hash value for this character string.
static String replaceWhiteSpace(const String &rhs)
Performs whitespace character normalization.
Span< const Byte > bytes() const
Returns the conversion of the instance into UTF-8 encoding.
bool endsWith(const String &s) const
Indicates if the string ends with the characters of s.
bool empty() const
True if the string is empty (null or "").
static String collapseWhiteSpace(const String &rhs)
Performs whitespace character normalization.
ConstArrayView< UChar > utf16() const
Returns the conversion of the instance into UTF-16 encoding.
StringView view() const
Returns a view of the current string.
String & operator=(const String &str)
Copies str into this instance.
bool contains(const String &s) const
Indicates if the string contains s.
std::string_view toStdStringView() const
Returns an STL view of the current string.
Integer len() const
Returns the length of the string in 32 bits.
String substring(Int64 pos) const
Substring starting at position pos.
bool isLess(const char *s1, const char *s2)
Returns true if s1 is less than (alphabetical order) s2 , false otherwise.
bool isEqual(const char *s1, const char *s2)
Returns true if s1 and s2 are identical, false otherwise.
-- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature --
constexpr __host__ __device__ Integer arccoreCheckArraySize(unsigned long long size)
Checks that size can be converted into an 'Integer' to serve as an array size. If possible,...
std::int64_t Int64
Signed integer type of 64 bits.
Int32 Integer
Type representing an integer.
bool operator<(const Item &item1, const Item &item2)
Compare two entities.
std::istream & operator>>(std::istream &istr, eItemKind &item_kind)
Input operator from a stream.
double Real
Type representing a real number.
ConstArrayView< UChar > UCharConstArrayView
C equivalent of a 1D array of unicode characters.
ConstArrayView< Byte > ByteConstArrayView
C equivalent of a 1D array of characters.
unsigned char Byte
Type of a byte.
unsigned short UChar
Type of a unicode character.
std::ostream & operator<<(std::ostream &ostr, eItemKind item_kind)
Output operator for a stream.
std::int32_t Int32
Signed integer type of 32 bits.