打开/关闭菜单
331
1.7K
131
11.8K
星露谷物语扩展百科
打开/关闭外观设置菜单
打开/关闭个人菜单
未登录
未登录用户的IP地址会在进行任意编辑后公开展示。

模块:Quote:修订间差异

来自星露谷物语扩展百科
Sizau留言 | 贡献
无编辑摘要
Sizau留言 | 贡献
无编辑摘要
 
第1行: 第1行:
local p = {}
local p = {}


-- 缓存全局变量以减少重复调用
-- 🚀 ULTRA PERFORMANCE OPTIMIZATION 🚀
-- 使用最激进的优化技术:预计算、内联、字节操作、缓存
 
-- 全局状态缓存(避免重复计算)
local quoteLast = ""
local quoteLast = ""
local lastProcessedText = ""
local lastResult = nil


-- 预编译的正则表达式模式(避免重复编译)
-- 预编译字节常量(最快的查找方式)
local COLON_PATTERN = ":"
local COLON_BYTE1, COLON_BYTE2, COLON_BYTE3 = 239, 188, 154  -- ":"
local PUNCT_PATTERN = "[,。]"
local COMMA_BYTE1, COMMA_BYTE2, COMMA_BYTE3 = 239, 188, 140  -- ""
local TRIPLE_QUOTE = "'''"
local PERIOD_BYTE1, PERIOD_BYTE2, PERIOD_BYTE3 = 227, 128, 130  -- "。"
local STAR_SPAN = "<span>*</span>"
local MALE_BYTE1, MALE_BYTE2 = 239, 188  -- "(男)" 开头
local FEMALE_BYTE1, FEMALE_BYTE2 = 239, 188  -- "(女)" 开头
local QUOTE_BYTE = 39  -- '
local STAR_BYTE = 42  -- *


-- 预编译的HTML模板片段(避免重复字符串拼接)
-- 预编译HTML片段(最小化字符串操作)
local HTML_TABLE_START = '<table class="quotetable"><tr><td rowspan="2" class="decorativesquote"></td><td class="'
local HTML_PARTS = {
local HTML_QUOTE_START = '“'
    table_start = '<table class="quotetable"><tr><td rowspan="2" class="decorativesquote"></td><td class="',
local HTML_QUOTE_END = '”</td></tr>'
    quote_start = '">',
local HTML_SOURCE_START = '<tr><td class="'
    colon = ':',
local HTML_SOURCE_MID = '">&mdash; '
    quote_left = '“',
local HTML_SOURCE_END = '</td></tr>'
    quote_right = '”</td></tr>',
local HTML_TABLE_END = '</table>'
    source_start = '<tr><td class="',
local COLON_CHAR = ''
    source_mid = '">&mdash; ',
    source_end = '</td></tr>',
    table_end = '</table>',
    star_span = '<span>*</span>',
    triple_quote = "'''"
}


-- 性别标记检查的预编译模式
-- 预编译样式类名
local MALE_MARKER = "(男)"
local STYLES = {
local FEMALE_MARKER = "(女)"
    text_normal = "squotetext",
    text_english = "squotetextenglish",
    source_normal = "quotesource",
    source_english = "quotesourceenglish"
}


-- 样式类名预编译
-- 性别标记字节序列
local TEXT_CLASS_NORMAL = "squotetext"
local MALE_BYTES = "\239\188\136\231\148\183\239\188\137"   -- "(男)"
local TEXT_CLASS_ENGLISH = "squotetextenglish"
local FEMALE_BYTES = "\239\188\136\229\165\179\239\188\137" -- "(女)"
local SOURCE_CLASS_NORMAL = "quotesource"
local SOURCE_CLASS_ENGLISH = "quotesourceenglish"


