| 1 | //===-- Implementation of wcsncmp -----------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "src/wchar/wcsncmp.h" |
| 10 | |
| 11 | #include "hdr/types/size_t.h" |
| 12 | #include "hdr/types/wchar_t.h" |
| 13 | #include "src/__support/common.h" |
| 14 | #include "src/__support/macros/null_check.h" |
| 15 | |
| 16 | namespace LIBC_NAMESPACE_DECL { |
| 17 | |
| 18 | LLVM_LIBC_FUNCTION(int, wcsncmp, |
| 19 | (const wchar_t *left, const wchar_t *right, size_t n)) { |
| 20 | LIBC_CRASH_ON_NULLPTR(left); |
| 21 | LIBC_CRASH_ON_NULLPTR(right); |
| 22 | |
| 23 | if (n == 0) |
| 24 | return 0; |
| 25 | |
| 26 | auto comp = [](wchar_t l, wchar_t r) -> int { return l - r; }; |
| 27 | |
| 28 | for (; n > 1; --n, ++left, ++right) { |
| 29 | wchar_t lc = *left; |
| 30 | if (!comp(lc, '\0') || comp(lc, *right)) |
| 31 | break; |
| 32 | } |
| 33 | return comp(*reinterpret_cast<const wchar_t *>(left), |
| 34 | *reinterpret_cast<const wchar_t *>(right)); |
| 35 | } |
| 36 | |
| 37 | } // namespace LIBC_NAMESPACE_DECL |
| 38 | |