--[[**expack1.lua*****************Copyright(c)******************************
**                Guangzhou DACAIGAUNGDIAN Technology Co. Ltd
**
**                             http://www.gz-dc.com
**
**------File Info-----------------------------------------------------------
** File Name:               expack1.lua
** Latest modified Date:    2022/09/01
** Latest Version:          
** Description:             各种扩展功能
**
**--------------------------------------------------------------------------
** Created By:              
** Created date:            2022/09/01
** Version:                 V0.1
** Descriptions:            
**
************************************************************************--]]
_ex={}

--字符串分割
function _ex.my_split(str, pat)
    local t = {}
    local last_end = 0
    local s, e = string.find(str, pat, 1)--  第一个分割号的位置
    while s 
    do
        -- 找出分割符的前的参数
        table.insert(t, string.sub(str, last_end + 1, last_end + s - last_end - 1))
        last_end = e
        s, e = string.find(str, pat, last_end + 1)
    end
    if last_end <= #str and  last_end ~= 0 then
        cap = string.sub(str, last_end + 1)
        table.insert(t, cap)
    end
    return t
end

function _ex.my_tonumber(hexstr)
    local num = 0
    local hex_len = string.len(hexstr)
    for i = 1, string.len(hexstr)
    do
        local _char = string.byte(string.sub(hexstr, (hex_len - i + 1), (hex_len - i + 1))) 
        if _char >= 0x61 and _char <= 0x66
        then
            _char = _char - 0x57
        elseif _char >= 0x30 and _char <= 0x39
        then
            _char = _char - 0x30
        elseif _char >= 0x65 and _char <= 0x6A
        then
            _char = _char - 0x5B
        else
            num = 0
            break
        end
        for j = 1, (i-1)
        do
            if i == 1
            then
                _char = _char * 1
            else
                _char = _char*16
            end
        end
        num = num + _char
    end
    return num
end

--[[***************************************************************************
** Function name:  e180.table2str
** Descriptions :  把表转换成字符串
***************************************************************************--]]
function _ex.table2str(tbl)    
    if tbl==nil 
    then return '' end
    local str = ''
    local count = #(tbl)
    for i=0,count do    
      ch = string.char(tbl[i])
      str = str .. ch
    end
    return str
end

--[[***************************************************************************
** Function name:  table2str16
** Descriptions :  把表转换成十六进制形式的字符串
***************************************************************************--]]
function _ex.table2str16(tbl)
    if tbl==nil
    then return '' end
    local str = ''
    local count = #(tbl)
    for i=0,count do
        ch = string.format('%02X',tbl[i])
        str = str..' '..ch
    end
    return str
end

function _ex.recv_remove(recv_data,rem_num)
    local data_len = #recv_data
    local transfer_num = data_len-rem_num
    if data_len>=rem_num
        and rem_num > 0
        and recv_data[0] ~= nil
    then
        for x=0,data_len
        do
            if x < transfer_num
            then
                recv_data[x] = recv_data[rem_num+x+1]
            else
                recv_data[x] = nil
            end
        end
    end
end

--查表，在table2中找table1
--table2：待查询的表
--table1：要找的表
function _ex.contrast_table(table2,table1)
    if table2[0]==nil or table1[0]==nil 
    then
        return false
    end
    local value_long = #table2
    local ack_long = #table1
    local data_i = 0
    for i=0,value_long
    do
        if table1[data_i] == table2[i]
        then
            data_i = data_i + 1
        else
            data_i = 0
        end
        if data_i-1 == ack_long
        then
            return i-ack_long
        end
    end
    return false
end