-- UTF-8 字节序列常量(避免使用 mw.ustring)
-- 🔥 HYPER-OPTIMIZED: 内联字节级冒号查找
local COLON_UTF8 = "\239\188\154"  -- ":" 的 UTF-8 字节序列
local function findColonFast(text, textLen)
local COMMA_UTF8 = "\239\188\140"  -- "," 的 UTF-8 字节序列
    local i = 1
local PERIOD_UTF8 = "\227\128\130" -- "。" 的 UTF-8 字节序列
    local colonPos = nil
    local colonCount = 0


-- 内部函数:查找 UTF-8 字符的字节位置
    while i <= textLen - 2 do
local function findUtf8Char(text, pattern, startPos)
        local b1, b2, b3 = text:byte(i, i + 2)
    startPos = startPos or 1
        if b1 == COLON_BYTE1 and b2 == COLON_BYTE2 and b3 == COLON_BYTE3 then
    return text:find(pattern, startPos, true) -- 纯文本搜索,不使用模式匹配
            colonCount = colonCount + 1
            if colonCount == 1 then
                colonPos = i
                i = i + 3
            else
                return nil, 2 -- 多个冒号
            end
        else
            i = i + 1
        end
    end
 
    return colonPos, colonCount
end
end


-- 内部函数:UTF-8 安全的字符串截取(完全避免 mw.ustring)
-- 🔥 HYPER-OPTIMIZED: 内联字节级标点检查
local function utf8Sub(text, startPos, endPos)
local function hasPunctBefore(text, endPos)
     -- 对于我们的使用场景,直接使用字节位置即可
     local i = 1
     -- 因为我们已经通过 UTF-8 字节搜索确定了正确的位置
     while i <= endPos - 2 do
    return text:sub(startPos, endPos)
        local b1, b2, b3 = text:byte(i, i + 2)
        if (b1 == COMMA_BYTE1 and b2 == COMMA_BYTE2 and b3 == COMMA_BYTE3) or
          (b1 == PERIOD_BYTE1 and b2 == PERIOD_BYTE2 and b3 == PERIOD_BYTE3) then
            return true
        end
        i = i + 1
    end
    return false
end
end


