blob: 036c563485272f15ac1e79df7fc14832edc6e896 [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
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 Scull18c78fc2018-08-20 12:57:41 +010017#include "hf/std.h"
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010018
19void *memset(void *s, int c, size_t n)
20{
21 char *p = (char *)s;
Andrew Scull7364a8e2018-07-19 15:39:29 +010022 while (n--) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010023 *p++ = c;
Andrew Scull7364a8e2018-07-19 15:39:29 +010024 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010025 return s;
26}
27
28/*
29 * Calculates the length of the provided null-terminated string.
30 */
31size_t strlen(const char *str)
32{
33 const char *p = str;
Andrew Scull7364a8e2018-07-19 15:39:29 +010034 while (*p) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010035 p++;
Andrew Scull7364a8e2018-07-19 15:39:29 +010036 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010037 return p - str;
38}
39
40void *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
54void *memmove(void *dst, const void *src, size_t n)
55{
56 char *x;
57 const char *y;
58
Andrew Scull7364a8e2018-07-19 15:39:29 +010059 if (dst < src) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010060 return memcpy(dst, src, n);
Andrew Scull7364a8e2018-07-19 15:39:29 +010061 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010062
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
75int 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 Scull7364a8e2018-07-19 15:39:29 +010081 if (*x != *y) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010082 return *x - *y;
Andrew Scull7364a8e2018-07-19 15:39:29 +010083 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010084 x++;
85 y++;
86 }
87
88 return 0;
89}
90
91int 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 Scull7364a8e2018-07-19 15:39:29 +010097 if (*x != *y) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010098 return *x - *y;
Andrew Scull7364a8e2018-07-19 15:39:29 +010099 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100100 x++;
101 y++;
102 }
103
104 return *x - *y;
105}