Blender V5.0
string_ref.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_string_ref.hh"
10#include "BLI_string_utf8.h"
11
12#include <ostream>
13
14namespace blender {
15
16std::ostream &operator<<(std::ostream &stream, StringRef ref)
17{
18 stream << std::string(ref);
19 return stream;
20}
21
22std::ostream &operator<<(std::ostream &stream, StringRefNull ref)
23{
24 stream << std::string(ref.data(), size_t(ref.size()));
25 return stream;
26}
27
28void StringRefBase::copy_utf8_truncated(char *dst, const int64_t dst_size) const
29{
30 /* Destination must at least hold the null terminator. */
31 BLI_assert(dst_size >= 1);
32 /* The current #StringRef is assumed to contain valid UTF8. */
34
35 /* Common case when the string can just be copied over entirely. */
36 if (size_ < dst_size) {
37 this->copy_unsafe(dst);
38 return;
39 }
40
41 const int64_t max_copy_num_without_terminator = std::min(size_, dst_size - 1);
42 const size_t new_len = BLI_strncpy_utf8_rlen_unterminated(
43 dst, data_, max_copy_num_without_terminator);
44 dst[new_len] = '\0';
45}
46
47void StringRefBase::copy_bytes_truncated(char *dst, const int64_t dst_size) const
48{
49 /* Destination must at least hold the null terminator. */
50 BLI_assert(dst_size >= 1);
51
52 /* Common case when the string can just be copied over entirely. */
53 if (size_ < dst_size) {
54 this->copy_unsafe(dst);
55 return;
56 }
57
58 const int64_t new_len = std::min(size_, dst_size - 1);
59 memcpy(dst, data_, new_len);
60 dst[new_len] = '\0';
61}
62
63} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:46
ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len) ATTR_NONNULL(1)
char size_t size_t BLI_strncpy_utf8_rlen_unterminated(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy)
long long int int64_t
void copy_unsafe(char *dst) const
void copy_bytes_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:47
constexpr int64_t size() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
constexpr const char * data() const
std::ostream & operator<<(std::ostream &stream, const eAlpha &space)
Definition BLI_color.cc:15