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.
This commit is contained in:
Derick Alangi 2021-06-24 12:17:30 +01:00 committed by GitHub
parent 6ce5c9f57e
commit 273fbafa09
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@
#include "pk_core.h"
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <time.h>
@ -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)));