diff options
Diffstat (limited to 'source/blender/blenlib/BLI_string_ref.hh')
-rw-r--r-- | source/blender/blenlib/BLI_string_ref.hh | 53 |
1 files changed, 29 insertions, 24 deletions
diff --git a/source/blender/blenlib/BLI_string_ref.hh b/source/blender/blenlib/BLI_string_ref.hh index 5b555b8cd1d..06fc66f6b55 100644 --- a/source/blender/blenlib/BLI_string_ref.hh +++ b/source/blender/blenlib/BLI_string_ref.hh @@ -60,9 +60,9 @@ class StringRef; class StringRefBase { protected: const char *data_; - uint size_; + int64_t size_; - StringRefBase(const char *data, const uint size) : data_(data), size_(size) + StringRefBase(const char *data, const int64_t size) : data_(data), size_(size) { } @@ -70,7 +70,7 @@ class StringRefBase { /** * Return the (byte-)length of the referenced string, without any null-terminator. */ - uint size() const + int64_t size() const { return size_; } @@ -94,7 +94,7 @@ class StringRefBase { */ operator std::string() const { - return std::string(data_, size_); + return std::string(data_, (size_t)size_); } const char *begin() const @@ -114,7 +114,7 @@ class StringRefBase { */ void unsafe_copy(char *dst) const { - memcpy(dst, data_, size_); + memcpy(dst, data_, (size_t)size_); dst[size_] = '\0'; } @@ -122,7 +122,7 @@ class StringRefBase { * Copy the string into a buffer. The copied string will be null-terminated. This invokes * undefined behavior when dst_size is too small. (Should we define the behavior?) */ - void copy(char *dst, const uint dst_size) const + void copy(char *dst, const int64_t dst_size) const { if (size_ < dst_size) { this->unsafe_copy(dst); @@ -137,7 +137,7 @@ class StringRefBase { * Copy the string into a char array. The copied string will be null-terminated. This invokes * undefined behavior when dst is too small. */ - template<uint N> void copy(char (&dst)[N]) + template<size_t N> void copy(char (&dst)[N]) { this->copy(dst, N); } @@ -152,7 +152,7 @@ class StringRefBase { */ bool endswith(StringRef suffix) const; - StringRef substr(uint start, const uint size) const; + StringRef substr(int64_t start, const int64_t size) const; }; /** @@ -168,7 +168,7 @@ class StringRefNull : public StringRefBase { /** * Construct a StringRefNull from a null terminated c-string. The pointer must not point to NULL. */ - StringRefNull(const char *str) : StringRefBase(str, (uint)strlen(str)) + StringRefNull(const char *str) : StringRefBase(str, (int64_t)strlen(str)) { BLI_assert(str != NULL); BLI_assert(data_[size_] == '\0'); @@ -178,9 +178,9 @@ class StringRefNull : public StringRefBase { * Construct a StringRefNull from a null terminated c-string. This invokes undefined behavior * when the given size is not the correct size of the string. */ - StringRefNull(const char *str, const uint size) : StringRefBase(str, size) + StringRefNull(const char *str, const int64_t size) : StringRefBase(str, size) { - BLI_assert((uint)strlen(str) == size); + BLI_assert((int64_t)strlen(str) == size); } /** @@ -194,8 +194,9 @@ class StringRefNull : public StringRefBase { /** * Get the char at the given index. */ - char operator[](const uint index) const + char operator[](const int64_t index) const { + BLI_assert(index >= 0); /* Use '<=' instead of just '<', so that the null character can be accessed as well. */ BLI_assert(index <= size_); return data_[index]; @@ -231,11 +232,11 @@ class StringRef : public StringRefBase { /** * Create a StringRef from a null-terminated c-string. */ - StringRef(const char *str) : StringRefBase(str, str ? (uint)strlen(str) : 0) + StringRef(const char *str) : StringRefBase(str, str ? (int64_t)strlen(str) : 0) { } - StringRef(const char *str, const uint length) : StringRefBase(str, length) + StringRef(const char *str, const int64_t length) : StringRefBase(str, length) { } @@ -244,7 +245,7 @@ class StringRef : public StringRefBase { * second point points to a smaller address than the first one. */ StringRef(const char *begin, const char *one_after_end) - : StringRefBase(begin, (uint)(one_after_end - begin)) + : StringRefBase(begin, (int64_t)(one_after_end - begin)) { BLI_assert(begin <= one_after_end); } @@ -253,15 +254,16 @@ class StringRef : public StringRefBase { * Reference a std::string. Remember that when the std::string is destructed, the StringRef * will point to uninitialized memory. */ - StringRef(const std::string &str) : StringRefBase(str.data(), (uint)str.size()) + StringRef(const std::string &str) : StringRefBase(str.data(), (int64_t)str.size()) { } /** * Return a new StringRef that does not contain the first n chars. */ - StringRef drop_prefix(const uint n) const + StringRef drop_prefix(const int64_t n) const { + BLI_assert(n >= 0); BLI_assert(n <= size_); return StringRef(data_ + n, size_ - n); } @@ -279,8 +281,9 @@ class StringRef : public StringRefBase { /** * Get the char at the given index. */ - char operator[](uint index) const + char operator[](int64_t index) const { + BLI_assert(index >= 0); BLI_assert(index < size_); return data_[index]; } @@ -297,7 +300,7 @@ inline std::ostream &operator<<(std::ostream &stream, StringRef ref) inline std::ostream &operator<<(std::ostream &stream, StringRefNull ref) { - stream << std::string(ref.data(), ref.size()); + stream << std::string(ref.data(), (size_t)ref.size()); return stream; } @@ -315,7 +318,7 @@ inline bool operator==(StringRef a, StringRef b) if (a.size() != b.size()) { return false; } - return STREQLEN(a.data(), b.data(), a.size()); + return STREQLEN(a.data(), b.data(), (size_t)a.size()); } inline bool operator!=(StringRef a, StringRef b) @@ -331,7 +334,7 @@ inline bool StringRefBase::startswith(StringRef prefix) const if (size_ < prefix.size_) { return false; } - for (uint i = 0; i < prefix.size_; i++) { + for (int64_t i = 0; i < prefix.size_; i++) { if (data_[i] != prefix.data_[i]) { return false; } @@ -347,8 +350,8 @@ inline bool StringRefBase::endswith(StringRef suffix) const if (size_ < suffix.size_) { return false; } - const uint offset = size_ - suffix.size_; - for (uint i = 0; i < suffix.size_; i++) { + const int64_t offset = size_ - suffix.size_; + for (int64_t i = 0; i < suffix.size_; i++) { if (data_[offset + i] != suffix.data_[i]) { return false; } @@ -359,8 +362,10 @@ inline bool StringRefBase::endswith(StringRef suffix) const /** * Return a new #StringRef containing only a sub-string of the original string. */ -inline StringRef StringRefBase::substr(const uint start, const uint size) const +inline StringRef StringRefBase::substr(const int64_t start, const int64_t size) const { + BLI_assert(size >= 0); + BLI_assert(start >= 0); BLI_assert(start + size <= size_); return StringRef(data_ + start, size); } |