added hawk_json_feeduchars()

This commit is contained in:
2025-06-08 21:51:11 +09:00
parent 7f9ac2047f
commit 63eb61c5c0
17 changed files with 710 additions and 577 deletions

View File

@ -113,7 +113,7 @@ int SedStd::FileStream::open (Data& io)
#if defined(HAWK_OOCH_IS_UCH) #if defined(HAWK_OOCH_IS_UCH)
this->file = hawk_gem_dupucstr(gem, tmp, HAWK_NULL); this->file = hawk_gem_dupucstr(gem, tmp, HAWK_NULL);
#else #else
this->file = hawk_duputobcstr(gem, tmp, HAWK_NULL); this->file = hawk_gem_duputobcstr(gem, tmp, HAWK_NULL);
#endif #endif
} }
else else
@ -350,7 +350,7 @@ int SedStd::StringStream::open (Data& io)
#if defined(HAWK_OOCH_IS_UCH) #if defined(HAWK_OOCH_IS_UCH)
this->in.str = hawk_gem_dupucstr(gem, (const hawk_uch_t*)this->in._str, &len); this->in.str = hawk_gem_dupucstr(gem, (const hawk_uch_t*)this->in._str, &len);
#else #else
this->in.str = hawk_duputobcstr(gem, (const hawk_uch_t*)this->in._str, &len); this->in.str = hawk_gem_duputobcstr(gem, (const hawk_uch_t*)this->in._str, &len);
#endif #endif
} }
else else

View File

