1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-21 04:39:55 +00:00

refactor(c-api): Use size_t on some JanetStringHead fields

This commit is contained in:
GrayJack 2024-04-15 18:40:20 -03:00
parent 7b0e5e31db
commit ed800bd39a
2 changed files with 17 additions and 17 deletions

View File

@ -31,8 +31,8 @@
#include <string.h> #include <string.h>
/* Begin building a string */ /* Begin building a string */
uint8_t *janet_string_begin(int32_t length) { uint8_t *janet_string_begin(size_t length) {
JanetStringHead *head = janet_gcalloc(JANET_MEMORY_STRING, sizeof(JanetStringHead) + (size_t) length + 1); JanetStringHead *head = janet_gcalloc(JANET_MEMORY_STRING, sizeof(JanetStringHead) + length + 1);
head->length = length; head->length = length;
uint8_t *data = (uint8_t *)head->data; uint8_t *data = (uint8_t *)head->data;
data[length] = 0; data[length] = 0;
@ -46,8 +46,8 @@ const uint8_t *janet_string_end(uint8_t *str) {
} }
/* Load a buffer as a string */ /* Load a buffer as a string */
const uint8_t *janet_string(const uint8_t *buf, int32_t len) { const uint8_t *janet_string(const uint8_t *buf, size_t len) {
JanetStringHead *head = janet_gcalloc(JANET_MEMORY_STRING, sizeof(JanetStringHead) + (size_t) len + 1); JanetStringHead *head = janet_gcalloc(JANET_MEMORY_STRING, sizeof(JanetStringHead) + len + 1);
head->length = len; head->length = len;
head->hash = janet_string_calchash(buf, len); head->hash = janet_string_calchash(buf, len);
uint8_t *data = (uint8_t *)head->data; uint8_t *data = (uint8_t *)head->data;
@ -60,7 +60,7 @@ const uint8_t *janet_string(const uint8_t *buf, int32_t len) {
int janet_string_compare(const uint8_t *lhs, const uint8_t *rhs) { int janet_string_compare(const uint8_t *lhs, const uint8_t *rhs) {
int32_t xlen = janet_string_length(lhs); int32_t xlen = janet_string_length(lhs);
int32_t ylen = janet_string_length(rhs); int32_t ylen = janet_string_length(rhs);
int32_t len = xlen > ylen ? ylen : xlen; size_t len = xlen > ylen ? ylen : xlen;
int res = memcmp(lhs, rhs, len); int res = memcmp(lhs, rhs, len);
if (res) return res > 0 ? 1 : -1; if (res) return res > 0 ? 1 : -1;
if (xlen == ylen) return 0; if (xlen == ylen) return 0;
@ -68,9 +68,9 @@ int janet_string_compare(const uint8_t *lhs, const uint8_t *rhs) {
} }
/* Compare a janet string with a piece of memory */ /* Compare a janet string with a piece of memory */
int janet_string_equalconst(const uint8_t *lhs, const uint8_t *rhs, int32_t rlen, int32_t rhash) { int janet_string_equalconst(const uint8_t *lhs, const uint8_t *rhs, size_t rlen, int32_t rhash) {
int32_t lhash = janet_string_hash(lhs); int32_t lhash = janet_string_hash(lhs);
int32_t llen = janet_string_length(lhs); size_t llen = janet_string_length(lhs);
if (lhs == rhs) if (lhs == rhs)
return 1; return 1;
if (lhash != rhash || llen != rlen) if (lhash != rhash || llen != rlen)
@ -208,7 +208,7 @@ JANET_CORE_FN(cfun_string_repeat,
if (rep < 0) janet_panic("expected non-negative number of repetitions"); if (rep < 0) janet_panic("expected non-negative number of repetitions");
if (rep == 0) return janet_cstringv(""); if (rep == 0) return janet_cstringv("");
int64_t mulres = (int64_t) rep * view.len; int64_t mulres = (int64_t) rep * view.len;
if (mulres > INT32_MAX) janet_panic("result string is too long"); if (mulres > JANET_INTMAX_INT64) janet_panic("result string is too long");
uint8_t *newbuf = janet_string_begin((int32_t) mulres); uint8_t *newbuf = janet_string_begin((int32_t) mulres);
uint8_t *end = newbuf + mulres; uint8_t *end = newbuf + mulres;
for (uint8_t *p = newbuf; p < end; p += view.len) { for (uint8_t *p = newbuf; p < end; p += view.len) {
@ -223,7 +223,7 @@ JANET_CORE_FN(cfun_string_bytes,
janet_fixarity(argc, 1); janet_fixarity(argc, 1);
JanetByteView view = janet_getbytes(argv, 0); JanetByteView view = janet_getbytes(argv, 0);
Janet *tup = janet_tuple_begin(view.len); Janet *tup = janet_tuple_begin(view.len);
int32_t i; size_t i;
for (i = 0; i < view.len; i++) { for (i = 0; i < view.len; i++) {
tup[i] = janet_wrap_integer((int32_t) view.bytes[i]); tup[i] = janet_wrap_integer((int32_t) view.bytes[i]);
} }
@ -504,11 +504,11 @@ JANET_CORE_FN(cfun_string_join,
joiner.len = 0; joiner.len = 0;
} }
/* Check args */ /* Check args */
int32_t i; size_t i;
int64_t finallen = 0; int64_t finallen = 0;
for (i = 0; i < parts.len; i++) { for (i = 0; i < parts.len; i++) {
const uint8_t *chunk; const uint8_t *chunk;
int32_t chunklen = 0; size_t chunklen = 0;
if (!janet_bytes_view(parts.items[i], &chunk, &chunklen)) { if (!janet_bytes_view(parts.items[i], &chunk, &chunklen)) {
janet_panicf("item %d of parts is not a byte sequence, got %v", i, parts.items[i]); janet_panicf("item %d of parts is not a byte sequence, got %v", i, parts.items[i]);
} }
@ -518,10 +518,10 @@ JANET_CORE_FN(cfun_string_join,
janet_panic("result string too long"); janet_panic("result string too long");
} }
uint8_t *buf, *out; uint8_t *buf, *out;
out = buf = janet_string_begin((int32_t) finallen); out = buf = janet_string_begin((size_t) finallen);
for (i = 0; i < parts.len; i++) { for (i = 0; i < parts.len; i++) {
const uint8_t *chunk = NULL; const uint8_t *chunk = NULL;
int32_t chunklen = 0; size_t chunklen = 0;
if (i) { if (i) {
safe_memcpy(out, joiner.bytes, joiner.len); safe_memcpy(out, joiner.bytes, joiner.len);
out += joiner.len; out += joiner.len;

View File

@ -1017,7 +1017,7 @@ struct JanetStructHead {
/* Prefix for a string */ /* Prefix for a string */
struct JanetStringHead { struct JanetStringHead {
JanetGCObject gc; JanetGCObject gc;
int32_t length; size_t length;
int32_t hash; int32_t hash;
const uint8_t data[]; const uint8_t data[];
}; };
@ -1649,13 +1649,13 @@ JANET_API JanetTuple janet_tuple_n(const Janet *values, size_t n);
#define janet_string_head(s) ((JanetStringHead *)((char *)s - offsetof(JanetStringHead, data))) #define janet_string_head(s) ((JanetStringHead *)((char *)s - offsetof(JanetStringHead, data)))
#define janet_string_length(s) (janet_string_head(s)->length) #define janet_string_length(s) (janet_string_head(s)->length)
#define janet_string_hash(s) (janet_string_head(s)->hash) #define janet_string_hash(s) (janet_string_head(s)->hash)
JANET_API uint8_t *janet_string_begin(int32_t length); JANET_API uint8_t *janet_string_begin(size_t length);
JANET_API JanetString janet_string_end(uint8_t *str); JANET_API JanetString janet_string_end(uint8_t *str);
JANET_API JanetString janet_string(const uint8_t *buf, int32_t len); JANET_API JanetString janet_string(const uint8_t *buf, size_t len);
JANET_API JanetString janet_cstring(const char *cstring); JANET_API JanetString janet_cstring(const char *cstring);
JANET_API int janet_string_compare(JanetString lhs, JanetString rhs); JANET_API int janet_string_compare(JanetString lhs, JanetString rhs);
JANET_API int janet_string_equal(JanetString lhs, JanetString rhs); JANET_API int janet_string_equal(JanetString lhs, JanetString rhs);
JANET_API int janet_string_equalconst(JanetString lhs, const uint8_t *rhs, int32_t rlen, int32_t rhash); JANET_API int janet_string_equalconst(JanetString lhs, const uint8_t *rhs, size_t rlen, size_t rhash);
JANET_API JanetString janet_description(Janet x); JANET_API JanetString janet_description(Janet x);
JANET_API JanetString janet_to_string(Janet x); JANET_API JanetString janet_to_string(Janet x);
JANET_API void janet_to_string_b(JanetBuffer *buffer, Janet x); JANET_API void janet_to_string_b(JanetBuffer *buffer, Janet x);