1*c6d03b58SMichael Jones //===-- Implementation of strncmp -----------------------------------------===//
2*c6d03b58SMichael Jones //
3*c6d03b58SMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*c6d03b58SMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5*c6d03b58SMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*c6d03b58SMichael Jones //
7*c6d03b58SMichael Jones //===----------------------------------------------------------------------===//
8*c6d03b58SMichael Jones 
9*c6d03b58SMichael Jones #include "src/string/strncmp.h"
10*c6d03b58SMichael Jones 
11*c6d03b58SMichael Jones #include "src/__support/common.h"
12*c6d03b58SMichael Jones #include <stddef.h>
13*c6d03b58SMichael Jones 
14*c6d03b58SMichael Jones namespace __llvm_libc {
15*c6d03b58SMichael Jones 
16*c6d03b58SMichael Jones // TODO: Look at benefits for comparing words at a time.
17*c6d03b58SMichael Jones LLVM_LIBC_FUNCTION(int, strncmp,
18*c6d03b58SMichael Jones                    (const char *left, const char *right, size_t n)) {
19*c6d03b58SMichael Jones 
20*c6d03b58SMichael Jones   if (n == 0)
21*c6d03b58SMichael Jones     return 0;
22*c6d03b58SMichael Jones 
23*c6d03b58SMichael Jones   for (; n > 1; --n, ++left, ++right) {
24*c6d03b58SMichael Jones     char lc = *left;
25*c6d03b58SMichael Jones     if (lc == '\0' || lc != *right)
26*c6d03b58SMichael Jones       break;
27*c6d03b58SMichael Jones   }
28*c6d03b58SMichael Jones   return *reinterpret_cast<const unsigned char *>(left) -
29*c6d03b58SMichael Jones          *reinterpret_cast<const unsigned char *>(right);
30*c6d03b58SMichael Jones }
31*c6d03b58SMichael Jones 
32*c6d03b58SMichael Jones } // namespace __llvm_libc
33