Coverage Report

Created: 2026-06-13 06:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/icu/source/common/fixedstring.h
Line
Count
Source
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
0
    FixedString() = default;
32
0
    ~FixedString() { operator delete[](ptr); }
33
34
0
    FixedString(const FixedString& other) : FixedString(other.data()) {}
35
36
0
    FixedString(std::string_view init) {
37
0
        size_t size = init.size();
38
0
        if (size > 0 && reserve(size + 1)) {
39
0
            uprv_memcpy(ptr, init.data(), size);
40
0
            ptr[size] = '\0';
41
0
        }
42
0
    }
43
44
0
    FixedString& operator=(const FixedString& other) {
45
0
        *this = other.data();
46
0
        return *this;
47
0
    }
48
49
0
    FixedString& operator=(std::string_view init) {
50
0
        if (init.empty()) {
51
0
            operator delete[](ptr);
52
0
            ptr = nullptr;
53
0
        } else {
54
0
            size_t size = init.size();
55
0
            if (reserve(size + 1)) {
56
0
                uprv_memcpy(ptr, init.data(), size);
57
0
                ptr[size] = '\0';
58
0
            }
59
0
        }
60
0
        return *this;
61
0
    }
62
63
0
    FixedString(FixedString&& other) noexcept : ptr(std::exchange(other.ptr, nullptr)) {}
64
65
0
    FixedString& operator=(FixedString&& other) noexcept {
66
0
        operator delete[](ptr);
67
0
        ptr = other.ptr;
68
0
        other.ptr = nullptr;
69
0
        return *this;
70
0
    }
71
72
0
    void clear() {
73
0
        operator delete[](ptr);
74
0
        ptr = nullptr;
75
0
    }
76
77
0
    const char* data() const {
78
0
        return isEmpty() ? "" : ptr;
79
0
    }
80
81
0
    char* getAlias() {
82
0
        return ptr;
83
0
    }
84
85
0
    bool isEmpty() const {
86
0
        return ptr == nullptr;
87
0
    }
88
89
    /** Allocate storage for a new string, without initializing it. */
90
0
    bool reserve(size_t size) {
91
0
        operator delete[](ptr);
92
0
        ptr = static_cast<char*>(operator new[](size));
93
0
        return ptr != nullptr;
94
0
    }
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