Coverage Report

Created: 2025-09-05 07:16

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