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 | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame^] | 22 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 23 | while (n--) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 24 | *p++ = c; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 25 | } |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame^] | 26 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 27 | return s; |
| 28 | } |
| 29 | |
| 30 | /* |
| 31 | * Calculates the length of the provided null-terminated string. |
| 32 | */ |
| 33 | size_t strlen(const char *str) |
| 34 | { |
| 35 | const char *p = str; |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame^] | 36 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 37 | while (*p) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 38 | p++; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 39 | } |
Andrew Scull | cbefbdb | 2019-01-11 16:36:26 +0000 | [diff] [blame^] | 40 | |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 41 | return p - str; |
| 42 | } |
| 43 | |
| 44 | void *memcpy(void *dst, const void *src, size_t n) |
| 45 | { |
| 46 | char *x = dst; |
| 47 | const char *y = src; |
| 48 | |
| 49 | while (n--) { |
| 50 | *x = *y; |
| 51 | x++; |
| 52 | y++; |
| 53 | } |
| 54 | |
| 55 | return dst; |
| 56 | } |
| 57 | |
| 58 | void *memmove(void *dst, const void *src, size_t n) |
| 59 | { |
| 60 | char *x; |
| 61 | const char *y; |
| 62 | |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 63 | if (dst < src) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 64 | return memcpy(dst, src, n); |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 65 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 66 | |
| 67 | x = (char *)dst + n - 1; |
| 68 | y = (const char *)src + n - 1; |
| 69 | |
| 70 | while (n--) { |
| 71 | *x = *y; |
| 72 | x--; |
| 73 | y--; |
| 74 | } |
| 75 | |
| 76 | return dst; |
| 77 | } |
| 78 | |
| 79 | int memcmp(const void *a, const void *b, size_t n) |
| 80 | { |
| 81 | const char *x = a; |
| 82 | const char *y = b; |
| 83 | |
| 84 | while (n--) { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 85 | if (*x != *y) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 86 | return *x - *y; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 87 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 88 | x++; |
| 89 | y++; |
| 90 | } |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | int strcmp(const char *a, const char *b) |
| 96 | { |
| 97 | const char *x = a; |
| 98 | const char *y = b; |
| 99 | |
| 100 | while (*x != 0 && *y != 0) { |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 101 | if (*x != *y) { |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 102 | return *x - *y; |
Andrew Scull | 7364a8e | 2018-07-19 15:39:29 +0100 | [diff] [blame] | 103 | } |
Wedson Almeida Filho | 987c0ff | 2018-06-20 16:34:38 +0100 | [diff] [blame] | 104 | x++; |
| 105 | y++; |
| 106 | } |
| 107 | |
| 108 | return *x - *y; |
| 109 | } |