1// Copyright 2016 The Fuchsia Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <string.h> 6 7#include <stdint.h> 8 9void* memset(void* _dst, int c, size_t n) { 10 uint8_t* dst = _dst; 11 while (n-- > 0) { 12 *dst++ = c; 13 } 14 return _dst; 15} 16 17void* memcpy(void* _dst, const void* _src, size_t n) { 18 uint8_t* dst = _dst; 19 const uint8_t* src = _src; 20 while (n-- > 0) { 21 *dst++ = *src++; 22 } 23 return _dst; 24} 25 26int memcmp(const void* _a, const void* _b, size_t n) { 27 const uint8_t* a = _a; 28 const uint8_t* b = _b; 29 while (n-- > 0) { 30 int x = *a++ - *b++; 31 if (x != 0) { 32 return x; 33 } 34 } 35 return 0; 36} 37 38size_t strlen(const char* s) { 39 size_t len = 0; 40 while (*s++) 41 len++; 42 return len; 43} 44 45size_t strnlen(const char* s, size_t max) { 46 size_t len = 0; 47 while (len < max && *s++) 48 len++; 49 return len; 50} 51 52char* strchr(const char* s, int c) { 53 while (*s != c && *s++) ; 54 if (*s != c) return 0; 55 return (char*)s; 56} 57 58char* strcpy(char* dst, const char* src) { 59 while (*src != 0) { 60 *dst++ = *src++; 61 } 62 return dst; 63} 64 65char* strncpy(char* dst, const char* src, size_t len) { 66 while (len-- > 0 && *src != 0) { 67 *dst++ = *src++; 68 } 69 return dst; 70} 71 72int strcmp(const char* s1, const char* s2) { 73 while (*s1 && (*s1 == *s2)) { 74 s1++; 75 s2++; 76 } 77 return *s1 - *s2; 78} 79 80int strncmp(const char* s1, const char* s2, size_t len) { 81 while (len-- > 0) { 82 int diff = *s1 - *s2; 83 if (diff != 0 || *s1 == '\0') { 84 return diff; 85 } 86 s1++; 87 s2++; 88 } 89 return 0; 90} 91