FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

stringlib startsWith, endsWith

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
Ligustah



Joined: 21 Oct 2007
Posts: 45
Location: Berlin, Germany

PostPosted: Sun Oct 28, 2007 11:15 am    Post subject: stringlib startsWith, endsWith Reply with quote

Hi!
I thought that the stringlib needed starts-/endsWith, so i made them. They are just a few lines of code:
Code:
   int endsWith(MDState s, uint numParams)
   {
      dchar[] str = s.getContext!(dchar[]);
      dchar[] find = s.getParam!(dchar[])(0);

      s.push(tango.core.Array.rfind(str, find) == str.length-find.length);

      return 1;
   }

   int startsWith(MDState s, uint numParams)
   {
      dchar[] str = s.getContext!(dchar[]);
      dchar[] find = s.getParam!(dchar[])(0);

      s.push(tango.core.Array.find(str, find) == 0);

      return 1;
   }

You have to append them to stringlib.d and add these two lines to the list in the init function of the stringlib:
Code:
         "endsWith"d,   new MDClosure(namespace, &lib.endsWith,   "string.endsWith"),
         "startsWith"d, new MDClosure(namespace, &lib.startsWith, "string.startsWith"),

I think they're pretty useful and i hope you can use them Smile

Mfg Ligustah
Back to top
View user's profile Send private message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Sun Oct 28, 2007 4:14 pm    Post subject: Reply with quote

Good idea, I'll put them in. Smile
Back to top
View user's profile Send private message
Ligustah



Joined: 21 Oct 2007
Posts: 45
Location: Berlin, Germany

PostPosted: Tue Oct 30, 2007 6:49 am    Post subject: Reply with quote

With a few changes you could also add istarsWith and iendsWith, if you do not have them yet Very Happy :
Code:
   int istartsWith(MDState s, uint numParams)
   {
      dchar[] str = toLowerD(s.getContext!(dchar[]));
      dchar[] find = toLowerD(s.getParam!(dchar[])(0));

      s.push(tango.core.Array.find(str, find) == 0);

      return 1;
   }

   int iendsWith(MDState s, uint numParams)
   {
      dchar[] str = toLowerD(s.getContext!(dchar[]));
      dchar[] find = toLowerD(s.getParam!(dchar[])(0));

      s.push(tango.core.Array.rfind(str, find) == str.length-find.length);

      return 1;
   }


Mfg Ligustah
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group