From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTP id 1D92822B22 for ; Mon, 7 May 2018 10:35:07 -0400 (EDT) Received: from turing.freelists.org ([127.0.0.1]) by localhost (turing.freelists.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id oP0KLkVxz22w for ; Mon, 7 May 2018 10:35:07 -0400 (EDT) Received: from smtp15.mail.ru (smtp15.mail.ru [94.100.176.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by turing.freelists.org (Avenir Technologies Mail Multiplex) with ESMTPS id 480D62270E for ; Mon, 7 May 2018 10:35:05 -0400 (EDT) From: "" (Redacted sender "gleb-skiba" for DMARC) Subject: [tarantool-patches] [PATCH] lua: Add string.decodehex method Date: Mon, 7 May 2018 17:34:55 +0300 Message-Id: <1525703695-13011-1-git-send-email-gleb-skiba@mail.ru> Sender: tarantool-patches-bounce@freelists.org Errors-to: tarantool-patches-bounce@freelists.org Reply-To: tarantool-patches@freelists.org List-help: List-unsubscribe: List-software: Ecartis version 1.0.0 List-Id: tarantool-patches List-subscribe: List-owner: List-post: List-archive: To: v.shpilevoy@tarantool.org Cc: tarantool-patches@freelists.org, Gleb From: Gleb Add string.fromhex method. Add test for string.fromhex(). Fixes #2562 --- Issue from https://github.com/tarantool/tarantool/issues/2562. Source from https://github.com/tarantool/tarantool/compare/gh-2562-string-bin?expand=1. src/lua/string.lua | 30 ++++++++++++++++++++++++++++++ test/app-tap/string.test.lua | 12 +++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/lua/string.lua b/src/lua/string.lua index 5ff64c9..c2a7198 100644 --- a/src/lua/string.lua +++ b/src/lua/string.lua @@ -292,6 +292,35 @@ local function string_hex(inp) return ffi.string(res, len) end +local tonum = {[48] = 0, [49] = 1, [50] = 2, [51] = 3, [52] = 4, [53] = 5, +[54] = 6, [55] = 7, [56] = 8, [57] = 9, [97] = 10, [98] = 11, +[99] = 12, [100] = 13, [101] = 14, [102] = 15, [65] = 10, [66] = 11, +[67] = 12, [68] = 13, [69] = 14, [70] = 15 } + +local function string_fromhex(inp) + if type(inp) ~= 'string' then + error(err_string_arg:format(1, 'string.fromhex', 'string', type(inp)), 2) + end + + if inp:len() % 2 ~= 0 then + error(err_string_arg:format(1, 'string.fromhex', 'even string', 'odd string'), 2) + end + + local len = inp:len() / 2 + local uinp = ffi.cast('const char *', inp) + local ans = ffi.new('char[?]', len) + + for i = 0, len - 1 do + local first = tonum[uinp[i * 2]] + local second = tonum[uinp[i * 2 + 1]] + if ((first == nil) or (second == nil)) then + error(err_string_arg:format(1, 'string.fromhex', 'hex string', 'not hex string'), 2) + end + ans[i] = first * 16 + second + end + return ffi.string(ans, len) +end + local function string_strip(inp) if type(inp) ~= 'string' then error(err_string_arg:format(1, "string.strip", 'string', type(inp)), 2) @@ -323,6 +352,7 @@ string.center = string_center string.startswith = string_startswith string.endswith = string_endswith string.hex = string_hex +string.fromhex = string_fromhex string.strip = string_strip string.lstrip = string_lstrip string.rstrip = string_rstrip diff --git a/test/app-tap/string.test.lua b/test/app-tap/string.test.lua index 852a792..685bb74 100755 --- a/test/app-tap/string.test.lua +++ b/test/app-tap/string.test.lua @@ -3,7 +3,7 @@ local tap = require('tap') local test = tap.test("string extensions") -test:plan(5) +test:plan(6) test:test("split", function(test) test:plan(10) @@ -114,6 +114,16 @@ test:test("hex", function(test) test:is(string.hex(""), "", "hex empty string") end) +test:test("fromhex", function(test) + test:plan(4) + test:is(string.fromhex("48656c6c6f"), "Hello", "from hex to bin") + test:is(string.fromhex("4c696e7578"), "Linux", "from hex to bin") + local _, err = pcall(function() string.fromhex("aaa") end) + test:ok(err and err:match("(even string expected, got odd string)"), err) + _, err = pcall(function() string.fromhex("qq") end) + test:ok(err and err:match("(hex string expected, got not hex string)"), err) +end) + test:test("strip", function(test) test:plan(6) local str = " hello hello " -- 2.7.4