Andrew Scull | 1883487 | 2018-10-12 11:48:09 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google LLC |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Scull | 18c78fc | 2018-08-20 12:57:41 +0100 | [diff] [blame] | 17 | #include "hf/std.h" |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 18 | |
| 19 | void *memset(void *s, int c, size_t n) |
| 20 | { |
| 21 | char *p = (char *)s; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 22 | while (n--) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 23 | *p++ = c; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 24 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 25 | return s; |
| 26 | } |
| 27 | |
| 28 | /* |
| 29 | * Calculates the length of the provided null-terminated string. |
| 30 | */ |
| 31 | size_t strlen(const char *str) |
| 32 | { |
| 33 | const char *p = str; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 34 | while (*p) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 35 | p++; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 36 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 37 | return p - str; |
| 38 | } |
| 39 | |
| 40 | void *memcpy(void *dst, const void *src, size_t n) |
| 41 | { |
| 42 | char *x = dst; |
| 43 | const char *y = src; |
| 44 | |
| 45 | while (n--) { |
| 46 | *x = *y; |
| 47 | x++; |
| 48 | y++; |
| 49 | } |
| 50 | |
| 51 | return dst; |
| 52 | } |
| 53 | |
| 54 | void *memmove(void *dst, const void *src, size_t n) |
| 55 | { |
| 56 | char *x; |
| 57 | const char *y; |
| 58 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 59 | if (dst < src) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 60 | return memcpy(dst, src, n); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 61 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 62 | |
| 63 | x = (char *)dst + n - 1; |
| 64 | y = (const char *)src + n - 1; |
| 65 | |
| 66 | while (n--) { |
| 67 | *x = *y; |
| 68 | x--; |
| 69 | y--; |
| 70 | } |
| 71 | |
| 72 | return dst; |
| 73 | } |
| 74 | |
| 75 | int memcmp(const void *a, const void *b, size_t n) |
| 76 | { |
| 77 | const char *x = a; |
| 78 | const char *y = b; |
| 79 | |
| 80 | while (n--) { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 81 | if (*x != *y) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 82 | return *x - *y; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 83 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 84 | x++; |
| 85 | y++; |
| 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | int strcmp(const char *a, const char *b) |
| 92 | { |
| 93 | const char *x = a; |
| 94 | const char *y = b; |
| 95 | |
| 96 | while (*x != 0 && *y != 0) { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 97 | if (*x != *y) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 98 | return *x - *y; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 99 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 100 | x++; |
| 101 | y++; |
| 102 | } |
| 103 | |
| 104 | return *x - *y; |
| 105 | } |