X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ian/git?a=blobdiff_plain;f=hbytes%2Fchop.c;h=3a172012520571e4120a7849dbc6d96b586877ef;hb=refs%2Fheads%2Fmaster;hp=49995339b2fdcf43d5919054d5b49904b2152d33;hpb=4ab162065d72217bcad27748442cbb27860c0cbd;p=chiark-tcl.git
diff --git a/hbytes/chop.c b/hbytes/chop.c
index 4999533..3a17201 100644
--- a/hbytes/chop.c
+++ b/hbytes/chop.c
@@ -1,61 +1,104 @@
/*
+ * hbytes - hex-stringrep efficient byteblocks for Tcl
+ * Copyright 2006-2012 Ian Jackson
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this library; if not, see .
*/
-#include
-#include "hbytes.h"
+#include "chiark_tcl_hbytes.h"
-static int strs(Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, int *l_r) {
- int rc, l, i;
+static int strs1(Tcl_Interp *ip, int strc, Tcl_Obj *const *strv, int *l_r) {
+ int rc, l, i, pl;
l= 0;
for (i=1; istart= v.hb->end= Tcl_Realloc(v.hb->start, ol + al);
-
- if (begin) v.hb->end += ol;
- else memmove(v.hb->start + al, v.hb->start, ol);
+static void strs2(Byte *dest, int strc, Tcl_Obj *const *strv) {
+ int tl, i;
for (i=1; iend, HBYTES(strv[i]).start, tl);
- v.hb->end += tl;
+ tl= cht_hb_len(OBJ_HBYTES(strv[i]));
+ memcpy(dest, cht_hb_data(OBJ_HBYTES(strv[i])), tl);
+ dest += tl;
}
- return TCL_OK;
-}
+}
-int do_hbytes_append(ClientData cd, Tcl_Interp *ip,
- HBytes_Var v, int strc, Tcl_Obj *const *strv) {
- return app_pre(cd,ip,0,v,strc,strv);
+int cht_do_hbytes_prepend(ClientData cd, Tcl_Interp *ip,
+ HBytes_Var v, int strc, Tcl_Obj *const *strv) {
+ int rc, el;
+ Byte *dest;
+
+ rc= strs1(ip,strc,strv,&el); if (rc) return rc;
+ dest= cht_hb_prepend(v.hb, el);
+ strs2(dest, strc,strv);
+ return TCL_OK;
}
-int do_hbytes_prepend(ClientData cd, Tcl_Interp *ip,
- HBytes_Var v, int strc, Tcl_Obj *const *strv) {
- return app_pre(cd,ip,1,v,strc,strv);
+int cht_do_hbytes_append(ClientData cd, Tcl_Interp *ip,
+ HBytes_Var v, int strc, Tcl_Obj *const *strv) {
+ int rc, el;
+ Byte *dest;
+
+ rc= strs1(ip,strc,strv,&el); if (rc) return rc;
+ dest= cht_hb_append(v.hb, el);
+ strs2(dest, strc,strv);
+ return TCL_OK;
}
-int do_hbytes_concat(ClientData cd, Tcl_Interp *ip,
+int cht_do_hbytes_concat(ClientData cd, Tcl_Interp *ip,
int strc, Tcl_Obj *const *strv, HBytes_Value *result) {
- HBytes_Var fake;
+ int rc, l;
+ Byte *dest;
- result->start= result->end= 0;
- fake.hb= result;
- return app_pre(cd,ip,1,fake,strc,strv);
+ rc= strs1(ip,strc,strv,&l); if (rc) return rc;
+ dest= cht_hb_arrayspace(result,l);
+ strs2(dest, strc,strv);
+ return TCL_OK;
+}
+
+static int underrun(Tcl_Interp *ip) {
+ return cht_staticerr(ip,"data underrun","HBYTES LENGTH UNDERRUN");
}
+int cht_do_hbytes_unprepend(ClientData cd, Tcl_Interp *ip,
+ HBytes_Var v, int preflength, HBytes_Value *result) {
+ const Byte *rdata= cht_hb_unprepend(v.hb, preflength);
+ if (!rdata) return underrun(ip);
+ cht_hb_array(result, rdata, preflength);
+ return TCL_OK;
+}
+
+int cht_do_hbytes_unappend(ClientData cd, Tcl_Interp *ip,
+ HBytes_Var v, int suflength, HBytes_Value *result) {
+ const Byte *rdata= cht_hb_unappend(v.hb, suflength);
+ if (!rdata) return underrun(ip);
+ cht_hb_array(result, rdata, suflength);
+ return TCL_OK;
+}
+
+int cht_do_hbytes_chopto(ClientData cd, Tcl_Interp *ip,
+ HBytes_Var v, int newlength, HBytes_Value *result) {
+ int suflength= cht_hb_len(v.hb) - newlength;
+ return cht_do_hbytes_unappend(0,ip,v, suflength, result);
+}