tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

fixedstring.h (2664B)


      1 // © 2025 and later: Unicode, Inc. and others.
      2 // License & terms of use: https://www.unicode.org/copyright.html
      3 
      4 #ifndef FIXEDSTRING_H
      5 #define FIXEDSTRING_H
      6 
      7 #include <string_view>
      8 #include <utility>
      9 
     10 #include "unicode/uobject.h"
     11 #include "unicode/utypes.h"
     12 #include "cmemory.h"
     13 
     14 U_NAMESPACE_BEGIN
     15 
     16 class UnicodeString;
     17 
     18 /**
     19 * ICU-internal fixed-length char* string class.
     20 * This is a complement to CharString to store fixed-length strings efficiently
     21 * (not allocating any unnecessary storage for future additions to the string).
     22 *
     23 * A terminating NUL is always stored, but the length of the string isn't.
     24 * An empty string is stored as nullptr, allocating no storage at all.
     25 *
     26 * This class wants to be convenient but is also deliberately minimalist.
     27 * Please do not add methods if they only add minor convenience.
     28 */
     29 class FixedString : public UMemory {
     30 public:
     31    FixedString() = default;
     32    ~FixedString() { operator delete[](ptr); }
     33 
     34    FixedString(const FixedString& other) : FixedString(other.data()) {}
     35 
     36    FixedString(std::string_view init) {
     37        size_t size = init.size();
     38        if (size > 0 && reserve(size + 1)) {
     39            uprv_memcpy(ptr, init.data(), size);
     40            ptr[size] = '\0';
     41        }
     42    }
     43 
     44    FixedString& operator=(const FixedString& other) {
     45        *this = other.data();
     46        return *this;
     47    }
     48 
     49    FixedString& operator=(std::string_view init) {
     50        if (init.empty()) {
     51            operator delete[](ptr);
     52            ptr = nullptr;
     53        } else {
     54            size_t size = init.size();
     55            if (reserve(size + 1)) {
     56                uprv_memcpy(ptr, init.data(), size);
     57                ptr[size] = '\0';
     58            }
     59        }
     60        return *this;
     61    }
     62 
     63    FixedString(FixedString&& other) noexcept : ptr(std::exchange(other.ptr, nullptr)) {}
     64 
     65    FixedString& operator=(FixedString&& other) noexcept {
     66        operator delete[](ptr);
     67        ptr = other.ptr;
     68        other.ptr = nullptr;
     69        return *this;
     70    }
     71 
     72    void clear() {
     73        operator delete[](ptr);
     74        ptr = nullptr;
     75    }
     76 
     77    const char* data() const {
     78        return isEmpty() ? "" : ptr;
     79    }
     80 
     81    char* getAlias() {
     82        return ptr;
     83    }
     84 
     85    bool isEmpty() const {
     86        return ptr == nullptr;
     87    }
     88 
     89    /** Allocate storage for a new string, without initializing it. */
     90    bool reserve(size_t size) {
     91        operator delete[](ptr);
     92        ptr = static_cast<char*>(operator new[](size));
     93        return ptr != nullptr;
     94    }
     95 
     96 private:
     97    char* ptr = nullptr;
     98 };
     99 
    100 U_COMMON_API void copyInvariantChars(const UnicodeString& src, FixedString& dst, UErrorCode& status);
    101 
    102 U_NAMESPACE_END
    103 
    104 #endif