🛰️航天仿真算法库 SpaceAST 0.4.0
载入中...
搜索中...
未找到
Identifier.hpp
浏览该文件的文档.
1
20
21#pragma once
22
23#include "AstGlobal.h"
24#include "AstUtil/IdentifierAPI.hpp"
25#include "AstUtil/StringView.hpp"
26#include <string>
27#include <memory.h>
28#include <cstring>
29#include <new> // for ::operator delete
30
31AST_NAMESPACE_BEGIN
32
33
39private:
40 uint32_t length_; // 字符串长度(字节数)
41 char data_[1]; // 内联数据(柔性数组)
42
43 // 禁止拷贝和赋值
44 Identifier(const Identifier&) = delete;
45 Identifier& operator=(const Identifier&) = delete;
46
47 // 私有构造函数,只能通过IdentifierTable创建
48 explicit Identifier(uint32_t len) : length_(len) {}
49
50public:
51 // 获取数据指针
52 const char* data() const { return data_; }
53 char* data() { return data_; }
54
55
56 // 获取长度
57 uint32_t length() const { return length_; }
58
59 // 转换为C字符串
60 const char* c_str() const {
61 return data_;
62 }
63
64 // 释放内存
65 // 注意:标识符由 IdentifierTable 变长分配(::operator new(sizeof(Identifier) + length)),
66 // 分配尺寸大于 sizeof(Identifier),而编译器为 delete 生成的 sized deallocation
67 // 传入的尺寸是 sizeof(Identifier),两者不符,ASan 会报 new-delete-type-mismatch。
68 // 这里提供类级 operator delete 并转发到无尺寸的全局版本。
69 // 有尺寸的版本必须一并声明:只声明无尺寸版本时,若实现仍选中全局 sized delete,问题依旧。
70 static void operator delete(void* ptr) noexcept { ::operator delete(ptr); }
71 static void operator delete(void* ptr, size_t) noexcept { ::operator delete(ptr); }
72 static void operator delete(void* ptr, void*) noexcept { ::operator delete(ptr); }
73
74 // 转换为std::string
75 std::string toString() const {
76 return std::string(data_, length_);
77 }
78
79 // 比较操作
80 bool equals(const char* str, size_t len) const {
81 if (len != length_) return false;
82 return memcmp(data_, str, len) == 0;
83 }
84
85 bool equals(const char* str) const {
86 return equals(str, std::char_traits<char>::length(str));
87 }
88
89 bool equals(const std::string& str) const {
90 return equals(str.c_str(), str.length());
91 }
92
93 bool equals(StringView sv) const {
94 return equals(sv.data(), sv.size());
95 }
96
98 static size_t hash(const char* data, size_t len) {
99 // DJB2哈希算法
100 size_t h = 5381;
101 for (size_t i = 0; i < len; ++i) {
102 h = ((h << 5) + h) + static_cast<unsigned char>(data[i]);
103 }
104 return h;
105 }
106
107 // 计算哈希值
108 size_t hash() const {
109 // DJB2哈希算法
110 return hash(data_, length_);
111 }
112
113
114 // 检查是否以特定前缀开头
115 bool starts_with(const char* prefix) const {
116 size_t prefix_len = strlen(prefix);
117 if (static_cast<size_t>(length_) < prefix_len) return false;
118 return memcmp(data_, prefix, prefix_len) == 0;
119 }
120
121 bool starts_with(const std::string& prefix) const {
122 return starts_with(prefix.c_str());
123 }
124
125 // 检查是否以特定后缀结尾
126 bool ends_with(const char* suffix) const {
127 size_t suffix_len = strlen(suffix);
128 if (static_cast<size_t>(length_) < suffix_len) return false;
129 return memcmp(data_ + length_ - suffix_len, suffix, suffix_len) == 0;
130 }
131
132 bool ends_with(const std::string& suffix) const {
133 return ends_with(suffix.c_str());
134 }
135
136 // 友元类,允许IdentifierTable访问私有构造函数
137 friend class IdentifierTable;
138};
139
140
141AST_NAMESPACE_END
标识符类
定义 Identifier.hpp:38
static size_t hash(const char *data, size_t len)
计算哈希值
定义 Identifier.hpp:98
Unit h
小时
定义 Unit.cpp:469