<HTML><BODY><br><br><br>-------- Пересылаемое сообщение --------<br>
От кого: Nikita Tatunov <n.tatunov@tarantool.org><br>
Кому: Vladislav Shpilevoy <v.shpilevoy@tarantool.org><br>
Копия: tarantool-patches@freelists.org, Alexander Turenko <alexander.turenko@tarantool.org><br>
Дата: Четверг,  9 августа 2018, 13:08 +03:00<br>
Тема: Re[3]: [PATCH] lua: add string.fromhex method<br>
<br>







<div class="js-helper js-readmsg-msg">
        <style type="text/css"></style>
        <div>
                <base target="_self" href="https://e.mail.ru/">
                
            <div id="style_15338093120000000358_BODY"><div class="class_1533851839">
Diff:<br><br><p>diff --git a/src/lua/string.lua b/src/lua/string.lua<br>index 5ff64c9f6..f6accb42c 100644<br>--- a/src/lua/string.lua<br>+++ b/src/lua/string.lua<br>@@ -292,6 +292,54 @@ local function string_hex(inp)<br> return ffi.string(res, len)<br> end<br> <br>+local hexadecimal_chars = {<br>+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',<br>+ 'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f'}<br>+<br>+local hexadecimal_values = {<br>+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,<br>+ 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}<br>+<br>+local hexadecimals_mapping = {}<br>+<br>+local function chars_to_hex(chars, values, mapping)<br>+ for i, char in ipairs(chars) do<br>+ mapping[string.byte(char)] = values[i]<br>+ end<br>+end<br>+<br>+chars_to_hex(hexadecimal_chars, hexadecimal_values, hexadecimals_mapping)<br>+<br>+--- Match a hexadecimal representation of a string to its<br>+-- string representation.<br>+-- @function fromhex<br>+-- @string inp the string of hexadecimals<br>+-- @returns formatted string<br>+local function string_fromhex(inp)<br>+ if type(inp) ~= 'string' then<br>+ error(err_string_arg:format(1, 'string.fromhex', 'string',<br>+ type(inp)), 2)<br>+ end<br>+ if inp:len() % 2 ~= 0 then<br>+ error(err_string_arg:format(1, 'string.fromhex',<br>+ 'even amount of chars',<br>+ 'odd amount'), 2)<br>+ end<br>+ local len = inp:len() / 2<br>+ local casted_inp = ffi.cast('const char *', inp)<br>+ local res = ffi.new('char[?]', len)<br>+ for i = 0, len - 1 do<br>+ local first = hexadecimals_mapping[casted_inp[i * 2]]<br>+ local second = hexadecimals_mapping[casted_inp[i * 2 + 1]]<br>+ if first == nil or second == nil then<br>+ error(err_string_arg:format(1, 'string.fromhex', 'hex string',<br>+ 'non hex chars'), 2)<br>+ end<br>+ res[i] = first * 16 + second<br>+ end<br>+ return ffi.string(res, len)<br>+end<br>+<br> local function string_strip(inp)<br> if type(inp) ~= 'string' then<br> error(err_string_arg:format(1, "string.strip", 'string', type(inp)), 2)<br>@@ -323,6 +371,7 @@ string.center = string_center<br> string.startswith = string_startswith<br> string.endswith = string_endswith<br> string.hex = string_hex<br>+string.fromhex = string_fromhex<br> string.strip = string_strip<br> string.lstrip = string_lstrip<br> string.rstrip = string_rstrip<br>diff --git a/test/app-tap/string.test.lua b/test/app-tap/string.test.lua<br>index 1d10dcfc9..32333bff1 100755<br>--- a/test/app-tap/string.test.lua<br>+++ b/test/app-tap/string.test.lua<br>@@ -3,7 +3,7 @@<br> local tap = require('tap')<br> local test = tap.test("string extensions")<br> <br>-test:plan(6)<br>+test:plan(7)<br> <br> test:test("split", function(test)<br> test:plan(10)<br>@@ -114,6 +114,25 @@ test:test("hex", function(test)<br> test:is(string.hex(""), "", "hex empty string")<br> end)<br> <br>+test:test("fromhex", function(test)<br>+ test:plan(11)<br>+ test:is(string.fromhex("48656c6c6f"), "Hello", "from hex to bin")<br>+ test:is(string.fromhex("4c696e7578"), "Linux", "from hex to bin")<br>+ test:is(string.fromhex("6C6F72656D"), "lorem", "from hex to bin")<br>+ test:is(string.fromhex("697073756D"), "ipsum", "from hex to bin")<br>+ test:is(string.fromhex("6c6f72656d"), "lorem", "from hex to bin")<br>+ test:is(string.fromhex("697073756d"), "ipsum", "from hex to bin")<br>+ test:is(string.fromhex("6A6B6C6D6E6F"), "jklmno", "from hex to bin")<br>+ test:is(string.fromhex("6a6b6c6d6e6f"), "jklmno", "from hex to bin")<br>+ local _, err = pcall(string.fromhex, "aaa")<br>+ test:ok(err and err:match("(even amount of chars expected," ..<br>+ " got odd amount)"))<br>+ local _, err = pcall(string.fromhex, "qq")<br>+ test:ok(err and err:match("(hex string expected, got non hex chars)"))<br>+ local _, err = pcall(string.fromhex, 795)<br>+ test:ok(err and err:match("(string expected, got number)"))<br>+end)<br>+<br> test:test("strip", function(test)<br> test:plan(6)<br> local str = " hello hello "</p><br><style></style>
</div></div>
            
        
                <base target="_self" href="https://e.mail.ru/">
        </div>

        
</div>



<br><hr>
<br>-- <br>WBR, Nikita Tatunov.<br></BODY></HTML>