From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng3.m.smailru.net (smtpng3.m.smailru.net [94.100.177.149]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id AE20046970E for ; Fri, 17 Jan 2020 11:03:10 +0300 (MSK) From: olegrok@tarantool.org Date: Fri, 17 Jan 2020 11:03:01 +0300 Message-Id: <20200117080301.97597-1-olegrok@tarantool.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH] lua: implement is_decimal check List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org Cc: Oleg Babin From: Oleg Babin Some of our users want to have a native method to check is specified value 'decimal' or not This patch introduces 'is_decimal' check in 'decimal' module Closes #4623 @TarantoolBot document Title: decimal.is_decimal is_decimal check function returns "true" if specified value is decimal and "false" otherwise --- Issue: https://github.com/tarantool/tarantool/issues/4623 Branch: olegrok/4623-is_decimal src/lua/decimal.c | 28 ++++++++++++++++++++++++++++ test/app/decimal.result | 29 +++++++++++++++++++++++++++++ test/app/decimal.test.lua | 9 +++++++++ 3 files changed, 66 insertions(+) diff --git a/src/lua/decimal.c b/src/lua/decimal.c index 8905a0b9d..d3400521d 100644 --- a/src/lua/decimal.c +++ b/src/lua/decimal.c @@ -87,6 +87,23 @@ lua_pushdecimal(struct lua_State *L) return res; } +/** + * Returns true if a value at a given index is a decimal + * and false otherwise + */ +bool +lua_isdecimal(struct lua_State *L, int index) +{ + if (lua_type(L, index) != LUA_TCDATA) + return false; + + uint32_t ctypeid; + luaL_checkcdata(L, index, &ctypeid); + if (ctypeid != CTID_DECIMAL) + return false; + return true; +} + /** Check whether a value at a given index is a decimal. */ static decimal_t * lua_checkdecimal(struct lua_State *L, int index) @@ -272,6 +289,16 @@ ldecimal_new(struct lua_State *L) return 1; } +static int +ldecimal_isdecimal(struct lua_State *L) +{ + if (lua_gettop(L) < 1) + luaL_error(L, "usage: decimal.is_decimal(value)"); + bool is_decimal = lua_isdecimal(L, 1); + lua_pushboolean(L, is_decimal); + return 1; +} + static int ldecimal_round(struct lua_State *L) { @@ -378,6 +405,7 @@ static const luaL_Reg ldecimal_lib[] = { {"precision", ldecimal_precision}, {"abs", ldecimal_abs}, {"new", ldecimal_new}, + {"is_decimal", ldecimal_isdecimal}, {NULL, NULL} }; diff --git a/test/app/decimal.result b/test/app/decimal.result index 90d0984b0..8c0c3c1e4 100644 --- a/test/app/decimal.result +++ b/test/app/decimal.result @@ -625,3 +625,32 @@ a % 72 | --- | - 29.96 | ... + +-- gh-4623 is_decimal function +decimal_value = decimal.new(1) + | --- + | ... +decimal.is_decimal(decimal_value) + | --- + | - true + | ... +decimal.is_decimal('string') + | --- + | - false + | ... +decimal.is_decimal(0) + | --- + | - false + | ... +decimal.is_decimal(-1ULL) + | --- + | - false + | ... +decimal.is_decimal(box.error.new(box.error.UNKNOWN)) + | --- + | - false + | ... +decimal.is_decimal(ffi.cast('char*', '42.42')) + | --- + | - false + | ... diff --git a/test/app/decimal.test.lua b/test/app/decimal.test.lua index 79189a2f5..8e838aa32 100644 --- a/test/app/decimal.test.lua +++ b/test/app/decimal.test.lua @@ -177,3 +177,12 @@ a % 100 a % 173 a % 72 720 % a + +-- gh-4623 is_decimal function +decimal_value = decimal.new(1) +decimal.is_decimal(decimal_value) +decimal.is_decimal('string') +decimal.is_decimal(0) +decimal.is_decimal(-1ULL) +decimal.is_decimal(box.error.new(box.error.UNKNOWN)) +decimal.is_decimal(ffi.cast('char*', '42.42')) -- 2.23.0