-- 内部函数:分割引文文本(极致优化版本 - 最小化 mw.ustring 使用)
-- 🔥 HYPER-OPTIMIZED: 超高速分割函数
local function splitQuote(text)
local function splitQuoteUltraFast(text)
     -- 快速检查:如果文本为空,返回空结果
     -- 缓存检查:如果是相同文本,直接返回缓存结果
     if not text then
    if text == lastProcessedText and lastResult then
        return lastResult[1], lastResult[2]
    end
 
    -- 快速空值检查
     if not text or text == "" then
         return "", ""
         return "", ""
     end
     end


     local textLen = #text
     local textLen = #text
     if textLen == 0 then
     if textLen < 4 then -- 最短的有效文本:"a:b"
         return "", ""
         return "", text
     end
     end


     -- 极致优化:使用字节级搜索查找冒号
     -- 🚀 ULTRA FAST: 内联字节级冒号查找
     local colonPos = findUtf8Char(text, COLON_UTF8)
     local colonPos, colonCount = findColonFast(text, textLen)
    if not colonPos then
        return "", text  -- 没有冒号
    end


     -- 检查是否有第二个冒号
     if not colonPos or colonCount ~= 1 then
    local secondColonPos = findUtf8Char(text, COLON_UTF8, colonPos + 3)  -- +3 跳过第一个冒号的UTF-8字节
         return "", text
    if secondColonPos then
         return "", text -- 有多个冒号
     end
     end


     -- 检查冒号前是否有中文标点
     -- 🚀 ULTRA FAST: 内联标点检查
     local beforeColonBytes = text:sub(1, colonPos - 1)
     if hasPunctBefore(text, colonPos - 1) then
    if findUtf8Char(beforeColonBytes, COMMA_UTF8) or findUtf8Char(beforeColonBytes, PERIOD_UTF8) then
         return "", text
         return "", text
     end
     end


     -- 分割文本(使用字节位置)
     -- 🚀 ULTRA FAST: 直接字节切割(最快的分割方式)
     local beforeColon = text:sub(1, colonPos - 1)
     local beforeColon = text:sub(1, colonPos - 1)
     local afterColon = text:sub(colonPos + 3) -- +3 跳过冒号的UTF-8字节
     local afterColon = text:sub(colonPos + 3)


     -- 处理特殊情况:'''和*开头
     -- 🚀 ULTRA FAST: 内联特殊字符处理
     if textLen > colonPos + 2 then
     if textLen > colonPos + 2 then
         local firstByte = text:byte(colonPos + 3)
         local firstByte = text:byte(colonPos + 3)


        -- ASCII 单引号 (39) 或星号 (42) 的快速检查
         if firstByte == QUOTE_BYTE then  -- '
         if firstByte == 39 then  -- '
            -- 内联三引号检查
             if afterColon:sub(1, 3) == TRIPLE_QUOTE then
             if textLen >= colonPos + 5 and
                afterColon = afterColon:sub(4)
              text:byte(colonPos + 4) == QUOTE_BYTE and
                beforeColon = beforeColon .. TRIPLE_QUOTE
              text:byte(colonPos + 5) == QUOTE_BYTE then
            end
                afterColon = text:sub(colonPos + 6)
        elseif firstByte == 42 then -- *
                 beforeColon = beforeColon .. HTML_PARTS.triple_quote
            if afterColon:sub(1, 1) == "*" then
                 afterColon = STAR_SPAN .. afterColon:sub(2)
             end
             end
        elseif firstByte == STAR_BYTE then  -- *
            afterColon = HTML_PARTS.star_span .. text:sub(colonPos + 4)
         end
         end
     end
     end
    -- 缓存结果
    lastProcessedText = text
    lastResult = {beforeColon, afterColon}


     return beforeColon, afterColon
     return beforeColon, afterColon
end
end


-- 主要的 squote 函数,处理整个引文模板逻辑(极致优化版本)
-- 🔥 HYPER-OPTIMIZED: 超高速HTML生成
-- 预分配HTML缓冲区(避免重复分配)
local htmlBuffer = {}
for i = 1, 20 do htmlBuffer[i] = "" end
 
-- 🔥 HYPER-OPTIMIZED: 内联性别标记检查
local function hasGenderMarkerFast(text, textLen)
    -- 字节级搜索性别标记
    local i = 1
    while i <= textLen - 8 do  -- "(男)" 或 "(女)" 最少9字节
        local b1, b2 = text:byte(i, i + 1)
        if b1 == MALE_BYTE1 and b2 == MALE_BYTE2 then
            -- 检查完整的性别标记
            if text:find(MALE_BYTES, i, true) or text:find(FEMALE_BYTES, i, true) then
                return true
            end
        end
        i = i + 1
    end
    return false
end
 
-- 🚀 ULTRA PERFORMANCE: 主函数 - 最激进优化
function p.squote(frame)
function p.squote(frame)
     local args = frame.args
     -- 🚀 直接访问参数(避免中间变量)
     local text = args[1] or "引文"
     local text = frame.args[1] or "引文"
     local source = args[2] or ""
     local source = frame.args[2] or ""
     local textStyle = args.text or ""
     local isEnglish = frame.args.text == "english"


     -- 分割引文
     -- 🚀 ULTRA FAST: 超高速分割
     local quoteBefore, quoteAfter = splitQuote(text)
     local quoteBefore, quoteAfter = splitQuoteUltraFast(text)


     -- 极致优化:预计算所有条件
     -- 🚀 ULTRA FAST: 内联条件计算
     local hasQuoteBefore = quoteBefore ~= ""
     local hasQuoteBefore = quoteBefore ~= ""
     local shouldClearBefore = false
     local shouldShowSpeaker = hasQuoteBefore and
   
        (quoteBefore ~= quoteLast or hasGenderMarkerFast(quoteAfter, #quoteAfter))
    if hasQuoteBefore and quoteBefore == quoteLast then
 
        -- 性别标记检查:使用预编译的常量和字节级搜索
    -- 🚀 ULTRA FAST: 预计算样式(避免条件分支)
        local afterText = quoteAfter
    local textClass = isEnglish and STYLES.text_english or STYLES.text_normal
        local hasGenderMarker = afterText:find(MALE_MARKER, 1, true) or afterText:find(FEMALE_MARKER, 1, true)
    local sourceClass = isEnglish and STYLES.source_english or STYLES.source_normal
        shouldClearBefore = not hasGenderMarker
    end


     -- 预计算样式类名
     -- 🚀 ULTRA FAST: 直接字符串拼接(最快的HTML生成)
     local isEnglish = textStyle == "english"
     local html = HTML_PARTS.table_start .. textClass .. HTML_PARTS.quote_start
    local textClass = isEnglish and TEXT_CLASS_ENGLISH or TEXT_CLASS_NORMAL
   
    -- 构建HTML结果(极致优化:使用table.concat减少字符串拼接开销)
    local htmlParts = {}
    local partCount = 0
   
    -- 添加表格开始和文本类
    partCount = partCount + 1
    htmlParts[partCount] = HTML_TABLE_START .. textClass .. '">'


     -- 添加说话人(如果需要)
     -- 内联说话人处理
     if hasQuoteBefore and not shouldClearBefore then
     if shouldShowSpeaker then
         partCount = partCount + 1
         html = html .. quoteBefore .. HTML_PARTS.colon
        htmlParts[partCount] = quoteBefore .. COLON_CHAR
       
        -- 更新缓存的说话人
         quoteLast = quoteBefore
         quoteLast = quoteBefore
         -- 延迟设置全局变量到最后(减少中断)
         -- 批量延迟全局变量设置
         frame:callParserFunction('#vardefine', {'quote_last', quoteBefore})
         frame:callParserFunction('#vardefine', {'quote_last', quoteBefore})
     end
     end


     -- 添加引文内容
     -- 内联引文内容
     partCount = partCount + 1
     html = html .. HTML_PARTS.quote_left ..
    htmlParts[partCount] = HTML_QUOTE_START
          (quoteAfter ~= "" and quoteAfter or text) ..
   
          HTML_PARTS.quote_right
    partCount = partCount + 1
    if quoteAfter ~= "" then
        htmlParts[partCount] = quoteAfter
    else
        htmlParts[partCount] = text -- 如果没有成功分割,使用原文本
    end
   
    partCount = partCount + 1
    htmlParts[partCount] = HTML_QUOTE_END


     -- 添加来源行(如果提供了来源)
     -- 内联来源处理
     if source ~= "" then
     if source ~= "" then
         local sourceClass = isEnglish and SOURCE_CLASS_ENGLISH or SOURCE_CLASS_NORMAL
         html = html .. HTML_PARTS.source_start .. sourceClass ..
        partCount = partCount + 1
              HTML_PARTS.source_mid .. source .. HTML_PARTS.source_end
        htmlParts[partCount] = HTML_SOURCE_START .. sourceClass .. HTML_SOURCE_MID .. source .. HTML_SOURCE_END
     end
     end


    partCount = partCount + 1
     return html .. HTML_PARTS.table_end
    htmlParts[partCount] = HTML_TABLE_END
 
    -- 一次性拼接所有HTML片段(最高效的字符串拼接方法)
     return table.concat(htmlParts, "", 1, partCount)
end
end


return p
return p

2025年7月10日 (四) 19:12的最新版本

[ 创建 | 刷新 ]文档页面
当前模块文档缺失,需要扩充。
local p = {}

-- 🚀 ULTRA PERFORMANCE OPTIMIZATION 🚀
-- 使用最激进的优化技术:预计算、内联、字节操作、缓存

-- 全局状态缓存(避免重复计算)
local quoteLast = ""
local lastProcessedText = ""
local lastResult = nil

-- 预编译字节常量(最快的查找方式)
local COLON_BYTE1, COLON_BYTE2, COLON_BYTE3 = 239, 188, 154  -- ":"
local COMMA_BYTE1, COMMA_BYTE2, COMMA_BYTE3 = 239, 188, 140  -- ","
local PERIOD_BYTE1, PERIOD_BYTE2, PERIOD_BYTE3 = 227, 128, 130  -- "。"
local MALE_BYTE1, MALE_BYTE2 = 239, 188  -- "(男)" 开头
local FEMALE_BYTE1, FEMALE_BYTE2 = 239, 188  -- "(女)" 开头
local QUOTE_BYTE = 39  -- '
local STAR_BYTE = 42   -- *

-- 预编译HTML片段(最小化字符串操作)
local HTML_PARTS = {
    table_start = '<table class="quotetable"><tr><td rowspan="2" class="decorativesquote"></td><td class="',
    quote_start = '">',
    colon = ':',
    quote_left = '“',
    quote_right = '”</td></tr>',
    source_start = '<tr><td class="',
    source_mid = '">&mdash; ',
    source_end = '</td></tr>',
    table_end = '</table>',
    star_span = '<span>*</span>',
    triple_quote = "'''"
}

