File: string.h

package info (click to toggle)
intel-compute-runtime 22.43.24595.41-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 57,740 kB
  • sloc: cpp: 631,142; lisp: 3,515; sh: 470; makefile: 76; python: 21
file content (125 lines) | stat: -rw-r--r-- 2,986 bytes parent folder | download | duplicates (3)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright (C) 2018-2022 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once

#include <memory>
#include <type_traits>

#if defined(__linux__)

#include <cstring>
#include <errno.h>
#include <string>

inline int strcpy_s(char *dst, size_t dstSize, const char *src) { // NOLINT(readability-identifier-naming)
    if ((dst == nullptr) || (src == nullptr)) {
        return -EINVAL;
    }
    size_t length = strlen(src);
    if (dstSize <= length) {
        return -ERANGE;
    }

    memcpy(dst, src, length);
    dst[length] = '\0';

    return 0;
}

inline size_t strnlen_s(const char *str, size_t count) { // NOLINT(readability-identifier-naming)
    if (str == nullptr) {
        return 0;
    }

    for (size_t i = 0; i < count; ++i) {
        if (str[i] == '\0')
            return i;
    }

    return count;
}

inline int strncpy_s(char *dst, size_t numberOfElements, const char *src, size_t count) { // NOLINT(readability-identifier-naming)
    if ((dst == nullptr) || (src == nullptr)) {
        return -EINVAL;
    }

    size_t length = strnlen_s(src, count);
    if (numberOfElements <= count && numberOfElements <= length) {
        return -ERANGE;
    }

    memcpy(dst, src, length);
    dst[length] = '\0';

    return 0;
}

inline int memcpy_s(void *dst, size_t destSize, const void *src, size_t count) { // NOLINT(readability-identifier-naming)
    if ((dst == nullptr) || (src == nullptr)) {
        return -EINVAL;
    }
    if (destSize < count) {
        return -ERANGE;
    }

    memcpy(dst, src, count);

    return 0;
}

inline int memmove_s(void *dst, size_t numberOfElements, const void *src, size_t count) { // NOLINT(readability-identifier-naming)
    if ((dst == nullptr) || (src == nullptr)) {
        return -EINVAL;
    }
    if (numberOfElements < count) {
        return -ERANGE;
    }

    memmove(dst, src, count);

    return 0;
}

template <typename... Args>
inline int snprintf_s(char *buffer, size_t sizeOfBuffer, size_t count, const char *format, Args &&...args) { // NOLINT(readability-identifier-naming)
    if ((buffer == nullptr) || (format == nullptr)) {
        return -EINVAL;
    }

    return snprintf(buffer, sizeOfBuffer, format, std::forward<Args>(args)...);
}

#endif

#if defined(_WIN32)

template <typename... Args>
inline int snprintf_s(char *buffer, size_t sizeOfBuffer, size_t count, const char *format, Args &&...args) { // NOLINT(readability-identifier-naming)
    if ((buffer == nullptr) || (format == nullptr)) {
        return -EINVAL;
    }

    return _snprintf_s(buffer, sizeOfBuffer, count, format, std::forward<Args>(args)...);
}

#endif

template <typename T = char>
inline std::unique_ptr<T[]> makeCopy(const void *src, size_t size) {
    if (size == 0) {
        return nullptr;
    }

    static_assert(sizeof(T) == 1u && std::is_trivially_copyable_v<T>);

    auto copiedData = std::make_unique<T[]>(size);
    memcpy_s(copiedData.get(), size, src, size);

    return copiedData;
}