blob: 45263c8f5ca4516c8d3d989667a915b2891c65e9 [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 Scullcbefbdb2019-01-11 16:36:26 +000022
Andrew Scull7364a8e2018-07-19 15:39:29 +010023 while (n--) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010024 *p++ = c;
Andrew Scull7364a8e2018-07-19 15:39:29 +010025 }
Andrew Scullcbefbdb2019-01-11 16:36:26 +000026
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010027 return s;
28}
29
30/*
31 * Calculates the length of the provided null-terminated string.
32 */
33size_t strlen(const char *str)
34{
35 const char *p = str;
Andrew Scullcbefbdb2019-01-11 16:36:26 +000036
Andrew Scull7364a8e2018-07-19 15:39:29 +010037 while (*p) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010038 p++;
Andrew Scull7364a8e2018-07-19 15:39:29 +010039 }
Andrew Scullcbefbdb2019-01-11 16:36:26 +000040
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010041 return p - str;
42}
43
44void *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
58void *memmove(void *dst, const void *src, size_t n)
59{
60 char *x;
61 const char *y;
62
Andrew Scull7364a8e2018-07-19 15:39:29 +010063 if (dst < src) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010064 return memcpy(dst, src, n);
Andrew Scull7364a8e2018-07-19 15:39:29 +010065 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010066
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
79int 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 Scull7364a8e2018-07-19 15:39:29 +010085 if (*x != *y) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010086 return *x - *y;
Andrew Scull7364a8e2018-07-19 15:39:29 +010087 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +010088 x++;
89 y++;
90 }
91
92 return 0;
93}
94
95int 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 Scull7364a8e2018-07-19 15:39:29 +0100101 if (*x != *y) {
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100102 return *x - *y;
Andrew Scull7364a8e2018-07-19 15:39:29 +0100103 }
Wedson Almeida Filho987c0ff2018-06-20 16:34:38 +0100104 x++;
105 y++;
106 }
107
108 return *x - *y;
109}