--- Simon Strandgaard <neoneye / gmail.com> wrote: > On 4/28/05, Eric Mahurin <eric_mahurin / yahoo.com> wrote: > > --- Simon Strandgaard <neoneye / gmail.com> wrote: > [snip] > > > Yeah, I don't have neither insert nor delete. > > > I didn't needed it when I made the lib. I though about > it, > > > but couldn't decide how to approach this problem. > > > > So that the number of methods doesn't blow up, I decided to > > have my "get" and "put" methods take a flags argument to > > provide many functions: > > > > forward/reverse > > move/hold > > read(return original value)/ignore(return length) > > scan or replace vs. delete or insert > > > > Last I counted, I have 12 get functions and 10 put > functions > > (all of which can operator on individual elements or a > > string/array of them). The base class will base all of > these > > on 4 primitives (getdelete before/after and insert > > before/after), but most classes will want to override most > of > > it. > > Watch out not putting too much behavior in a single function > :-) I know. I've been thinking about which way (many methods or a few with flags) to go for a while. Both have their cons, but I've decided on fewer methods with flags. > Can you paste some of your code? I'd like to see it. Let me give you the interface I have for now: # flag bits Reverse = 1 # go in reverse rather than forward Hold = 2 # hold position instead of moving Ignore = 4 # get: return length scanned rather than value Read = 4 # put: return original value rather than length Delete = 8 # get: delete value retrieved Insert = 8 # put: insert rather than replace Next = 0 Prev = Reverse After = Hold # don't increment position like Next Before = Reverse|Hold def get(flags=0,length=nil) # get(Next) # get next element (like getc) # get(Next,2) # get next 2 elements (like read) # get(After) # get an element after (like getc+ungetc) # get(Next,"\n") # get next elements ending in "\n" (like gets) # get(Next|Delete) # delete and return next element # get(Next|Ignore,4) # skip next 4 elements (like seek) def put(flags,value) # put(Next,"X"[0]) # put next element (like putc) # put(Next,"hello") # put next string of elements (like write) # put(Next|Insert," world") # insert rather than replace # put(Next|Read,"bye") # read what "bye" is replacing def position(mark=true,&code) # p = position() # get position for setting later # p = position(false) # get position for observe only # match = position { get(Next,5)=="hello" } # back if mismatch # the above is great for parsing def position=(p,mark=true) # position=(p) # set position (you can still set to p later) # position=(p,false) # set position and discard p def unposition(p) # unposition(p) # discard p def position?(p=nil) # position?(p) # is this a valid position to set to? # position? # are there any valid positions to set to? After looking at your stuff, I think I may add some other routines: def put_to(p,flags,value) position { position=p; ret = put(flags,value); false } ret end def get_from(p,flags,length) position { position=p; ret = get(flags,value); false } ret end def copy_from(p,gflags,pflags,length) gflags &= ~Ignore position { position=p; v = get(gflags,length); false } put(pflags,v) end def copy_to(p,gflags,pflags,length) gflags &= ~Ignore v = get(gflags,length) position { position=p; ret = put(pflags,v); false } put(pflags,ret) end With the above routines, you can really see where having flags rather than separate routines becomes very handy. Without it, there would be too many variations. __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com