File: string.h

package info (click to toggle)
memtest86%2B 6.10-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,532 kB
  • sloc: ansic: 19,267; asm: 2,027; makefile: 367; sh: 134
file content (99 lines) | stat: -rw-r--r-- 2,582 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// SPDX-License-Identifier: GPL-2.0
#ifndef STRING_H
#define STRING_H
/**
 * \file
 *
 * Provides a subset of the functions normally provided by <string.h>.
 *
 *//*
 * Copyright (C) 2020-2022 Martin Whitaker.
 */

#include <stddef.h>

/**
 * Compares the first n bytes of the memory areas pointed to by s1 and s2
 * and returns 0 if all bytes are the same or the numerical difference
 * between the first mismatching byte in s1 (interpreted as an unsigned
 * value) and the corresponding byte in s2.
 */
static inline int memcmp(const void *s1, const void *s2, size_t n)
{
    const unsigned char *src1 = s1, *src2 = s2;

    for (size_t i = 0; i < n; i++) {
        if (src1[i] != src2[i]) {
            return (int)src1[i] - (int)src2[i];
        }
    }
    return 0;
}

/**
 * Copies n bytes from the memory area pointed to by src to the memory area
 * pointed to by dest and returns a pointer to dest. The memory areas must
 * not overlap.
 * void *memcpy(void *dst, const void *src, size_t n);
 */
#define memcpy(d, s, n) __builtin_memcpy((d), (s), (n))

/**
 * Copies n bytes from the memory area pointed to by src to the memory area
 * pointed to by dest and returns a pointer to dest. The memory areas may
 * overlap.
 */
void *memmove(void *dest, const void *src, size_t n);

/**
 * Fills the first n bytes of the memory area pointed to by s with the byte
 * value c.
 * void *memset(void *s, int c, size_t n);
 */
#define memset(s, c, n) __builtin_memset((s), (c), (n))

/**
 * Returns the string length, excluding the terminating null character.
 */
static inline size_t strlen(const char *s)
{
    size_t len = 0;
    while (*s++) {
        len++;
    }
    return len;
}

/**
 * Compares at most the first n characters in the strings s1 and s2 and
 * returns 0 if all characters are the same or the numerical difference
 * between the first mismatching character in s1 (interpreted as a signed
 * value) and the corresponding character in s2.
 */
static inline int strncmp(const char *s1, const char *s2, size_t n)
{
    for (size_t i = 0; i < n; i++) {
        if (s1[i] != s2[i]) {
            return (int)s1[i] - (int)s2[i];
        }
        if (s1[i] == '\0') {
            return 0;
        }
    }
    return 0;
}

/**
 * Finds the first occurrence of the substring needle in the string haystack
 * and returns a pointer to the beginning of the located substring, or NULL
 * if the substring is not found.
 */
char *strstr(const char *haystack, const char *needle);

/**
 * Convert n to characters in s
 */

char *itoa(int num, char *str);

#endif // STRING_H