mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-05 20:26:53 +08:00
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:
parent
6ce5c9f57e
commit
273fbafa09
@ -6,6 +6,7 @@
|
|||||||
#include "pk_core.h"
|
#include "pk_core.h"
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <limits.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
@ -31,6 +32,9 @@
|
|||||||
static const char* DOCSTRING(fn) = docstring; \
|
static const char* DOCSTRING(fn) = docstring; \
|
||||||
static void fn(PKVM* vm)
|
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 */
|
/* CORE PUBLIC API */
|
||||||
/*****************************************************************************/
|
/*****************************************************************************/
|
||||||
@ -613,7 +617,9 @@ DEF(coreStrChr,
|
|||||||
int64_t num;
|
int64_t num;
|
||||||
if (!validateInteger(vm, ARG(1), &num, "Argument 1")) return;
|
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;
|
char c = (char)num;
|
||||||
RET(VAR_OBJ(newStringLength(vm, &c, 1)));
|
RET(VAR_OBJ(newStringLength(vm, &c, 1)));
|
||||||
|
Loading…
Reference in New Issue
Block a user