chiark / gitweb /
hbytes compare
authorian <ian>
Sun, 8 Sep 2002 17:10:35 +0000 (17:10 +0000)
committerian <ian>
Sun, 8 Sep 2002 17:10:35 +0000 (17:10 +0000)
base/chiark-tcl.h
base/hook.c
base/tables-examples.tct
hbytes/hbytes.h
hbytes/hook.c

index 37697bb7dfa7e3db01cf061d99c2afbcc3148d7c..4422d328c99e3914243f76a7174b376f6b90e809 100644 (file)
  *  hbytes chopto VAR NEWVARLENGTH               => suffix (removed from VAR)
  *                                                  (too short? error)
  *
  *  hbytes chopto VAR NEWVARLENGTH               => suffix (removed from VAR)
  *                                                  (too short? error)
  *
+ *  hbytes compare A B
+ *      =>  -2   A is lexically earlier than B and not a prefix of B  (A<B)
+ *          -1   A is prefix of B but not equal                       (A<B)
+ *           0   A == B
+ *          +1   A is B plus a nonempty suffix (ie, A has B as a prefix)
+ *          +2   A is lexically later than B and does not have B as a prefix
+ *
  *  hbytes pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
  *  hbytes pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
index e53af08ba8e8fd8dd01cafa1c263a71df1698899..8774f7b6a3fb174a5588305052cb65dc1f8dff93 100644 (file)
@@ -177,6 +177,26 @@ int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
   return TCL_OK;
 }
 
+int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
+                     HBytes_Value a, HBytes_Value b, int *result) {
+  int al, bl, minl, r;
+
+  al= hbytes_len(&a);
+  bl= hbytes_len(&b);
+  minl= al<bl ? al : bl;
+
+  r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
+  
+  if (r<0) *result= -2;
+  else if (r>0) *result= +2;
+  else {
+    if (al<bl) *result= -1;
+    else if (al>bl) *result= +1;
+    else *result= 0;
+  }
+  return TCL_OK;
+}
+
 int do__hbytes(ClientData cd, Tcl_Interp *ip,
               const HBytes_SubCommand *subcmd,
               int objc, Tcl_Obj *const *objv) {
 int do__hbytes(ClientData cd, Tcl_Interp *ip,
               const HBytes_SubCommand *subcmd,
               int objc, Tcl_Obj *const *objv) {
index 37286adb6bcf5597d479aaa4dfbcb9534b59cedd..6bdc7ff4afc95966cbc0182826be367ce232524f 100644 (file)
@@ -43,6 +43,10 @@ Table hbytes HBytes_SubCommand
        length
                v       hb
                =>      int
        length
                v       hb
                =>      int
+       compare
+               a       hb
+               b       hb
+               =>      int
        prepend
                v       hbv
                str     ...
        prepend
                v       hbv
                str     ...
index 37697bb7dfa7e3db01cf061d99c2afbcc3148d7c..4422d328c99e3914243f76a7174b376f6b90e809 100644 (file)
  *  hbytes chopto VAR NEWVARLENGTH               => suffix (removed from VAR)
  *                                                  (too short? error)
  *
  *  hbytes chopto VAR NEWVARLENGTH               => suffix (removed from VAR)
  *                                                  (too short? error)
  *
+ *  hbytes compare A B
+ *      =>  -2   A is lexically earlier than B and not a prefix of B  (A<B)
+ *          -1   A is prefix of B but not equal                       (A<B)
+ *           0   A == B
+ *          +1   A is B plus a nonempty suffix (ie, A has B as a prefix)
+ *          +2   A is lexically later than B and does not have B as a prefix
+ *
  *  hbytes pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
  *  hbytes pkcs5 pa|ua VAR ALG                   => worked?  (always 1 for p)
  *  hbytes pkcs5 pn|un VAR BLOCKSIZE             => worked?  (always 1 for p)
  *  hbytes blockcipher d|e VAR ALG KEY MODE [IV] => IV
index e53af08ba8e8fd8dd01cafa1c263a71df1698899..8774f7b6a3fb174a5588305052cb65dc1f8dff93 100644 (file)
@@ -177,6 +177,26 @@ int do_hbytes_zeroes(ClientData cd, Tcl_Interp *ip,
   return TCL_OK;
 }
 
   return TCL_OK;
 }
 
+int do_hbytes_compare(ClientData cd, Tcl_Interp *ip,
+                     HBytes_Value a, HBytes_Value b, int *result) {
+  int al, bl, minl, r;
+
+  al= hbytes_len(&a);
+  bl= hbytes_len(&b);
+  minl= al<bl ? al : bl;
+
+  r= memcmp(hbytes_data(&a), hbytes_data(&b), minl);
+  
+  if (r<0) *result= -2;
+  else if (r>0) *result= +2;
+  else {
+    if (al<bl) *result= -1;
+    else if (al>bl) *result= +1;
+    else *result= 0;
+  }
+  return TCL_OK;
+}
+
 int do__hbytes(ClientData cd, Tcl_Interp *ip,
               const HBytes_SubCommand *subcmd,
               int objc, Tcl_Obj *const *objv) {
 int do__hbytes(ClientData cd, Tcl_Interp *ip,
               const HBytes_SubCommand *subcmd,
               int objc, Tcl_Obj *const *objv) {