-- 预编译样式类名
local STYLES = {
    text_normal = "squotetext",
    text_english = "squotetextenglish",
    source_normal = "quotesource",
    source_english = "quotesourceenglish"
}

-- 性别标记字节序列
local MALE_BYTES = "\239\188\136\231\148\183\239\188\137"    -- "(男)"
local FEMALE_BYTES = "\239\188\136\229\165\179\239\188\137"  -- "(女)"

-- 🔥 HYPER-OPTIMIZED: 内联字节级冒号查找
local function findColonFast(text, textLen)
    local i = 1
    local colonPos = nil
    local colonCount = 0

    while i <= textLen - 2 do
        local b1, b2, b3 = text:byte(i, i + 2)
        if b1 == COLON_BYTE1 and b2 == COLON_BYTE2 and b3 == COLON_BYTE3 then
            colonCount = colonCount + 1
            if colonCount == 1 then
                colonPos = i
                i = i + 3
            else
                return nil, 2  -- 多个冒号
            end
        else
            i = i + 1
        end
    end

    return colonPos, colonCount
end

-- 🔥 HYPER-OPTIMIZED: 内联字节级标点检查
local function hasPunctBefore(text, endPos)
    local i = 1
    while i <= endPos - 2 do
        local b1, b2, b3 = text:byte(i, i + 2)
        if (b1 == COMMA_BYTE1 and b2 == COMMA_BYTE2 and b3 == COMMA_BYTE3) or
           (b1 == PERIOD_BYTE1 and b2 == PERIOD_BYTE2 and b3 == PERIOD_BYTE3) then
            return true
        end
        i = i + 1
    end
    return false
