| 779 | |
| 780 | // `strndup` using mi_malloc |
| 781 | mi_decl_nodiscard mi_decl_restrict char* mi_heap_strndup(mi_heap_t* heap, const char* s, size_t n) mi_attr_noexcept { |
| 782 | if (s == NULL) return NULL; |
| 783 | const char* end = (const char*)memchr(s, 0, n); // find end of string in the first `n` characters (returns NULL if not found) |
| 784 | const size_t m = (end != NULL ? (size_t)(end - s) : n); // `m` is the minimum of `n` or the end-of-string |
| 785 | mi_assert_internal(m <= n); |
| 786 | char* t = (char*)mi_heap_malloc(heap, m+1); |
| 787 | if (t == NULL) return NULL; |
| 788 | _mi_memcpy(t, s, m); |
| 789 | t[m] = 0; |
| 790 | return t; |
| 791 | } |
| 792 | |
| 793 | mi_decl_nodiscard mi_decl_restrict char* mi_strndup(const char* s, size_t n) mi_attr_noexcept { |
| 794 | return mi_heap_strndup(mi_get_default_heap(),s,n); |
no test coverage detected