🛰️航天仿真算法库 SpaceAST 0.3.1
载入中...
搜索中...
未找到
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
73 // 转换为std::string
74 std::string toString() const {
75 return std::string(data_, length_);
76 }
77
78 // 比较操作
79 bool equals(const char* str, size_t len) const {
80 if (len != length_) return false;
81 return memcmp(data_, str, len) == 0;
82 }
83
84 bool equals(const char* str) const {
85 return equals(str, std::char_traits<char>::length(str));
86 }
87
88 bool equals(const std::string& str) const {
89 return equals(str.c_str(), str.length());
90 }
91
92 bool equals(StringView sv) const {
93 return equals(sv.data(), sv.size());
94 }
95
97 static size_t hash(const char* data, size_t len) {
98 // DJB2哈希算法
99 size_t h = 5381;
100 for (size_t i = 0; i < len; ++i) {
101 h = ((h << 5) + h) + static_cast<unsigned char>(data[i]);
102 }
103 return h;
104 }
105
106 // 计算哈希值
107 size_t hash() const {
108 // DJB2哈希算法
109 return hash(data_, length_);
110 }
111
112
113 // 检查是否以特定前缀开头
114 bool starts_with(const char* prefix) const {
115 size_t prefix_len = strlen(prefix);
116 if (static_cast<size_t>(length_) < prefix_len) return false;
117 return memcmp(data_, prefix, prefix_len) == 0;
118 }
119
120 bool starts_with(const std::string& prefix) const {
121 return starts_with(prefix.c_str());
122 }
123
124 // 检查是否以特定后缀结尾
125 bool ends_with(const char* suffix) const {
126 size_t suffix_len = strlen(suffix);
127 if (static_cast<size_t>(length_) < suffix_len) return false;
128 return memcmp(data_ + length_ - suffix_len, suffix, suffix_len) == 0;
129 }
130
131 bool ends_with(const std::string& suffix) const {
132 return ends_with(suffix.c_str());
133 }
134
135 // 友元类,允许IdentifierTable访问私有构造函数
136 friend class IdentifierTable;
137};
138
139
140AST_NAMESPACE_END
标识符类
定义 Identifier.hpp:38
static size_t hash(const char *data, size_t len)
计算哈希值
定义 Identifier.hpp:97
Unit h
小时
定义 Unit.cpp:469