4 #include "chiark_tcl_hbytes.h"
6 static int strs1(Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, int *l_r) {
10 for (i=1; i<strc; i++) {
11 rc= Tcl_ConvertToType(ip,strv[i],&cht_hbytes_type);
13 l += cht_hb_len(OBJ_HBYTES(strv[i]));
19 static void strs2(Byte *dest, int strc, Tcl_Obj *const *strv) {
22 for (i=1; i<strc; i++) {
23 tl= cht_hb_len(OBJ_HBYTES(strv[i]));
24 memcpy(dest, cht_hb_data(OBJ_HBYTES(strv[i])), tl);
29 int cht_do_hbytes_prepend(ClientData cd, Tcl_Interp *ip,
30 HBytes_Var v, int strc, Tcl_Obj *const *strv) {
34 rc= strs1(ip,strc,strv,&el); if (rc) return rc;
35 dest= cht_hb_prepend(v.hb, el);
36 strs2(dest, strc,strv);
40 int cht_do_hbytes_append(ClientData cd, Tcl_Interp *ip,
41 HBytes_Var v, int strc, Tcl_Obj *const *strv) {
45 rc= strs1(ip,strc,strv,&el); if (rc) return rc;
46 dest= cht_hb_append(v.hb, el);
47 strs2(dest, strc,strv);
51 int cht_do_hbytes_concat(ClientData cd, Tcl_Interp *ip,
52 int strc, Tcl_Obj *const *strv, HBytes_Value *result) {
56 rc= strs1(ip,strc,strv,&l); if (rc) return rc;
57 dest= cht_hb_arrayspace(result,l);
58 strs2(dest, strc,strv);
62 static int underrun(Tcl_Interp *ip) {
63 return cht_staticerr(ip,"data underrun","HBYTES LENGTH UNDERRUN");
66 int cht_do_hbytes_unprepend(ClientData cd, Tcl_Interp *ip,
67 HBytes_Var v, int preflength, HBytes_Value *result) {
68 const Byte *rdata= cht_hb_unprepend(v.hb, preflength);
69 if (!rdata) return underrun(ip);
70 cht_hb_array(result, rdata, preflength);
74 int cht_do_hbytes_unappend(ClientData cd, Tcl_Interp *ip,
75 HBytes_Var v, int suflength, HBytes_Value *result) {
76 const Byte *rdata= cht_hb_unappend(v.hb, suflength);
77 if (!rdata) return underrun(ip);
78 cht_hb_array(result, rdata, suflength);
82 int cht_do_hbytes_chopto(ClientData cd, Tcl_Interp *ip,
83 HBytes_Var v, int newlength, HBytes_Value *result) {
84 int suflength= cht_hb_len(v.hb) - newlength;
85 return cht_do_hbytes_unappend(0,ip,v, suflength, result);