@ -126,13 +126,26 @@ HAWK_EXPORT void hawk_json_reset (
hawk_json_t* json hawk_json_t* json
); );
HAWK_EXPORT int hawk_json_feed ( HAWK_EXPORT int hawk_json_feedbchars (
hawk_json_t* json, hawk_json_t* json,
const void* ptr, const hawk_bch_t* ptr,
hawk_oow_t len, hawk_oow_t len,
hawk_oow_t* xlen hawk_oow_t* xlen
); );
HAWK_EXPORT int hawk_json_feeduchars (
hawk_json_t* json,
const hawk_uch_t* ptr,
hawk_oow_t len,
hawk_oow_t* xlen
);
#if defined(HAWK_OOCH_IS_UCH)
# define hawk_json_feed hawk_json_feeduchars
#else
# define hawk_json_feed hawk_json_feedbchars
#endif
HAWK_EXPORT hawk_json_state_t hawk_json_getstate ( HAWK_EXPORT hawk_json_state_t hawk_json_getstate (
hawk_json_t* json hawk_json_t* json
); );
@ -208,12 +221,12 @@ static HAWK_INLINE void hawk_json_geterror (hawk_json_t* json, hawk_errnum_t* er
#define hawk_json_geterror(json, errnum, errmsg, errloc) (hawk_gem_geterror(hawk_json_getgem(json), errnum, errmsg, errloc)) #define hawk_json_geterror(json, errnum, errmsg, errloc) (hawk_gem_geterror(hawk_json_getgem(json), errnum, errmsg, errloc))
#endif #endif
#if defined(HAWK_OOCH_IS_BCH) #if defined(HAWK_OOCH_IS_UCH)
# define hawk_json_geterrmsg hawk_json_geterrbmsg
# define hawk_json_geterrinf hawk_json_geterrbinf
#else
# define hawk_json_geterrmsg hawk_json_geterrumsg # define hawk_json_geterrmsg hawk_json_geterrumsg
# define hawk_json_geterrinf hawk_json_geterruinf # define hawk_json_geterrinf hawk_json_geterruinf
#else
# define hawk_json_geterrmsg hawk_json_geterrbmsg
# define hawk_json_geterrinf hawk_json_geterrbinf
#endif #endif

View File

@ -143,6 +143,35 @@ HAWK_EXPORT int hawk_comp_bchars_bcstr (
/* ------------------------------------ */ /* ------------------------------------ */
HAWK_EXPORT int hawk_comp_ucstr_bcstr (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_ucstr_bcstr_limited (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
hawk_oow_t maxlen,
int ignorecase
);
HAWK_EXPORT int hawk_comp_uchars_bcstr (
const hawk_uch_t* str1,
hawk_oow_t len,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_bchars_ucstr (
const hawk_bch_t* str1,
hawk_oow_t len,
const hawk_uch_t* str2,
int ignorecase
);
/* ------------------------------------ */
HAWK_EXPORT hawk_oow_t hawk_concat_uchars_to_ucstr ( HAWK_EXPORT hawk_oow_t hawk_concat_uchars_to_ucstr (
hawk_uch_t* buf, hawk_uch_t* buf,
hawk_oow_t bsz, hawk_oow_t bsz,

View File

@ -143,6 +143,35 @@ HAWK_EXPORT int hawk_comp_bchars_bcstr (
/* ------------------------------------ */ /* ------------------------------------ */
HAWK_EXPORT int hawk_comp_ucstr_bcstr (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_ucstr_bcstr_limited (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
hawk_oow_t maxlen,
int ignorecase
);
HAWK_EXPORT int hawk_comp_uchars_bcstr (
const hawk_uch_t* str1,
hawk_oow_t len,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_bchars_ucstr (
const hawk_bch_t* str1,
hawk_oow_t len,
const hawk_uch_t* str2,
int ignorecase
);
/* ------------------------------------ */
HAWK_EXPORT hawk_oow_t hawk_concat_uchars_to_ucstr ( HAWK_EXPORT hawk_oow_t hawk_concat_uchars_to_ucstr (
hawk_uch_t* buf, hawk_uch_t* buf,
hawk_oow_t bsz, hawk_oow_t bsz,

View File

@ -374,35 +374,6 @@ HAWK_EXPORT hawk_oow_t hawk_hash_bytes_ (
* STRING * STRING
* ========================================================================= */ * ========================================================================= */
HAWK_EXPORT int hawk_comp_ucstr_bcstr (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_ucstr_bcstr_limited (
const hawk_uch_t* str1,
const hawk_bch_t* str2,
hawk_oow_t maxlen,
int ignorecase
);
HAWK_EXPORT int hawk_comp_uchars_bcstr (
const hawk_uch_t* str1,
hawk_oow_t len,
const hawk_bch_t* str2,
int ignorecase
);
HAWK_EXPORT int hawk_comp_bchars_ucstr (
const hawk_bch_t* str1,
hawk_oow_t len,
const hawk_uch_t* str2,
int ignorecase
);
/* ------------------------------------------------------------------------- */
HAWK_EXPORT hawk_oow_t hawk_subst_for_uchars_to_ucstr ( HAWK_EXPORT hawk_oow_t hawk_subst_for_uchars_to_ucstr (
hawk_uch_t* buf, hawk_uch_t* buf,
hawk_oow_t bsz, hawk_oow_t bsz,

View File

@ -696,7 +696,7 @@ start_over:
/* ========================================================================= */ /* ========================================================================= */
static int feed_json_data (hawk_json_t* json, const hawk_bch_t* data, hawk_oow_t len, hawk_oow_t* xlen) static int feed_json_data_b (hawk_json_t* json, const hawk_bch_t* data, hawk_oow_t len, hawk_oow_t* xlen)
{ {
const hawk_bch_t* ptr; const hawk_bch_t* ptr;
const hawk_bch_t* end; const hawk_bch_t* end;
@ -747,9 +747,51 @@ oops:
return -1; return -1;
} }
/* ========================================================================= */
static int feed_json_data_u (hawk_json_t* json, const hawk_uch_t* data, hawk_oow_t len, hawk_oow_t* xlen)
{
const hawk_uch_t* ptr;
const hawk_uch_t* end;
ptr = data;
end = ptr + len;
while (ptr < end)
{
hawk_ooci_t c;
#if defined(HAWK_OOCH_IS_UCH)
c = *ptr++;
/* handle a single character */
if (handle_char(json, c) <= -1) goto oops;
#else
hawk_bch_t bcsbuf[HAWK_BCSIZE_MAX];
hawk_oow_t mlen = 0;
hawk_oow_t n, i;
n = json->_gem.cmgr->uctobc(*ptr++, bcsbuf, HAWK_COUNTOF(bcsbuf));
if (n == 0) goto oops; // illegal character
for (i = 0; i < n; i++)
{
if (handle_char(json, bcsbuf[i]) <= -1) goto oops;
}
#endif
}
*xlen = ptr - data;
return 1;
oops:
/* TODO: compute the number of processed bytes so far and return it via a parameter??? */
/*printf ("feed oops....\n");*/
return -1;
}
/* ========================================================================= */ /* ========================================================================= */
hawk_json_t* hawk_json_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t* cmgr, hawk_json_prim_t* prim, hawk_errnum_t* errnum) hawk_json_t* hawk_json_open (hawk_mmgr_t* mmgr, hawk_oow_t xtnsize, hawk_cmgr_t* cmgr, hawk_json_prim_t* prim, hawk_errnum_t* errnum)
{ {
hawk_json_t* json; hawk_json_t* json;
@ -896,17 +938,34 @@ void hawk_json_reset (hawk_json_t* json)
json->state_stack->state = HAWK_JSON_STATE_START; json->state_stack->state = HAWK_JSON_STATE_START;
} }
int hawk_json_feed (hawk_json_t* json, const void* ptr, hawk_oow_t len, hawk_oow_t* xlen) int hawk_json_feedbchars (hawk_json_t* json, const hawk_bch_t* ptr, hawk_oow_t len, hawk_oow_t* xlen)
{ {
int x; int x;
hawk_oow_t total, ylen; hawk_oow_t total, ylen;
const hawk_bch_t* buf;
buf = (const hawk_bch_t*)ptr;
total = 0; total = 0;
while (total < len) while (total < len)
{ {
x = feed_json_data(json, &buf[total], len - total, &ylen); x = feed_json_data_b(json, &ptr[total], len - total, &ylen);
if (x <= -1) return -1;
total += ylen;
if (x == 0) break; /* incomplete sequence encountered */
}
*xlen = total;
return 0;
}
int hawk_json_feeduchars (hawk_json_t* json, const hawk_uch_t* ptr, hawk_oow_t len, hawk_oow_t* xlen)
{
int x;
hawk_oow_t total, ylen;
total = 0;
while (total < len)
{
x = feed_json_data_u(json, &ptr[total], len - total, &ylen);
if (x <= -1) return -1; if (x <= -1) return -1;
total += ylen; total += ylen;

View File

@ -1,11 +1,39 @@
#include <hawk-json.h> #include <hawk-json.h>
#include <hawk-str.h>
#include <stdio.h> #include <stdio.h>
#include "tap.h" #include "tap.h"
#define OK_X(test) OK(test, #test)
static int on_json_element (hawk_json_t* json, hawk_json_inst_t inst, const hawk_oocs_t* str) static int on_json_element (hawk_json_t* json, hawk_json_inst_t inst, const hawk_oocs_t* str)
{ {
printf ("%d\n", inst); static int phase = 0;
if (phase == 0) OK_X(inst == HAWK_JSON_INST_START_DIC);
if (phase == 1)
{
OK_X(inst == HAWK_JSON_INST_KEY);
OK_X(hawk_comp_oochars_bcstr(str->ptr, str->len, "hello", 0) == 0);
}
if (phase == 2)
{
OK_X(inst == HAWK_JSON_INST_STRING);
OK_X(hawk_comp_oochars_bcstr(str->ptr, str->len, "world", 0) == 0);
}
if (phase == 3)
{
OK_X(inst == HAWK_JSON_INST_KEY);
OK_X(hawk_comp_oochars_bcstr(str->ptr, str->len, "key1", 0) == 0);
}
if (phase == 4)
{
OK_X(inst == HAWK_JSON_INST_NUMBER);
OK_X(hawk_comp_oochars_bcstr(str->ptr, str->len, "12345", 0) == 0);
}
if (phase == 5) OK_X(inst == HAWK_JSON_INST_END_DIC);
phase++;
return 0; return 0;
} }
@ -13,7 +41,9 @@ int main ()
{ {
hawk_json_t* json; hawk_json_t* json;
hawk_json_prim_t prim; hawk_json_prim_t prim;
/* const hawk_ooch_t* TODO */ hawk_oow_t xlen;
int n;
no_plan(); no_plan();
prim.instcb = on_json_element; prim.instcb = on_json_element;
@ -21,7 +51,9 @@ int main ()
json = hawk_json_openstd(0, &prim, HAWK_NULL); json = hawk_json_openstd(0, &prim, HAWK_NULL);
OK (json != HAWK_NULL, "instantiation must be successful"); OK (json != HAWK_NULL, "instantiation must be successful");
/* hawk_json_feed(json, "{\"hello\": \"world\"}", & TODO */ n = hawk_json_feed(json, HAWK_T("{\"hello\": \"world\", \"key1\": 12345}"), 33, &xlen);
OK_X(n == 0);
OK_X(xlen == 33);
hawk_json_close(json); hawk_json_close(json);
return exit_status(); return exit_status();