From 273fbafa09feb041e43b6865162ff7efdb79f7e8 Mon Sep 17 00:00:00 2001 From: Derick Alangi Date: Thu, 24 Jun 2021 12:17:30 +0100 Subject: [PATCH] Add `IS_NUM_BYTE` validator to check if number is a byte (#127) In conjunction with the ASCII char set, a byte should be any valid character mapping to an integer between -128 to 127 (that is signed integers as well). Anything beyond can't fit into a byte hence this can't be a character but 2 characters. --- src/pk_core.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/pk_core.c b/src/pk_core.c index 40e34b1..f06e8c3 100644 --- a/src/pk_core.c +++ b/src/pk_core.c @@ -6,6 +6,7 @@ #include "pk_core.h" #include +#include #include #include @@ -31,6 +32,9 @@ static const char* DOCSTRING(fn) = docstring; \ static void fn(PKVM* vm) +// Checks if a number is a byte +#define IS_NUM_BYTE(num) ((CHAR_MIN <= (num)) && ((num) <= CHAR_MAX)) + /*****************************************************************************/ /* CORE PUBLIC API */ /*****************************************************************************/ @@ -613,7 +617,9 @@ DEF(coreStrChr, int64_t num; if (!validateInteger(vm, ARG(1), &num, "Argument 1")) return; - // TODO: validate num is a byte. + if (!IS_NUM_BYTE(num)) { + RET_ERR(newString(vm, "The number is not in a byte range.")); + } char c = (char)num; RET(VAR_OBJ(newStringLength(vm, &c, 1)));