@@ -28,6 +28,8 @@ extern "C" {
2828 extern char *strchr (const char *s, int c);
2929 extern wchar_t *wmemchr (const wchar_t *s, wchar_t c, size_t n);
3030 extern wchar_t *wcschr (const wchar_t *s, wchar_t c);
31+ extern int wcscmp (const wchar_t *s1, const wchar_t *s2);
32+ extern int wcsncmp (const wchar_t *s1, const wchar_t *s2, size_t n);
3133}
3234
3335namespace strcmp {
@@ -72,6 +74,50 @@ namespace strcmp {
7274 static_assert (__builtin_strncmp(" abab\0 banana" , " abab\0 canada" , 100 ) == 0 );
7375}
7476
77+ namespace WcsCmp {
78+ constexpr wchar_t kFoobar [6 ] = {L' f' ,L' o' ,L' o' ,L' b' ,L' a' ,L' r' };
79+ constexpr wchar_t kFoobazfoobar [12 ] = {L' f' ,L' o' ,L' o' ,L' b' ,L' a' ,L' z' ,L' f' ,L' o' ,L' o' ,L' b' ,L' a' ,L' r' };
80+
81+ static_assert (__builtin_wcscmp(L" abab" , L" abab" ) == 0 );
82+ static_assert (__builtin_wcscmp(L" abab" , L" abba" ) == -1 );
83+ static_assert (__builtin_wcscmp(L" abab" , L" abaa" ) == 1 );
84+ static_assert (__builtin_wcscmp(L" ababa" , L" abab" ) == 1 );
85+ static_assert (__builtin_wcscmp(L" abab" , L" ababa" ) == -1 );
86+ static_assert (__builtin_wcscmp(L" abab\0 banana" , L" abab" ) == 0 );
87+ static_assert (__builtin_wcscmp(L" abab" , L" abab\0 banana" ) == 0 );
88+ static_assert (__builtin_wcscmp(L" abab\0 banana" , L" abab\0 canada" ) == 0 );
89+ #if __WCHAR_WIDTH__ == 32
90+ static_assert (__builtin_wcscmp(L" a\x83838383 " , L" a" ) == (wchar_t )-1U >> 31);
91+ #endif
92+ static_assert (__builtin_wcscmp(0 , L" abab" ) == 0 ); // both-error {{not an integral constant}} \
93+ // both-note {{dereferenced null}}
94+ static_assert (__builtin_wcscmp(L" abab" , 0 ) == 0 ); // both-error {{not an integral constant}} \
95+ // both-note {{dereferenced null}}
96+
97+ static_assert (__builtin_wcscmp(kFoobar , kFoobazfoobar ) == -1 );
98+ static_assert (__builtin_wcscmp(kFoobar , kFoobazfoobar + 6 ) == 0 ); // both-error {{not an integral constant}} \
99+ // both-note {{dereferenced one-past-the-end}}
100+
101+ static_assert (__builtin_wcsncmp(L" abaa" , L" abba" , 5 ) == -1 );
102+ static_assert (__builtin_wcsncmp(L" abaa" , L" abba" , 4 ) == -1 );
103+ static_assert (__builtin_wcsncmp(L" abaa" , L" abba" , 3 ) == -1 );
104+ static_assert (__builtin_wcsncmp(L" abaa" , L" abba" , 2 ) == 0 );
105+ static_assert (__builtin_wcsncmp(L" abaa" , L" abba" , 1 ) == 0 );
106+ static_assert (__builtin_wcsncmp(L" abaa" , L" abba" , 0 ) == 0 );
107+ static_assert (__builtin_wcsncmp(0 , 0 , 0 ) == 0 );
108+ static_assert (__builtin_wcsncmp(L" abab\0 banana" , L" abab\0 canada" , 100 ) == 0 );
109+ #if __WCHAR_WIDTH__ == 32
110+ static_assert (__builtin_wcsncmp(L" a\x83838383 " , L" aa" , 2 ) ==
111+ (wchar_t )-1U >> 31);
112+ #endif
113+
114+ static_assert (__builtin_wcsncmp(kFoobar , kFoobazfoobar , 6 ) == -1 );
115+ static_assert (__builtin_wcsncmp(kFoobar , kFoobazfoobar , 7 ) == -1 );
116+ static_assert (__builtin_wcsncmp(kFoobar , kFoobazfoobar + 6 , 6 ) == 0 );
117+ static_assert (__builtin_wcsncmp(kFoobar , kFoobazfoobar + 6 , 7 ) == 0 ); // both-error {{not an integral constant}} \
118+ // both-note {{dereferenced one-past-the-end}}
119+ }
120+
75121// / Copied from constant-expression-cxx11.cpp
76122namespace strlen {
77123constexpr const char *a = " foo\0 quux" ;
0 commit comments