7#include <gtest/gtest.h>
9#include "arccore/base/String.h"
10#include "arccore/base/TraceInfo.h"
11#include "arccore/base/StringView.h"
13#include "arccore/base/CoreArray.h"
14#include "arccore/base/BasicTranscoder.h"
19#ifdef ARCCORE_OS_WIN32
33 explicit IosFlagsWrapper(std::ostream* o)
37 ~IosFlagsWrapper() { m_stream->flags(m_flags); }
41 std::ostream* m_stream;
42 std::ios_base::fmtflags m_flags;
45#ifdef ARCCORE_OS_WIN32
46void _testWStringWin32(
const char* str0)
48 std::wstring wideWhat;
49 int str0len = (int)std::strlen(str0);
50 int convertResult = MultiByteToWideChar(CP_UTF8, 0, str0, str0len, NULL, 0);
51 if (convertResult <= 0) {
52 wideWhat = L
"Exception occurred: Failure to convert its message text using MultiByteToWideChar: convertResult=";
55 wideWhat.resize(convertResult + 10);
56 convertResult = MultiByteToWideChar(CP_UTF8, 0, str0, str0len, wideWhat.data(), (
int)wideWhat.size());
57 if (convertResult <= 0) {
58 wideWhat = L
"Exception occurred: Failure to convert its message text using MultiByteToWideChar: convertResult=";
61 wideWhat.resize(convertResult);
65 std::cout <<
"STR0=" << str0 <<
" len=" << str0len <<
"\n";
66 std::cout <<
"convertResult=" << convertResult <<
"\n";
67 std::wcout <<
"WSTR0 len=" << wideWhat.length() <<
" v='" << wideWhat <<
"'\n ";
68 for (
int i = 0; i < convertResult; ++i)
69 std::wcout <<
"PRINT1 I=" << i <<
" V=" << wideWhat[i] <<
"\n";
71 for (
int i = 0; i < convertResult; ++i)
72 std::cout <<
"PRINT2 I=" << i <<
" V=" << std::hex <<
static_cast<int>(wideWhat[i]) <<
"\n";
76void _doConvertTest(
const char* name,
const String& str)
78 std::cout <<
"Name=" << name <<
"\n";
79 std::cout <<
"OrigUtf8 size=" << str.
utf8() <<
"\n";
84 BasicTranscoder::transcodeFromUtf8ToUtf16(utf8_orig_bytes, utf16_bytes);
86 std::cout <<
"OrigBytes=" << utf8_orig_bytes.constView() <<
"\n";
87 std::cout <<
"FinalTranscoderUtf16=" << utf16_bytes.
constView() <<
"\n";
88 std::cout <<
"FinalTranscoderUtf8=" << utf8_final_bytes.
constView() <<
"\n";
89 ASSERT_EQ(utf8_orig_bytes.constView(), utf8_final_bytes.
constView());
96 std::cout <<
"Utf16 bytes = " << utf16_bytes <<
"\n";
97 String str3(utf16_bytes.smallView());
98 std::cout <<
"ToUtf8 size=" << str3.bytes() <<
"\n";
109 IosFlagsWrapper io_wrapper(&std::cout);
111 String str1_direct =
"▲▼●■◆éà😀a😈";
112 std::cout <<
"STR1_UTF8=" << str1_direct <<
"\n";
114 std::array<Byte, 28> str1_bytes = { 0xE2, 0x96, 0xB2, 0xE2, 0x96, 0xBC, 0xE2, 0x97, 0x8F, 0xE2, 0x96, 0xA0, 0xE2, 0x97,
115 0x86, 0xC3, 0xA9, 0xC3, 0xA0, 0xF0, 0x9F, 0x98, 0x80, 0x61, 0xF0, 0x9F, 0x98, 0x88 };
117 String str1_orig(
"▲▼●■◆");
119#ifdef ARCCORE_OS_WIN32
120 _testWStringWin32(str1.localstr());
124 std::vector<UChar> big_endian_ref_vector{ 0x25b2, 0x25bc, 0x25cf, 0x25a0, 0x25c6, 0x00E9, 0x00E0, 0xD83D, 0xDE00, 0x0061, 0xD83D, 0xDE08 };
126 for (
int x : utf16_vector)
127 std::cout <<
"Utf16: " << std::hex << x <<
"\n";
128 ASSERT_EQ(big_endian_ref_vector, utf16_vector_direct);
129 ASSERT_EQ(big_endian_ref_vector, utf16_vector);
131 std::cout <<
"Utf16_size=" << utf16_bytes.smallView() <<
"\n";
133 std::cout <<
"BEFORE_CREATE_STR2\n";
134 String str2(utf16_bytes.smallView());
135 std::cout <<
"str1.utf16=" << str1.utf16() <<
"\n";
136 std::cout <<
"str2.utf16=" << str2.utf16() <<
"\n";
138 bool is_same = (str1 == str2);
140 std::cout <<
"is_same=" << is_same <<
"\n";
141 ASSERT_EQ(str1, str2);
143 std::cout <<
"str1.utf16=" << str1.utf16() <<
"\n";
144 std::cout <<
"str2.utf16=" << str2.utf16() <<
"\n";
147 ASSERT_EQ(str1.utf16().size(), 13);
148 ASSERT_EQ(str2.utf16().size(), 13);
150 ASSERT_EQ(str1.utf8().size(), str2.utf8().size());
151 ASSERT_EQ(str1.utf16().size(), str2.utf16().size());
156 ASSERT_EQ(b.
size(), 0);
158 ASSERT_EQ(u.
size(), 0);
164 ASSERT_EQ(b.
size(), 2);
166 ASSERT_EQ(u.
size(), 3);
171 std::array<Byte, 3> ref_a{ 0xe2, 0x82, 0xac };
174 ASSERT_EQ(b.
size(), 3);
175 ASSERT_EQ(b, ref_a_view);
177 ASSERT_EQ(u.
size(), 4);
179 for (
Integer i = 0; i < 3; ++i) {
180 ASSERT_EQ(u[i], ref_a[i]);
181 ASSERT_EQ(b[i], ref_a[i]);
186 _doConvertTest(
"X2", x2);
187 String x3 =
"\xe2\x82\xa1";
188 _doConvertTest(
"X3", x3);
189 String x4 =
"\xf0\x90\x8c\xbc";
190 _doConvertTest(
"X4", x4);
210 std::cout <<
"S2_8=" << s2 <<
'\n';
214 std::cout <<
"S2_9=" << s2 <<
'\n';
218 std::cout <<
"S2_10=" << s2 <<
'\n';
222 std::cout <<
"S2_11=" << s2 <<
'\n';
226 std::cout <<
"S2_12=" << s2 <<
'\n';
230 std::cout <<
"S2_13=" << s2 <<
'\n';
233 String g =
" \tceci \tcela ";
234 std::cout <<
" G= '" << g <<
"'" <<
'\n';
236 std::cout <<
" G2= '" << g2 <<
"'" <<
'\n';
238 std::cout <<
" G3= '" << g3 <<
"'" <<
'\n';
239 String expected_g3 =
" ceci cela ";
240 ASSERT_EQ(g3,expected_g3);
241 String expected_g2 =
"ceci cela";
242 ASSERT_EQ(g2,expected_g2);
246 std::cout <<
"GNULL2='" << gnull2 <<
"'" <<
'\n';
247 ASSERT_EQ(gnull2,
String());
251 std::cout <<
"GEMPTY2='" << gempty2 <<
"'" <<
'\n';
252 String expected_gempty2 =
"";
253 ASSERT_EQ(gempty2,expected_gempty2);
263 ASSERT_TRUE(kempty.contains(gnull)) <<
"Bad empty contains null";
264 ASSERT_TRUE(kempty.contains(gempty)) <<
"Bad empty contains null";
265 ASSERT_TRUE(k1.contains(gnull)) <<
"Bad null contains null";
266 ASSERT_TRUE(k1.contains(gempty)) <<
"Bad contains empty";
267 ASSERT_TRUE(k1.contains(k2)) <<
"Bad k1 contains k2";
272 String k0 =
":Toto::Titi:::Tata::::Tutu:Tete:";
275 std::cout <<
"ORIGINAL STRING TO STRING = '" << k0 <<
"'" <<
'\n';
276 std::vector<String> k0_list;
277 k0.
split(k0_list,
':');
278 for(
size_t i=0, n=k0_list.size(); i<n; ++i ){
279 std::cout <<
"K i=" << i <<
" v='" << k0_list[i] <<
"' is_null?=" << k0_list[i].null() <<
'\n';
281 ASSERT_EQ(k0_list[0],
String(
":Toto"));
282 ASSERT_EQ(k0_list[1],
String(
":Titi"));
283 ASSERT_EQ(k0_list[2],
String(
":"));
284 ASSERT_EQ(k0_list[3],
String(
"Tata"));
285 ASSERT_EQ(k0_list[4],
String(
":"));
286 ASSERT_EQ(k0_list[5],
String(
":Tutu"));
287 ASSERT_EQ(k0_list[6],
String(
"Tete"));
291TEST(
String, StdStringView)
293 const char* ref1 =
"S1éà";
294 const char* ref2 =
"ù*aXZáé";
296 const char* ref3 =
"S1éàù*aXZáé";
297 std::string std_ref3 { ref3 };
304 std::cout <<
"S1 '" << s1 <<
"'_SIZE=" << s1.
length() <<
'\n';
305 std::cout <<
"S2 '" << s2 <<
"'_SIZE=" << s2.
length() <<
'\n';
306 std::cout <<
"S3 '" << s3 <<
"'_SIZE=" << s3.
length() <<
'\n';
307 std::string_view vempty = sempty.toStdStringView();
308 ASSERT_EQ((Int64)vempty.size(),0) <<
"vempty.size()==0";
310 ASSERT_EQ((Int64)vnull.size(),0) <<
"vnull.size()==0";
312 ASSERT_EQ(v1,ref1) <<
"v1==ref1";
314 ASSERT_EQ(v2,ref2) <<
"v2==ref2";
316 ASSERT_EQ(v3,std_ref3) <<
"v3==ref3";
320 ASSERT_EQ(v4,v3) <<
"v4==v3";
324 ASSERT_EQ(v5,v4) <<
"v5==v4";
327 const char* t1 =
"testà1";
328 const char* t2 =
"testé2";
329 std::string st1 = t1;
330 std::string_view st1v = st1;
331 std::string st1_2 = st1 + t2;
337 ASSERT_EQ(s7,st1_2) <<
"s7==st1_2";
339 ASSERT_EQ(s8,st1_2) <<
"s8==st1_2";
341 ASSERT_EQ(s9,st1_2) <<
"s9==st1_2";
343 ASSERT_EQ(s10,st1_2) <<
"s10==st1_2";
#define ASSERT_FALSE(condition)
Vérifie que condition est faux.
#define ASSERT_TRUE(condition)
Vérifie que condition est vrai.
Fonctions utilitaires sur les chaînes de caractères.
ARCCORE_BASE_EXPORT std::vector< UChar > asUtf16BE(const String &str)
Retourne la conversion de l'instance dans l'encodage UTF-16BE.
static void transcodeFromUtf16ToUtf8(Span< const UChar > utf16, CoreArray< Byte > &utf8)
Traduit depuis UTF16 vers UTF8.
constexpr Integer size() const noexcept
Nombre d'éléments du tableau.
Tableau interne pour Arccore.
Span< const DataType > constView() const
Vue constante.
constexpr __host__ __device__ SizeType size() const noexcept
Retourne la taille du tableau.
Vue d'un tableau d'éléments de type T.
Chaîne de caractères unicode.
bool startsWith(const String &s) const
Indique si la chaîne commence par les caractères de s.
Int64 length() const
Retourne la longueur de la chaîne.
ByteConstArrayView utf8() const
Retourne la conversion de l'instance dans l'encodage UTF-8.
static String replaceWhiteSpace(const String &rhs)
Effectue une normalisation des caractères espaces.
Span< const Byte > bytes() const
Retourne la conversion de l'instance dans l'encodage UTF-8.
bool endsWith(const String &s) const
Indique si la chaîne se termine par les caractères de s.
void split(StringContainer &str_array, char c) const
Découpe la chaîne suivant le caractère c.
static String collapseWhiteSpace(const String &rhs)
Effectue une normalisation des caractères espaces.
bool contains(const String &s) const
Indique si la chaîne contient s.
std::string_view toStdStringView() const
Retourne une vue de la STL sur la chaîne actuelle.
String substring(Int64 pos) const
Sous-chaîne commençant à la position pos.
Int32 Integer
Type représentant un entier.
ConstArrayView< Byte > ByteConstArrayView
Equivalent C d'un tableau à une dimension de caractères.
Espace de nom de Arccore.