1 //===-- Implementation of strncmp -----------------------------------------===// 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/string/strncmp.h" 10 11 #include "src/__support/common.h" 12 #include <stddef.h> 13 14 namespace __llvm_libc { 15 16 // TODO: Look at benefits for comparing words at a time. 17 LLVM_LIBC_FUNCTION(int, strncmp, 18 (const char *left, const char *right, size_t n)) { 19 20 if (n == 0) 21 return 0; 22 23 for (; n > 1; --n, ++left, ++right) { 24 char lc = *left; 25 if (lc == '\0' || lc != *right) 26 break; 27 } 28 return *reinterpret_cast<const unsigned char *>(left) - 29 *reinterpret_cast<const unsigned char *>(right); 30 } 31 32 } // namespace __llvm_libc 33