Lua 的字符串 #
Lua 版本 5.3.4
1 字符串的数据结构
1.1 字符串分类
从 5.2.0版本开始,Lua 开始区分长字符串和短字符串,“长短”长度的标准定义在 llimits.h
#define LUAI_MAXSHORTLEN 40
“长短” 类型的定义在 lobject.h
/* Variant tags for strings */
#define LUA_TSHRSTR (LUA_TSTRING | (0 << 4)) /* short strings */
#define LUA_TLNGSTR (LUA_TSTRING | (1 << 4)) /* long strings */
1.2 字符串的结构
/*
** Header for string value; string bytes follow the end of this structure
** (aligned according to 'UTString'; see next).
** **字符串的头部,字符串的真正内容在这个结构后面**
*/
typedef struct TString {
CommonHeader;
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
/* 对于短字符串:这个标示是否是保留字,长字符串:是否已经哈希① */
lu_byte shrlen; /* 短字符串的长度 */
unsigned int hash;
union {
size_t lnglen; /* 长字符串的长度 */
struct TString *hnext; /* 短字符串:linked list for hash table */
} u;
} TString;
/*
** Ensures that address after this type is always fully aligned.
*/
typedef union UTString {
L_Umaxalign dummy; /* 内存对齐 */
TString tsv;
} UTString;
补充1: 字符串申请内存大小
#define sizelstring(l) (sizeof(union UTString) + ((l) + 1) * sizeof(char))
说明①: 长字符串是惰性求hast值
unsigned int luaS_hashlongstr (TString *ts) {
lua_assert(ts->tt == LUA_TLNGSTR);
if (ts->extra == 0) { /* no hash? */
ts->hash = luaS_hash(getstr(ts), ts->u