<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/72411>72411</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [libc++] collate_bytename<charT>::compare function bug in libc++
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          reporterfix
      </td>
    </tr>
</table>

<pre>
    **- Bug Description:**
   * At the line 9 and 10,  auto& f = std::use_facet<std::collate<char>>(l); and auto& g = std::use_facet<std::collate<wchar_t>>(l);  facet is created as object type of [collate_byname<char>](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/include/__locale#L327) and [collate_byname<wchar_t>](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/include/__locale#L346) by call of  global function of [use_facet](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/include/__locale#L232).
 * At the line 12 and 15,  int res = f.compare( ... ) [compare](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/include/__locale#L340) function of f and g (object type collate_byname class) which is a virtual function is called.
   * If we look into the implementation of compare function, it is inherited from collate<charT> class and it calls do_compare function.
   * In implementation of[ collate_byname<char>::do_compare ](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/src/locale.cpp#L700)and collate_byname<wchar_t>::do_compare, the variable r is generated with un-expected result which the return value of strcoll_l and wcscoll_l function.
   * Using [collate_byname<char>::do_compare](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/src/locale.cpp#L700) or [collate_byname<wchar_t>::do_compare](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/src/locale.cpp#L748C1-L748C36) should avoid considering "\0" as end of string character but [strcoll_l ](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/src/locale.cpp#L705C1-L705C54)and[ wcscoll_l](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/src/locale.cpp#L753C13-L753C22) functions consider it as end of string character.
   * These functions ( [collate_byname<char>::do_compare](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/src/locale.cpp#L700) or [collate_byname<wchar_t>::do_compare](https://github.com/llvm/llvm-project/blob/19a71f6bdf2dddb10764939e7f0ec2b98dba76c9/libcxx/src/locale.cpp#L748C1-L748C36) ) generate the expected output using standard library c++ (stdlibc++).

**- Test Case:**
```c++
1. #include <locale>
2. #include <iostream>
3. #include <iomanip>
4. 
5. int main() {
6. int res = 0;
7.      std::locale l("C");
8.
9.      auto& f = std::use_facet<std::collate<char>>(l);
10.     auto& g = std::use_facet<std::collate<wchar_t>>(l);
11. 
12.     res = f.compare("abcd\0efghl", (char*) "abcd\0efghl" + 10, "abcd\0efghl\0qwert", (char*) "abcd\0efghl\0qwert" + 16);
13. std::cout << std::boolalpha << (res == -1) << "\n";
14.  
15. res = g.compare(L"abcd\0efghl", (wchar_t*)L"abcd\0efghl" + 10, L"abcd\0efghl\0qwert", (wchar_t*)L"abcd\0efghl\0qwert" + 16);
16. std::cout << std::boolalpha << (res == -1) << "\n";
17. 
18. return 0;
19. }
``` 
=> test case link with libc++ 17.0.1 : https://godbolt.org/z/dna3K7MW9
=> test case link with libstdc++ 17.0.1 : https://godbolt.org/z/svdd76qn6


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWE2T2jgT_jXi0jUuS_IHPnAAM1S99WZvs7VHSrZkrI2wiCTPx_76LckGDEwyyVZ2l8OmSBz00Xr66e5HbZi1ctcJsUD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