Tarantool development patches archive
 help / color / mirror / Atom feed
* [tarantool-patches] Fwd: Re[2]: [PATCH] lua: add string.fromhex method
@ 2018-08-09 18:35 Nikita Tatunov
  2018-08-09 19:00 ` [tarantool-patches] " Vladislav Shpilevoy
  2018-08-16  6:06 ` [tarantool-patches] " Kirill Yukhin
  0 siblings, 2 replies; 10+ messages in thread
From: Nikita Tatunov @ 2018-08-09 18:35 UTC (permalink / raw)
  To: tarantool-patches

[-- Attachment #1: Type: text/plain, Size: 3295 bytes --]




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


Hello, Alexander, thnx for review!
Vlad, could you please take a look?

>Четверг,  9 августа 2018, 10:06 +03:00 от Alexander Turenko <  alexander.turenko@tarantool.org >:
>
>Hi, Nikita!
>
>The implementation LGTM.
>
>See minor comments about the test below.
>
>Please, proceed the next review round with Vlad.
>
>WBR, Alexander Turenko.
>
>On Wed, Aug 08, 2018 at 03:21:03PM +0300, N.Tatunov wrote:
>> Add string.fromhex method. Add test for string.fromhex().
>> 
>> Closes #2562
>> ---
>> 
>> Issue:  https://github.com/tarantool/tarantool/issues/2562
>> Branch:  https://github.com/tarantool/tarantool/tree/N_Tatunov/gh-2562-fromhex-method
>>
>> <...>
>>
>> diff --git a/test/app-tap/string.test.lua b/test/app-tap/string.test.lua
>> index 1d10dcfc9..f88296fff 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(6)
>> +test:plan(7)
>> 
>>  test:test("split", function(test)
>>      test:plan(10)
>> @@ -114,6 +114,25 @@ test:test("hex", function(test)
>>      test:is(string.hex(""), "", "hex empty string")
>>  end)
>> 
>> +test:test("fromhex", function(test)
>> +    test:plan(11)
>> +    test:is(string.fromhex("48656c6c6f"), "Hello", "from hex to bin")
>> +    test:is(string.fromhex("4c696e7578"), "Linux", "from hex to bin")
>> +    test:is(string.fromhex("6C6F72656D"), "lorem", "from hex to bin")
>> +    test:is(string.fromhex("697073756D"), "ipsum", "from hex to bin")
>> +    test:is(string.fromhex("6c6f72656d"), "lorem", "from hex to bin")
>> +    test:is(string.fromhex("697073756d"), "ipsum", "from hex to bin")
>> +    test:is(string.fromhex("6A6B6C6D6E6F"), "jklmno", "from hex to bin")
>> +    test:is(string.fromhex("6a6b6c6d6e6f"), "jklmno", "from hex to bin")
>> +    local _, err = pcall(string.fromhex, 'aaa')
>
>Use double quotes when a file primarily uses this quotes type.
>

Fixed.

>> +    test:ok(err and err:match("(even amount of chars expected," ..
>> +                    " got odd amount)"), err)
>
>1. Indent is strange (don't get what is the rule).
>2. Don't use `err` for diagnostics (the message could can be unusable in case of an
>   error).

Didn't notice it, thnx.
>
>> +    local _, err = pcall(string.fromhex, 'qq')
>
>Single qutoes -> doule quotes.
>

Fixed.

>> +    test:ok(err and err:match("(hex string expected, got non hex chars)"), err)
>> +    local _, err = pcall(string.fromhex, 795)
>> +    test:ok(err and err:match("(string expected, got " .. type(795) .. ")"))
>
>type(795) -> number
>

Changed.

>> +end)
>> +
>>  test:test("strip", function(test)
>>      test:plan(6)
>>      local str = "  hello hello "
>> -- 
>> 2.15.2 (Apple Git-101.1)
>> 


-- 
WBR, Nikita Tatunov.

----------------------------------------------------------------------

-- 
WBR, Nikita Tatunov.

[-- Attachment #2: Type: text/html, Size: 4712 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re: Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-09 18:35 [tarantool-patches] Fwd: Re[2]: [PATCH] lua: add string.fromhex method Nikita Tatunov
@ 2018-08-09 19:00 ` Vladislav Shpilevoy
  2018-08-09 19:02   ` [tarantool-patches] Re[2]: [tarantool-patches] " Nikita Tatunov
  2018-08-16  6:06 ` [tarantool-patches] " Kirill Yukhin
  1 sibling, 1 reply; 10+ messages in thread
From: Vladislav Shpilevoy @ 2018-08-09 19:00 UTC (permalink / raw)
  To: tarantool-patches, Nikita Tatunov

Hi! I saw the message, please, give some time.

On 09/08/2018 21:35, Nikita Tatunov wrote:
> 
> 
> 
> -------- Пересылаемое сообщение --------
> От кого: Nikita Tatunov <n.tatunov@tarantool.org>
> Кому: Vladislav Shpilevoy <v.shpilevoy@tarantool.org>
> Копия: tarantool-patches@freelists.org, Alexander Turenko <alexander.turenko@tarantool.org>
> Дата: Четверг, 9 августа 2018, 13:07 +03:00
> Тема: Re[2]: [PATCH] lua: add string.fromhex method
> 
> 
> Hello, Alexander, thnx for review!
> Vlad, could you please take a look?
> 
>  >Четверг, 9 августа 2018, 10:06 +03:00 от Alexander Turenko < alexander.turenko@tarantool.org <mailto:alexander.turenko@tarantool.org> >:
>  >
>  >Hi, Nikita!
>  >
>  >The implementation LGTM.
>  >
>  >See minor comments about the test below.
>  >
>  >Please, proceed the next review round with Vlad.
>  >
>  >WBR, Alexander Turenko.
>  >
>  >On Wed, Aug 08, 2018 at 03:21:03PM +0300, N.Tatunov wrote:
>  >> Add string.fromhex method. Add test for string.fromhex().
>  >>
>  >> Closes #2562
>  >> ---
>  >>
>  >> Issue: https://github.com/tarantool/tarantool/issues/2562
>  >> Branch: https://github.com/tarantool/tarantool/tree/N_Tatunov/gh-2562-fromhex-method
>  >>
>  >> <...>
>  >>
>  >> diff --git a/test/app-tap/string.test.lua b/test/app-tap/string.test.lua
>  >> index 1d10dcfc9..f88296fff 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(6)
>  >> +test:plan(7)
>  >>
>  >> test:test("split", function(test)
>  >> test:plan(10)
>  >> @@ -114,6 +114,25 @@ test:test("hex", function(test)
>  >> test:is(string.hex(""), "", "hex empty string")
>  >> end)
>  >>
>  >> +test:test("fromhex", function(test)
>  >> + test:plan(11)
>  >> + test:is(string.fromhex("48656c6c6f"), "Hello", "from hex to bin")
>  >> + test:is(string.fromhex("4c696e7578"), "Linux", "from hex to bin")
>  >> + test:is(string.fromhex("6C6F72656D"), "lorem", "from hex to bin")
>  >> + test:is(string.fromhex("697073756D"), "ipsum", "from hex to bin")
>  >> + test:is(string.fromhex("6c6f72656d"), "lorem", "from hex to bin")
>  >> + test:is(string.fromhex("697073756d"), "ipsum", "from hex to bin")
>  >> + test:is(string.fromhex("6A6B6C6D6E6F"), "jklmno", "from hex to bin")
>  >> + test:is(string.fromhex("6a6b6c6d6e6f"), "jklmno", "from hex to bin")
>  >> + local _, err = pcall(string.fromhex, 'aaa')
>  >
>  >Use double quotes when a file primarily uses this quotes type.
>  >
> 
> Fixed.
> 
>  >> + test:ok(err and err:match("(even amount of chars expected," ..
>  >> + " got odd amount)"), err)
>  >
>  >1. Indent is strange (don't get what is the rule).
>  >2. Don't use `err` for diagnostics (the message could can be unusable in case of an
>  >   error).
> 
> Didn't notice it, thnx.
>  >
>  >> + local _, err = pcall(string.fromhex, 'qq')
>  >
>  >Single qutoes -> doule quotes.
>  >
> 
> Fixed.
> 
>  >> + test:ok(err and err:match("(hex string expected, got non hex chars)"), err)
>  >> + local _, err = pcall(string.fromhex, 795)
>  >> + test:ok(err and err:match("(string expected, got " .. type(795) .. ")"))
>  >
>  >type(795) -> number
>  >
> 
> Changed.
> 
>  >> +end)
>  >> +
>  >> test:test("strip", function(test)
>  >> test:plan(6)
>  >> local str = " hello hello "
>  >> --
>  >> 2.15.2 (Apple Git-101.1)
>  >>
> 
> 
> -- 
> WBR, Nikita Tatunov.
> 
> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> 
> -- 
> WBR, Nikita Tatunov.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re[2]: [tarantool-patches] Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-09 19:00 ` [tarantool-patches] " Vladislav Shpilevoy
@ 2018-08-09 19:02   ` Nikita Tatunov
  2018-08-13 14:46     ` [tarantool-patches] Re: Re[2]: " Vladislav Shpilevoy
  0 siblings, 1 reply; 10+ messages in thread
From: Nikita Tatunov @ 2018-08-09 19:02 UTC (permalink / raw)
  To: Vladislav Shpilevoy; +Cc: tarantool-patches

[-- Attachment #1: Type: text/plain, Size: 46 bytes --]

Sorry, Kirill asked to resend it to freelists.

[-- Attachment #2: Type: text/html, Size: 74 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re: Re[2]: Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-09 19:02   ` [tarantool-patches] Re[2]: [tarantool-patches] " Nikita Tatunov
@ 2018-08-13 14:46     ` Vladislav Shpilevoy
  2018-08-13 15:04       ` Alexander Turenko
  0 siblings, 1 reply; 10+ messages in thread
From: Vladislav Shpilevoy @ 2018-08-13 14:46 UTC (permalink / raw)
  To: tarantool-patches, Nikita Tatunov

Hi! Thanks for the patch!

See 3 comments below.

> commit c208a8173b2f7632c3432d03807490becfe7276b
> Author: N.Tatunov <n.tatunov@tarantool.org>
> Date:   Wed Aug 8 14:17:02 2018 +0300
> 
>     lua: add string.fromhex method
>     
>     Add string.fromhex method. Add test for string.fromhex().
>     
>     Closes #2562
> 
> diff --git a/src/lua/string.lua b/src/lua/string.lua
> index 5ff64c9f6..f6accb42c 100644
> --- a/src/lua/string.lua
> +++ b/src/lua/string.lua
> @@ -292,6 +292,54 @@ local function string_hex(inp)
>      return ffi.string(res, len)
>  end
>  
> +local hexadecimal_chars = {
> +    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
> +    'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f'}
> +
> +local hexadecimal_values = {
> +    0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
> +    10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}
> +
> +local hexadecimals_mapping = {}
> +
> +local function chars_to_hex(chars, values, mapping)
> +    for i, char in ipairs(chars) do
> +        mapping[string.byte(char)] = values[i]
> +    end
> +end

1. Why do you need this function to be called only once 1 line
below? Can you just inline it?

> +
> +chars_to_hex(hexadecimal_chars, hexadecimal_values, hexadecimals_mapping)
> +
> +--- Match a hexadecimal representation of a string to its
> +-- string representation.
> +-- @function fromhex
> +-- @string   inp     the string of hexadecimals
> +-- @returns          formatted string

2. I know that such comments style you got from other functions
of the file, but each line above contradicts with our official
code style, as I understand. Just for record.

> +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 amount of chars',
> +                                    'odd amount'), 2)
> +    end
> +    local len = inp:len() / 2
> +    local casted_inp = ffi.cast('const char *', inp)
> +    local res = ffi.new('char[?]', len)

3. Why '?'? You know that res will be exactly len bytes.

> +    for i = 0, len - 1 do
> +        local first = hexadecimals_mapping[casted_inp[i * 2]]
> +        local second = hexadecimals_mapping[casted_inp[i * 2 + 1]]
> +        if first == nil or second == nil then
> +            error(err_string_arg:format(1, 'string.fromhex', 'hex string',
> +                                        'non hex chars'), 2)
> +        end
> +        res[i] = first * 16 + second
> +    end
> +    return ffi.string(res, len)
> +end
> +
>  local function string_strip(inp)
>      if type(inp) ~= 'string' then
>          error(err_string_arg:format(1, "string.strip", 'string', type(inp)), 2)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re: Re[2]: Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-13 14:46     ` [tarantool-patches] Re: Re[2]: " Vladislav Shpilevoy
@ 2018-08-13 15:04       ` Alexander Turenko
  2018-08-15 14:01         ` [tarantool-patches] " Nikita Tatunov
  0 siblings, 1 reply; 10+ messages in thread
From: Alexander Turenko @ 2018-08-13 15:04 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Nikita Tatunov

[-- Attachment #1: Type: text/html, Size: 6086 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re: [tarantool-patches] Re: Re[2]: Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-13 15:04       ` Alexander Turenko
@ 2018-08-15 14:01         ` Nikita Tatunov
  2018-08-15 14:06           ` Vladislav Shpilevoy
  0 siblings, 1 reply; 10+ messages in thread
From: Nikita Tatunov @ 2018-08-15 14:01 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Vladislav Shpilevoy

Hello, thank you for the review!
See diff for src/lua/string.lua in the end of the letter.


>Понедельник, 13 августа 2018, 18:05 +03:00 от Alexander Turenko <alexander.turenko@tarantool.org>:
>
>Hi!
>
>One inline note is below.
>
>WBR, Alexander Turenko.
>
>On Aug 13, 2018 5:46 PM, Vladislav Shpilevoy < v.shpilevoy@tarantool.org > wrote:
>>Hi! Thanks for the patch!
>>
>>See 3 comments below.
>>
>>> commit c208a8173b2f7632c3432d03807490becfe7276b
>>> Author: N.Tatunov < n.tatunov@tarantool.org >
>>> Date:   Wed Aug 8 14:17:02 2018 +0300
>>> 
>>>     lua: add string.fromhex method
>>>     
>>>     Add string.fromhex method. Add test for string.fromhex().
>>>     
>>>     Closes #2562
>>> 
>>> diff --git a/src/lua/string.lua b/src/lua/string.lua
>>> index 5ff64c9f6..f6accb42c 100644
>>> --- a/src/lua/string.lua
>>> +++ b/src/lua/string.lua
>>> @@ -292,6 +292,54 @@ local function string_hex(inp)
>>>      return ffi.string(res, len)
>>>  end
>>>  
>>> +local hexadecimal_chars = {
>>> +    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
>>> +    'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f'}
>>> +
>>> +local hexadecimal_values = {
>>> +    0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
>>> +    10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}
>>> +
>>> +local hexadecimals_mapping = {}
>>> +
>>> +local function chars_to_hex(chars, values, mapping)
>>> +    for i, char in ipairs(chars) do
>>> +        mapping[string.byte(char)] = values[i]
>>> +    end
>>> +end
>>
>>1. Why do you need this function to be called only once 1 line
>>below? Can you just inline it?
>>

1. Fixed.

>>> +
>>> +chars_to_hex(hexadecimal_chars, hexadecimal_values, hexadecimals_mapping)
>>> +
>>> +--- Match a hexadecimal representation of a string to its
>>> +-- string representation.
>>> +-- @function fromhex
>>> +-- @string   inp     the string of hexadecimals
>>> +-- @returns          formatted string
>>
>>2. I know that such comments style you got from other functions
>>of the file, but each line above contradicts with our official
>>code style, as I understand. Just for record.
>>

2. It's a codestyle from lua style guideline. As we discussed verbally
I fixed it to the appropriate one. Also created an issue to fix the guideline (#596).

>>> +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 amount of chars',
>>> +                                    'odd amount'), 2)
>>> +    end
>>> +    local len = inp:len() / 2
>>> +    local casted_inp = ffi.cast('const char *', inp)
>>> +    local res = ffi.new('char[?]', len)
>>
>>3. Why '?'? You know that res will be exactly len bytes.
>
>Concatenation in Lua is slower than providing a type and a length to ffi.new function separately. It is according to benchmarking results.
>

3. As Alexander told it is faster.

>>
>>> +    for i = 0, len - 1 do
>>> +        local first = hexadecimals_mapping[casted_inp[i * 2]]
>>> +        local second = hexadecimals_mapping[casted_inp[i * 2 + 1]]
>>> +        if first == nil or second == nil then
>>> +            error(err_string_arg:format(1, 'string.fromhex', 'hex string',
>>> +                                        'non hex chars'), 2)
>>> +        end
>>> +        res[i] = first * 16 + second
>>> +    end
>>> +    return ffi.string(res, len)
>>> +end
>>> +
>>>  local function string_strip(inp)
>>>      if type(inp) ~= 'string' then
>>>          error(err_string_arg:format(1, "string.strip", 'string', type(inp)), 2)
>>
>

diff --git a/src/lua/string.lua b/src/lua/string.lua
index 5ff64c9f6..cbce26b35 100644
--- a/src/lua/string.lua
+++ b/src/lua/string.lua
@@ -292,6 +292,53 @@ local function string_hex(inp)
     return ffi.string(res, len)
 end
 
+local hexadecimal_chars = {
+    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
+    'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f'}
+
+local hexadecimal_values = {
+    0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
+    10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}
+
+local hexadecimals_mapping = {}
+
+for i, char in ipairs(hexadecimal_chars) do
+    hexadecimals_mapping[string.byte(char)] = hexadecimal_values[i]
+end
+
+
+--
+-- Match a hexadecimal representation of a string to its
+-- string representation.
+-- @param inp the string of hexadecimals
+--
+-- @retval formatted string
+--
+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 amount of chars',
+                                    'odd amount'), 2)
+    end
+    local len = inp:len() / 2
+    local casted_inp = ffi.cast('const char *', inp)
+    local res = ffi.new('char[?]', len)
+    for i = 0, len - 1 do
+        local first = hexadecimals_mapping[casted_inp[i * 2]]
+        local second = hexadecimals_mapping[casted_inp[i * 2 + 1]]
+        if first == nil or second == nil then
+            error(err_string_arg:format(1, 'string.fromhex', 'hex string',
+                                        'non hex chars'), 2)
+        end
+        res[i] = first * 16 + second
+    end
+    return ffi.string(res, 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 +370,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


-- 
WBR, Nikita Tatunov.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re: Re[2]: Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-15 14:01         ` [tarantool-patches] " Nikita Tatunov
@ 2018-08-15 14:06           ` Vladislav Shpilevoy
  0 siblings, 0 replies; 10+ messages in thread
From: Vladislav Shpilevoy @ 2018-08-15 14:06 UTC (permalink / raw)
  To: Nikita Tatunov, tarantool-patches

Hi! Thanks for the fixes! LGTM.

On 15/08/2018 17:01, Nikita Tatunov wrote:
> Hello, thank you for the review!
> See diff for src/lua/string.lua in the end of the letter.
> 
> 
>> Понедельник, 13 августа 2018, 18:05 +03:00 от Alexander Turenko <alexander.turenko@tarantool.org>:
>>
>> Hi!
>>
>> One inline note is below.
>>
>> WBR, Alexander Turenko.
>>
>> On Aug 13, 2018 5:46 PM, Vladislav Shpilevoy < v.shpilevoy@tarantool.org > wrote:
>>> Hi! Thanks for the patch!
>>>
>>> See 3 comments below.
>>>
>>>> commit c208a8173b2f7632c3432d03807490becfe7276b
>>>> Author: N.Tatunov < n.tatunov@tarantool.org >
>>>> Date:   Wed Aug 8 14:17:02 2018 +0300
>>>>
>>>>       lua: add string.fromhex method
>>>>       
>>>>       Add string.fromhex method. Add test for string.fromhex().
>>>>       
>>>>       Closes #2562
>>>>
>>>> diff --git a/src/lua/string.lua b/src/lua/string.lua
>>>> index 5ff64c9f6..f6accb42c 100644
>>>> --- a/src/lua/string.lua
>>>> +++ b/src/lua/string.lua
>>>> @@ -292,6 +292,54 @@ local function string_hex(inp)
>>>>        return ffi.string(res, len)
>>>>    end
>>>>    
>>>> +local hexadecimal_chars = {
>>>> +    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
>>>> +    'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f'}
>>>> +
>>>> +local hexadecimal_values = {
>>>> +    0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
>>>> +    10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}
>>>> +
>>>> +local hexadecimals_mapping = {}
>>>> +
>>>> +local function chars_to_hex(chars, values, mapping)
>>>> +    for i, char in ipairs(chars) do
>>>> +        mapping[string.byte(char)] = values[i]
>>>> +    end
>>>> +end
>>>
>>> 1. Why do you need this function to be called only once 1 line
>>> below? Can you just inline it?
>>>
> 
> 1. Fixed.
> 
>>>> +
>>>> +chars_to_hex(hexadecimal_chars, hexadecimal_values, hexadecimals_mapping)
>>>> +
>>>> +--- Match a hexadecimal representation of a string to its
>>>> +-- string representation.
>>>> +-- @function fromhex
>>>> +-- @string   inp     the string of hexadecimals
>>>> +-- @returns          formatted string
>>>
>>> 2. I know that such comments style you got from other functions
>>> of the file, but each line above contradicts with our official
>>> code style, as I understand. Just for record.
>>>
> 
> 2. It's a codestyle from lua style guideline. As we discussed verbally
> I fixed it to the appropriate one. Also created an issue to fix the guideline (#596).
> 
>>>> +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 amount of chars',
>>>> +                                    'odd amount'), 2)
>>>> +    end
>>>> +    local len = inp:len() / 2
>>>> +    local casted_inp = ffi.cast('const char *', inp)
>>>> +    local res = ffi.new('char[?]', len)
>>>
>>> 3. Why '?'? You know that res will be exactly len bytes.
>>
>> Concatenation in Lua is slower than providing a type and a length to ffi.new function separately. It is according to benchmarking results.
>>
> 
> 3. As Alexander told it is faster.
> 
>>>
>>>> +    for i = 0, len - 1 do
>>>> +        local first = hexadecimals_mapping[casted_inp[i * 2]]
>>>> +        local second = hexadecimals_mapping[casted_inp[i * 2 + 1]]
>>>> +        if first == nil or second == nil then
>>>> +            error(err_string_arg:format(1, 'string.fromhex', 'hex string',
>>>> +                                        'non hex chars'), 2)
>>>> +        end
>>>> +        res[i] = first * 16 + second
>>>> +    end
>>>> +    return ffi.string(res, len)
>>>> +end
>>>> +
>>>>    local function string_strip(inp)
>>>>        if type(inp) ~= 'string' then
>>>>            error(err_string_arg:format(1, "string.strip", 'string', type(inp)), 2)
>>>
>>
> 
> diff --git a/src/lua/string.lua b/src/lua/string.lua
> index 5ff64c9f6..cbce26b35 100644
> --- a/src/lua/string.lua
> +++ b/src/lua/string.lua
> @@ -292,6 +292,53 @@ local function string_hex(inp)
>       return ffi.string(res, len)
>   end
>   
> +local hexadecimal_chars = {
> +    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
> +    'a', 'B', 'b', 'C', 'c', 'D', 'd', 'E', 'e', 'F', 'f'}
> +
> +local hexadecimal_values = {
> +    0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10,
> +    10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15}
> +
> +local hexadecimals_mapping = {}
> +
> +for i, char in ipairs(hexadecimal_chars) do
> +    hexadecimals_mapping[string.byte(char)] = hexadecimal_values[i]
> +end
> +
> +
> +--
> +-- Match a hexadecimal representation of a string to its
> +-- string representation.
> +-- @param inp the string of hexadecimals
> +--
> +-- @retval formatted string
> +--
> +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 amount of chars',
> +                                    'odd amount'), 2)
> +    end
> +    local len = inp:len() / 2
> +    local casted_inp = ffi.cast('const char *', inp)
> +    local res = ffi.new('char[?]', len)
> +    for i = 0, len - 1 do
> +        local first = hexadecimals_mapping[casted_inp[i * 2]]
> +        local second = hexadecimals_mapping[casted_inp[i * 2 + 1]]
> +        if first == nil or second == nil then
> +            error(err_string_arg:format(1, 'string.fromhex', 'hex string',
> +                                        'non hex chars'), 2)
> +        end
> +        res[i] = first * 16 + second
> +    end
> +    return ffi.string(res, 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 +370,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
> 
> 

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re: Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-09 18:35 [tarantool-patches] Fwd: Re[2]: [PATCH] lua: add string.fromhex method Nikita Tatunov
  2018-08-09 19:00 ` [tarantool-patches] " Vladislav Shpilevoy
@ 2018-08-16  6:06 ` Kirill Yukhin
  2018-08-16  9:28   ` [tarantool-patches] " Alexander Turenko
  1 sibling, 1 reply; 10+ messages in thread
From: Kirill Yukhin @ 2018-08-16  6:06 UTC (permalink / raw)
  To: tarantool-patches

Hello,
On 09 авг 21:35, Nikita Tatunov wrote:
> >On Wed, Aug 08, 2018 at 03:21:03PM +0300, N.Tatunov wrote:
> >> Add string.fromhex method. Add test for string.fromhex().
> >> 
> >> Closes #2562
> >> ---
> >> 
> >> Issue:  https://github.com/tarantool/tarantool/issues/2562
> >> Branch:  https://github.com/tarantool/tarantool/tree/N_Tatunov/gh-2562-fromhex-method
I've checked the patch into 2.0 branch.

--
Regards, Kirill Yukhin

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re: [tarantool-patches] Re: Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-16  6:06 ` [tarantool-patches] " Kirill Yukhin
@ 2018-08-16  9:28   ` Alexander Turenko
  2018-08-16 16:07     ` Kirill Yukhin
  0 siblings, 1 reply; 10+ messages in thread
From: Alexander Turenko @ 2018-08-16  9:28 UTC (permalink / raw)
  To: Kirill Yukhin; +Cc: tarantool-patches, Nikita Tatunov

Hi!

It is not SQL issue, so I think it should be pushed into 1.10. AFAIR, it
was moved into 2.1.0 milestone because of deadline. Do I miss some
change within our check-in politics?

WBR, Alexander Turenko.

>Четверг, 16 августа 2018, 9:06 +03:00 от Kirill Yukhin <kyukhin@tarantool.org>:
>
>Hello,
>On 09 авг 21:35, Nikita Tatunov wrote:
>> >On Wed, Aug 08, 2018 at 03:21:03PM +0300, N.Tatunov wrote:
>> >> Add string.fromhex method. Add test for string.fromhex().
>> >> 
>> >> Closes #2562
>> >> ---
>> >> 
>> >> Issue:  https://github.com/tarantool/tarantool/issues/2562
>> >> Branch:  https://github.com/tarantool/tarantool/tree/N_Tatunov/gh-2562-fromhex-method
>I've checked the patch into 2.0 branch.
>
>--
>Regards, Kirill Yukhin
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [tarantool-patches] Re: Fwd: Re[2]: [PATCH] lua: add string.fromhex method
  2018-08-16  9:28   ` [tarantool-patches] " Alexander Turenko
@ 2018-08-16 16:07     ` Kirill Yukhin
  0 siblings, 0 replies; 10+ messages in thread
From: Kirill Yukhin @ 2018-08-16 16:07 UTC (permalink / raw)
  To: Alexander Turenko; +Cc: tarantool-patches, Nikita Tatunov

Hello,
On 16 авг 12:28, Alexander Turenko wrote:
> Hi!
> 
> It is not SQL issue, so I think it should be pushed into 1.10. AFAIR, it
> was moved into 2.1.0 milestone because of deadline. Do I miss some
> change within our check-in politics?
Committed to 1.10 branch.

--
Regards, Kirill Yukhin

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2018-08-16 16:08 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-09 18:35 [tarantool-patches] Fwd: Re[2]: [PATCH] lua: add string.fromhex method Nikita Tatunov
2018-08-09 19:00 ` [tarantool-patches] " Vladislav Shpilevoy
2018-08-09 19:02   ` [tarantool-patches] Re[2]: [tarantool-patches] " Nikita Tatunov
2018-08-13 14:46     ` [tarantool-patches] Re: Re[2]: " Vladislav Shpilevoy
2018-08-13 15:04       ` Alexander Turenko
2018-08-15 14:01         ` [tarantool-patches] " Nikita Tatunov
2018-08-15 14:06           ` Vladislav Shpilevoy
2018-08-16  6:06 ` [tarantool-patches] " Kirill Yukhin
2018-08-16  9:28   ` [tarantool-patches] " Alexander Turenko
2018-08-16 16:07     ` Kirill Yukhin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox