Tarantool development patches archive
 help / color / mirror / Atom feed
From: Alexander Turenko <alexander.turenko@tarantool.org>
To: Gleb Skiba <gleb-skiba@mail.ru>
Cc: tarantool-patches@freelists.org
Subject: [tarantool-patches] Re: [PATCH] lua: Add string.decodehex method
Date: Mon, 14 May 2018 11:00:30 +0300	[thread overview]
Message-ID: <20180514080029.5e3unj6ucuxq7jmt@tkn_work_nb> (raw)
In-Reply-To: <1525702677-11477-1-git-send-email-gleb-skiba@mail.ru>

Hi, Gleb!

The commit message header is the following:

> lua: Add string.decodehex method

Please, start the sentence from lowercase letter after the colon.

Use actual name: 'fromhex'.

See other comments below.

WBR, Alexander Turenko.

On Mon, May 07, 2018 at 05:17:57PM +0300, Redacted sender "gleb-skiba" for DMARC wrote:
> From: Gleb <gleb-skiba@mail.ru>
> 
> 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..9be9a2a 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 }
> +

It is better to use symbols explicitly (['a'] = 10 and so on).

Make appropriate indent. Don't put whitespace before closing curly
parenthesis.

Use less common name for module-global 'constant'. Preferably prepend it
with the function name when the function is only one used this variable.
Place it before all functions, after other 'constants' definition.
Proposed: fromhex_hex_char_to_number.

> +local function string_fromhex(inp)

Doxygen-style comment is very advisable here.

> +    if type(inp) ~= 'string' then
> +        error(err_string_arg:format(1, 'string.fromhex', 'string', type(inp)), 2)

Don't exceed 80 symbols limit here and below.

> +    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)

What `uinp` means? Don't clear for me. Excuse me for nitpicking.

> +    local ans = ffi.new('char[?]', len)

`res` is a way more common the `ans`.

> +
> +    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

All parenthesis are redundant.

> +            error(err_string_arg:format(1, 'string.fromhex', 'hex string', 'not hex string'), 2)

'wrong format' or like so would looks better then 'not hex string', IMO.

> +        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")

Uppercase hex test is needed.

I would add a bit more symbols (use 'lorem ipsum') to better cover all
possible symbols from [0-9a-fA-F].

> +    local _, err = pcall(function() string.fromhex("aaa") end)

It is better to use `pcall(string.fromhex, 'aaa')`

> +    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)

fromhex have three error situations. It is better to test all three.

> +end)
> +
>  test:test("strip", function(test)
>      test:plan(6)
>      local str = "  hello hello "
> -- 
> 2.7.4
> 
> 

      parent reply	other threads:[~2018-05-14  8:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-05-07 14:17 [tarantool-patches] " Redacted sender "gleb-skiba" for DMARC
2018-05-07 14:22 ` [tarantool-patches] " Vladislav Shpilevoy
2018-05-14  8:00 ` Alexander Turenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180514080029.5e3unj6ucuxq7jmt@tkn_work_nb \
    --to=alexander.turenko@tarantool.org \
    --cc=gleb-skiba@mail.ru \
    --cc=tarantool-patches@freelists.org \
    --subject='[tarantool-patches] Re: [PATCH] lua: Add string.decodehex method' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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