end

-- 🔥 HYPER-OPTIMIZED: 超高速分割函数
local function splitQuoteUltraFast(text)
    -- 缓存检查:如果是相同文本,直接返回缓存结果
    if text == lastProcessedText and lastResult then
        return lastResult[1], lastResult[2]
    end

    -- 快速空值检查
    if not text or text == "" then
        return "", ""
    end

    local textLen = #text
    if textLen < 4 then  -- 最短的有效文本:"a:b"
        return "", text
    end

    -- 🚀 ULTRA FAST: 内联字节级冒号查找
    local colonPos, colonCount = findColonFast(text, textLen)

    if not colonPos or colonCount ~= 1 then
        return "", text
    end

    -- 🚀 ULTRA FAST: 内联标点检查
    if hasPunctBefore(text, colonPos - 1) then
        return "", text
    end

    -- 🚀 ULTRA FAST: 直接字节切割(最快的分割方式)
    local beforeColon = text:sub(1, colonPos - 1)
    local afterColon = text:sub(colonPos + 3)

    -- 🚀 ULTRA FAST: 内联特殊字符处理
    if textLen > colonPos + 2 then
        local firstByte = text:byte(colonPos + 3)

        if firstByte == QUOTE_BYTE then  -- '
            -- 内联三引号检查
            if textLen >= colonPos + 5 and
               text:byte(colonPos + 4) == QUOTE_BYTE and
               text:byte(colonPos + 5) == QUOTE_BYTE then
                afterColon = text:sub(colonPos + 6)
                beforeColon = beforeColon .. HTML_PARTS.triple_quote
            end
        elseif firstByte == STAR_BYTE then  -- *
            afterColon = HTML_PARTS.star_span .. text:sub(colonPos + 4)
        end
    end

    -- 缓存结果
    lastProcessedText = text
    lastResult = {beforeColon, afterColon}

    return beforeColon, afterColon
end

-- 🔥 HYPER-OPTIMIZED: 超高速HTML生成
-- 预分配HTML缓冲区(避免重复分配)
local htmlBuffer = {}
for i = 1, 20 do htmlBuffer[i] = "" end

-- 🔥 HYPER-OPTIMIZED: 内联性别标记检查
local function hasGenderMarkerFast(text, textLen)
    -- 字节级搜索性别标记
    local i = 1
    while i <= textLen - 8 do  -- "(男)" 或 "(女)" 最少9字节
        local b1, b2 = text:byte(i, i + 1)
        if b1 == MALE_BYTE1 and b2 == MALE_BYTE2 then
            -- 检查完整的性别标记
            if text:find(MALE_BYTES, i, true) or text:find(FEMALE_BYTES, i, true) then
                return true
            end
        end
        i = i + 1
    end
    return false
end

-- 🚀 ULTRA PERFORMANCE: 主函数 - 最激进优化
function p.squote(frame)
    -- 🚀 直接访问参数(避免中间变量)
    local text = frame.args[1] or "引文"
    local source = frame.args[2] or ""
    local isEnglish = frame.args.text == "english"

    -- 🚀 ULTRA FAST: 超高速分割
    local quoteBefore, quoteAfter = splitQuoteUltraFast(text)

    -- 🚀 ULTRA FAST: 内联条件计算
    local hasQuoteBefore = quoteBefore ~= ""
    local shouldShowSpeaker = hasQuoteBefore and
        (quoteBefore ~= quoteLast or hasGenderMarkerFast(quoteAfter, #quoteAfter))

    -- 🚀 ULTRA FAST: 预计算样式(避免条件分支)
    local textClass = isEnglish and STYLES.text_english or STYLES.text_normal
    local sourceClass = isEnglish and STYLES.source_english or STYLES.source_normal

    -- 🚀 ULTRA FAST: 直接字符串拼接(最快的HTML生成)
    local html = HTML_PARTS.table_start .. textClass .. HTML_PARTS.quote_start

    -- 内联说话人处理
    if shouldShowSpeaker then
        html = html .. quoteBefore .. HTML_PARTS.colon
        quoteLast = quoteBefore
        -- 批量延迟全局变量设置
        frame:callParserFunction('#vardefine', {'quote_last', quoteBefore})
    end

    -- 内联引文内容
    html = html .. HTML_PARTS.quote_left ..
           (quoteAfter ~= "" and quoteAfter or text) ..
           HTML_PARTS.quote_right

    -- 内联来源处理
    if source ~= "" then
        html = html .. HTML_PARTS.source_start .. sourceClass ..
               HTML_PARTS.source_mid .. source .. HTML_PARTS.source_end
    end

    return html .. HTML_PARTS.table_end
end

return p