Blender V5.3
BLI_string_ref.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
33
34#include <cstring>
35#include <string>
36#include <string_view>
37
38#include "BLI_compiler_attrs.hh"
39#include "BLI_span.hh"
40
41namespace blender {
42
43class StringRef;
44
50 protected:
51 /* May be nullptr when used in StringRef. */
52 const char *data_;
54
55 constexpr StringRefBase(const char *data, int64_t size);
56
57 public:
59 using value_type = char;
60
62 static constexpr int64_t not_found = -1;
63
64 constexpr int64_t size() const;
65 constexpr bool is_empty() const;
66 constexpr const char *data() const;
67 constexpr operator Span<char>() const;
68
69 operator std::string() const;
70 constexpr operator std::string_view() const;
71
72 constexpr const char *begin() const;
73 constexpr const char *end() const;
74
75 constexpr IndexRange index_range() const;
76
82 void copy_utf8_truncated(char *dst, int64_t dst_size) const;
83 template<size_t N> void copy_utf8_truncated(char (&dst)[N]) const;
84
93 void copy_bytes_truncated(char *dst, int64_t dst_size) const;
94 template<size_t N> void copy_bytes_truncated(char (&dst)[N]) const;
95
101 void copy_unsafe(char *dst) const;
102
103 constexpr bool startswith(StringRef prefix) const;
104 constexpr bool endswith(StringRef suffix) const;
105 constexpr StringRef substr(int64_t start, int64_t size) const;
106
107 constexpr const char &front() const;
108 constexpr const char &back() const;
109
117 constexpr int64_t find(char c, int64_t pos = 0) const;
118 constexpr int64_t find(StringRef str, int64_t pos = 0) const;
119 constexpr int64_t rfind(char c, int64_t pos = INT64_MAX) const;
120 constexpr int64_t rfind(StringRef str, int64_t pos = INT64_MAX) const;
121 constexpr int64_t find_first_of(StringRef chars, int64_t pos = 0) const;
122 constexpr int64_t find_first_of(char c, int64_t pos = 0) const;
123 constexpr int64_t find_last_of(StringRef chars, int64_t pos = INT64_MAX) const;
124 constexpr int64_t find_last_of(char c, int64_t pos = INT64_MAX) const;
125 constexpr int64_t find_first_not_of(StringRef chars, int64_t pos = 0) const;
126 constexpr int64_t find_first_not_of(char c, int64_t pos = 0) const;
127 constexpr int64_t find_last_not_of(StringRef chars, int64_t pos = INT64_MAX) const;
128 constexpr int64_t find_last_not_of(char c, int64_t pos = INT64_MAX) const;
129
130 constexpr StringRef trim() const;
131 constexpr StringRef trim(StringRef characters_to_remove) const;
132 constexpr StringRef trim(char character_to_remove) const;
133 constexpr StringRef trim_left() const;
134 constexpr StringRef trim_left(StringRef characters_to_remove) const;
135 constexpr StringRef trim_right() const;
136 constexpr StringRef trim_right(StringRef characters_to_remove) const;
137};
138
145
146 public:
147 constexpr StringRefNull();
148 constexpr StringRefNull(const char *str, int64_t size) ATTR_NONNULL(2);
149 StringRefNull(std::nullptr_t) = delete;
150 constexpr StringRefNull(const char *str) ATTR_NONNULL(2);
151 StringRefNull(const std::string &str);
152
153 constexpr char operator[](int64_t index) const;
154 constexpr const char *c_str() const;
155};
156
165class StringRef : public StringRefBase {
166 public:
167 constexpr StringRef();
168 constexpr StringRef(StringRefNull other);
169 constexpr StringRef(const char *str);
170 constexpr StringRef(const char *str, int64_t length);
171 constexpr StringRef(const char *begin, const char *one_after_end);
172 constexpr StringRef(std::string_view view);
173 constexpr StringRef(Span<char> span);
174 StringRef(const std::string &str);
175
176 constexpr StringRef drop_prefix(int64_t n) const;
177 constexpr StringRef drop_known_prefix(StringRef prefix) const;
178 constexpr StringRef drop_suffix(int64_t n) const;
179 constexpr StringRef drop_known_suffix(StringRef suffix) const;
180
181 constexpr char operator[](int64_t index) const;
182};
183
184/* -------------------------------------------------------------------- */
187
188constexpr StringRefBase::StringRefBase(const char *data, const int64_t size)
189 : data_(data), size_(size)
190{
191}
192
197{
198 return size_;
199}
200
201constexpr bool StringRefBase::is_empty() const
202{
203 return size_ == 0;
204}
205
209constexpr const char *StringRefBase::data() const
210{
211 return data_;
212}
213
214constexpr StringRefBase::operator Span<char>() const
215{
216 return Span<char>(data_, size_);
217}
218
223inline StringRefBase::operator std::string() const
224{
225 return std::string(data_, size_t(size_));
226}
227
228constexpr StringRefBase::operator std::string_view() const
229{
230 return std::string_view(data_, size_t(size_));
231}
232
233constexpr const char *StringRefBase::begin() const
234{
235 return data_;
236}
237
238constexpr const char *StringRefBase::end() const
239{
240 return data_ + size_;
241}
242
244{
245 return IndexRange(size_);
246}
247
248inline void StringRefBase::copy_unsafe(char *dst) const
249{
250 if (size_ > 0) {
251 memcpy(dst, data_, size_t(size_));
252 }
253 dst[size_] = '\0';
254}
255
256template<size_t N> inline void StringRefBase::copy_utf8_truncated(char (&dst)[N]) const
257{
258 this->copy_utf8_truncated(dst, N);
259}
260
261template<size_t N> inline void StringRefBase::copy_bytes_truncated(char (&dst)[N]) const
262{
263 this->copy_bytes_truncated(dst, N);
264}
265
269constexpr bool StringRefBase::startswith(StringRef prefix) const
270{
271 if (size_ < prefix.size_) {
272 return false;
273 }
274 for (int64_t i = 0; i < prefix.size_; i++) {
275 if (data_[i] != prefix.data_[i]) {
276 return false;
277 }
278 }
279 return true;
280}
281
285constexpr bool StringRefBase::endswith(StringRef suffix) const
286{
287 if (size_ < suffix.size_) {
288 return false;
289 }
290 const int64_t offset = size_ - suffix.size_;
291 for (int64_t i = 0; i < suffix.size_; i++) {
292 if (data_[offset + i] != suffix.data_[i]) {
293 return false;
294 }
295 }
296 return true;
297}
298
304 const int64_t max_size = INT64_MAX) const
305{
306 BLI_assert(max_size >= 0);
307 BLI_assert(start >= 0);
308 const int64_t substr_size = std::min(max_size, size_ - start);
309 return StringRef(data_ + start, substr_size);
310}
311
315constexpr const char &StringRefBase::front() const
316{
317 BLI_assert(size_ >= 1);
318 return data_[0];
319}
320
324constexpr const char &StringRefBase::back() const
325{
326 BLI_assert(size_ >= 1);
327 return data_[size_ - 1];
328}
329
331{
332 /* The compiler will probably optimize this check away. */
333 if (index == std::string_view::npos) {
335 }
336 return int64_t(index);
337}
338
339constexpr int64_t StringRefBase::find(char c, int64_t pos) const
340{
341 BLI_assert(pos >= 0);
342 return index_or_npos_to_int64(std::string_view(*this).find(c, size_t(pos)));
343}
344
346{
347 BLI_assert(pos >= 0);
348 return index_or_npos_to_int64(std::string_view(*this).find(str, size_t(pos)));
349}
350
351constexpr int64_t StringRefBase::rfind(char c, int64_t pos) const
352{
353 BLI_assert(pos >= 0);
354 return index_or_npos_to_int64(std::string_view(*this).rfind(c, size_t(pos)));
355}
356
358{
359 BLI_assert(pos >= 0);
360 return index_or_npos_to_int64(std::string_view(*this).rfind(str, size_t(pos)));
361}
362
364{
365 BLI_assert(pos >= 0);
366 return index_or_npos_to_int64(std::string_view(*this).find_first_of(chars, size_t(pos)));
367}
368
369constexpr int64_t StringRefBase::find_first_of(char c, int64_t pos) const
370{
371 BLI_assert(pos >= 0);
372 return index_or_npos_to_int64(std::string_view(*this).find_first_of(c, size_t(pos)));
373}
374
376{
377 BLI_assert(pos >= 0);
378 return index_or_npos_to_int64(std::string_view(*this).find_last_of(chars, size_t(pos)));
379}
380
381constexpr int64_t StringRefBase::find_last_of(char c, int64_t pos) const
382{
383 BLI_assert(pos >= 0);
384 return index_or_npos_to_int64(std::string_view(*this).find_last_of(c, size_t(pos)));
385}
386
388{
389 BLI_assert(pos >= 0);
390 return index_or_npos_to_int64(std::string_view(*this).find_first_not_of(chars, size_t(pos)));
391}
392
394{
395 BLI_assert(pos >= 0);
396 return index_or_npos_to_int64(std::string_view(*this).find_first_not_of(c, size_t(pos)));
397}
398
400{
401 BLI_assert(pos >= 0);
402 return index_or_npos_to_int64(std::string_view(*this).find_last_not_of(chars, size_t(pos)));
403}
404
406{
407 BLI_assert(pos >= 0);
408 return index_or_npos_to_int64(std::string_view(*this).find_last_not_of(c, size_t(pos)));
409}
410
415{
416 return this->trim(" \t\r\n");
417}
418
423constexpr StringRef StringRefBase::trim(const char character_to_remove) const
424{
425 return this->trim(StringRef(&character_to_remove, 1));
426}
427
432constexpr StringRef StringRefBase::trim(StringRef characters_to_remove) const
433{
434 const int64_t find_front = this->find_first_not_of(characters_to_remove);
435 if (find_front == not_found) {
436 return StringRef();
437 }
438 const int64_t find_end = this->find_last_not_of(characters_to_remove);
439 /* `find_end` cannot be `not_found`, because that means the string is only
440 * `characters_to_remove`, in which case `find_front` would already have
441 * been `not_found`. */
442 BLI_assert_msg(find_end != not_found,
443 "forward search found characters-to-not-remove, but backward search did not");
444 const int64_t substr_len = find_end - find_front + 1;
445 return this->substr(find_front, substr_len);
446}
447
452{
453 return this->trim_left(" \t\r\n");
454}
455
460constexpr StringRef StringRefBase::trim_left(StringRef characters_to_remove) const
461{
462 const int64_t find_front = this->find_first_not_of(characters_to_remove);
463 if (find_front == not_found) {
464 return StringRef();
465 }
466 return this->substr(find_front);
467}
468
473{
474 return this->trim_right(" \t\r\n");
475}
476
481constexpr StringRef StringRefBase::trim_right(StringRef characters_to_remove) const
482{
483 const int64_t find_end = this->find_last_not_of(characters_to_remove);
484 if (find_end == not_found) {
485 return StringRef();
486 }
487 return this->substr(0, find_end + 1);
488}
489
491
492/* -------------------------------------------------------------------- */
495
497
502constexpr StringRefNull::StringRefNull(const char *str, const int64_t size)
504{
505 BLI_assert(int64_t(std::char_traits<char>::length(str)) == size);
506}
507
512constexpr StringRefNull::StringRefNull(const char *str)
513 : StringRefBase(str, int64_t(std::char_traits<char>::length(str)))
514{
515 BLI_assert(data_ != nullptr);
516 BLI_assert(data_[size_] == '\0');
517}
518
523inline StringRefNull::StringRefNull(const std::string &str)
525{
526}
527
531constexpr char StringRefNull::operator[](const int64_t index) const
532{
533 BLI_assert(index >= 0);
534 /* Use '<=' instead of just '<', so that the null character can be accessed as well. */
536 return data_[index];
537}
538
544constexpr const char *StringRefNull::c_str() const
545{
546 return data_;
547}
548
550
551/* -------------------------------------------------------------------- */
554
556
560constexpr StringRef::StringRef(StringRefNull other) : StringRefBase(other.data(), other.size()) {}
561
565constexpr StringRef::StringRef(const char *str)
566 : StringRefBase(str, str ? int64_t(std::char_traits<char>::length(str)) : 0)
567{
568}
569
571{
572}
573
579{
580 BLI_assert(n >= 0);
581 const int64_t clamped_n = std::min(n, size_);
582 const int64_t new_size = size_ - clamped_n;
583 return StringRef(data_ + clamped_n, new_size);
584}
585
591{
592 BLI_assert(this->startswith(prefix));
593 return this->drop_prefix(prefix.size());
594}
595
601{
602 BLI_assert(n >= 0);
603 const int64_t new_size = std::max<int64_t>(0, size_ - n);
604 return StringRef(data_, new_size);
605}
606
612{
613 BLI_assert(this->endswith(suffix));
614 return this->drop_suffix(suffix.size());
615}
616
621{
622 BLI_assert(index >= 0);
624 return data_[index];
625}
626
631constexpr StringRef::StringRef(const char *begin, const char *one_after_end)
632 : StringRefBase(begin, int64_t(one_after_end - begin))
633{
634 BLI_assert(begin <= one_after_end);
635}
636
641inline StringRef::StringRef(const std::string &str)
643{
644}
645
646constexpr StringRef::StringRef(std::string_view view)
647 : StringRefBase(view.data(), int64_t(view.size()))
648{
649}
650
651constexpr StringRef::StringRef(Span<char> span) : StringRefBase(span.data(), span.size()) {}
652
654
655/* -------------------------------------------------------------------- */
658
659std::ostream &operator<<(std::ostream &stream, StringRef ref);
660std::ostream &operator<<(std::ostream &stream, StringRefNull ref);
661
666inline std::string operator+(StringRef a, StringRef b)
667{
668 return std::string(a) + std::string(b);
669}
670
681constexpr bool operator==(StringRef a, StringRef b)
682{
683 return std::string_view(a) == std::string_view(b);
684}
685
686constexpr bool operator!=(StringRef a, StringRef b)
687{
688 return std::string_view(a) != std::string_view(b);
689}
690
691constexpr bool operator<(StringRef a, StringRef b)
692{
693 return std::string_view(a) < std::string_view(b);
694}
695
696constexpr bool operator>(StringRef a, StringRef b)
697{
698 return std::string_view(a) > std::string_view(b);
699}
700
701constexpr bool operator<=(StringRef a, StringRef b)
702{
703 return std::string_view(a) <= std::string_view(b);
704}
705
706constexpr bool operator>=(StringRef a, StringRef b)
707{
708 return std::string_view(a) >= std::string_view(b);
709}
710
712
713/* -------------------------------------------------------------------- */
716
720inline std::string_view format_as(StringRef str)
721{
722 return str;
723}
724
726
727} // namespace blender
#define INT64_MAX
#define BLI_assert_msg(a, msg)
constexpr IndexRange()=default
void copy_unsafe(char *dst) const
constexpr StringRef trim(StringRef characters_to_remove) const
constexpr int64_t find(StringRef str, int64_t pos=0) const
constexpr StringRef trim_right(StringRef characters_to_remove) const
constexpr const char & front() const
static constexpr int64_t not_found
constexpr int64_t find_last_not_of(char c, int64_t pos=INT64_MAX) const
void copy_utf8_truncated(char(&dst)[N]) const
constexpr int64_t rfind(char c, int64_t pos=INT64_MAX) const
constexpr int64_t find_last_of(StringRef chars, int64_t pos=INT64_MAX) const
constexpr int64_t find(char c, int64_t pos=0) const
void copy_bytes_truncated(char(&dst)[N]) const
constexpr const char * begin() const
constexpr const char * end() const
constexpr int64_t find_last_of(char c, int64_t pos=INT64_MAX) const
constexpr int64_t find_last_not_of(StringRef chars, int64_t pos=INT64_MAX) const
constexpr int64_t find_first_not_of(StringRef chars, int64_t pos=0) const
constexpr int64_t find_first_of(char c, int64_t pos=0) const
constexpr bool is_empty() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr StringRefBase(const char *data, int64_t size)
constexpr int64_t rfind(StringRef str, int64_t pos=INT64_MAX) const
constexpr StringRef trim_right() const
void copy_bytes_truncated(char *dst, int64_t dst_size) const
constexpr bool startswith(StringRef prefix) const
constexpr int64_t find_first_not_of(char c, int64_t pos=0) const
constexpr StringRef trim_left() const
constexpr bool endswith(StringRef suffix) const
constexpr int64_t find_first_of(StringRef chars, int64_t pos=0) const
constexpr StringRef trim(char character_to_remove) const
constexpr IndexRange index_range() const
constexpr int64_t size() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
constexpr StringRef trim_left(StringRef characters_to_remove) const
constexpr StringRef trim() const
constexpr const char * data() const
constexpr const char & back() const
constexpr StringRefNull(const char *str) ATTR_NONNULL(2)
StringRefNull(const std::string &str)
constexpr const char * c_str() const
StringRefNull(std::nullptr_t)=delete
constexpr StringRefNull()
constexpr char operator[](int64_t index) const
constexpr StringRefNull(const char *str, int64_t size) ATTR_NONNULL(2)
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef drop_suffix(int64_t n) const
constexpr char operator[](int64_t index) const
constexpr StringRef(const char *begin, const char *one_after_end)
constexpr StringRef(std::string_view view)
constexpr StringRef drop_known_prefix(StringRef prefix) const
constexpr StringRef(Span< char > span)
constexpr StringRef(StringRefNull other)
StringRef(const std::string &str)
constexpr StringRef(const char *str, int64_t length)
constexpr StringRef(const char *str)
constexpr StringRef()
constexpr StringRef drop_known_suffix(StringRef suffix) const
constexpr StringRef trim() const
constexpr int64_t find_last_not_of(StringRef chars, int64_t pos=INT64_MAX) const
constexpr bool startswith(StringRef prefix) const
constexpr const char * begin() const
static constexpr int64_t not_found
constexpr int64_t size() const
void copy_bytes_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:47
constexpr StringRef trim_right() const
constexpr int64_t find_first_not_of(StringRef chars, int64_t pos=0) const
constexpr StringRef trim_left() const
constexpr StringRef substr(int64_t start, int64_t size) const
constexpr const char * data() const
void copy_utf8_truncated(char *dst, int64_t dst_size) const
Definition string_ref.cc:28
constexpr StringRefBase(const char *data, int64_t size)
constexpr bool endswith(StringRef suffix) const
constexpr const char * c_str() const
constexpr StringRef drop_prefix(int64_t n) const
constexpr StringRef drop_suffix(int64_t n) const
#define str(s)
float length(VecOp< float, D >) RET
#define N
bool operator==(const Error &left, const Error &right)
std::ostream & operator<<(std::ostream &stream, StringRef ref)
constexpr int64_t index_or_npos_to_int64(size_t index)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
constexpr int64_t index_or_npos_to_int64(size_t index)
__int64 int64_t
Definition BKE_text.h:90
bool operator!=(const CPPType &a, const CPPType &b)
std::string_view format_as(StringRef str)
constexpr bool operator>=(StringRef a, StringRef b)
constexpr bool operator<(StringRef a, StringRef b)
constexpr bool operator<=(StringRef a, StringRef b)
constexpr bool operator>(StringRef a, StringRef b)
void ATTR_NONNULL(1, 2)
std::string operator+(StringRef a, StringRef b)