poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Search function


From: apache2
Subject: Search function
Date: Tue, 22 Mar 2022 16:47:35 +0100
User-agent: Mutt/1.9.3 (2018-01-21)

Hi poke-devel,

Below is my "search" function that currently resides in the runtime/standard 
library for the editor I'm hacking on. I believe it has general utility outside 
of the editor and thought I'd share it with pokers around the world in case 
anybody else finds it useful, or has ideas for improvements.

/**** searching: */

type __pd_search_result = struct {
  offset<uint<64>,1> start;
  offset<uint<64>,1> end;
};

fun __pd_search = (Pk_Type typ, int32 fd, offset<uint<64>,1> off, 
offset<uint64,1>align=1#B) __pd_search_result:
{
// args: 0:strict, 1:ios, 2:bitoffset 3:no idea 4:no idea
   try {
     try {
       var found = typ.mapper(1, fd, off'magnitude*off'unit,0,0); // typ @ fd : 
offset;
       var end = found'offset + found'size;
       return __pd_search_result{
         start=found'offset,
         end=end+alignto(end, align)};
     } until E_constraint;
     off += align;
   } until E_eof;
   raise E_eof;
};
/* example 1: search for a string in /dev/urandom with len > 4
{
  type x = {string y : y'length > 4;}
  var fd = open("/dev/urandom");
  x @ fd: __pd_search(typeof(x), fd, 0#b).start
}
*/
 /* example 2: print all non-overlapping strings in file that have length > 5 
and start with 'p':
{ var fd = open("/bin/id");
  type xx = struct{ string x : x'length > 5 && x[0] == 'p' ;};
  var off = 0#b;
  try {
    var res = __pd_search(typeof(xx), fd, off);
    printf("%v: %v\n", res.start, (xx @ fd : res.start).x);
    off=res.end;
  } until E_eof;
}
*/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]