www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - cmp and icmp.

reply "Damian" <damianday hotmail.co.uk> writes:
Why don't these functions take a length parameter, like strcmp 
and memcmp?
Is it worth making an enhancement request for such?
Jul 04 2013
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Damian:

 Why don't these functions take a length parameter, like strcmp 
 and memcmp?
 Is it worth making an enhancement request for such?
Probably slicing of their inputs, or using std.range.take() of their inputs, is enough. Do you agree? Bye, bearophile
Jul 04 2013
parent reply "Damian" <damianday hotmail.co.uk> writes:
On Thursday, 4 July 2013 at 22:02:06 UTC, bearophile wrote:
 Damian:

 Why don't these functions take a length parameter, like strcmp 
 and memcmp?
 Is it worth making an enhancement request for such?
Probably slicing of their inputs, or using std.range.take() of their inputs, is enough. Do you agree? Bye, bearophile
Ah but with slicing the inputs, you must check the length first so that you don't get an out of bounds error. I would prefer it if cmp did this for me, since it is going to check lengths anyway. Example below. private void ParsePacket(in char[] data) { static string Good = "GOOD"; static string Bad = "BAD"; if (data.length >= Good.length) // Prefer not to do this check { if (std.algorithm.cmp(data[0 .. Good.length], Good) == 0) } else if { // Check Bad, and so on } }
Jul 04 2013
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Damian:

 private void ParsePacket(in char[] data)
 {
     static string Good = "GOOD";
     static string Bad = "BAD";

     if (data.length >= Good.length) // Prefer not to do this 
 check
     {
         if (std.algorithm.cmp(data[0 .. Good.length], Good) == 
 0)
     }
     else if
     {
         // Check Bad, and so on
     }
 }
To compare if two strings (or char arrays) are equal use ==, or use a switch. For your case you can use: import std.stdio, std.string; void parsePacket(in char[] data) { if (data.startsWith("GOOD")) { "very good".writeln; } else if (data.startsWith("BAD")) { "very bad".writeln; } else { "something else".writeln; // ... } } void main() { parsePacket("BAD_DAY"); } Note that idiomatic D variables and functions usually start with a lowercase. It's better to ask similar questions in the D.learn newsgroup. Bye, bearophile
Jul 04 2013
prev sibling parent reply Jonathan M Davis <jmdavisProg gmx.com> writes:
On Friday, July 05, 2013 00:36:02 Damian wrote:
 On Thursday, 4 July 2013 at 22:02:06 UTC, bearophile wrote:
 Damian:
 Why don't these functions take a length parameter, like strcmp
 and memcmp?
 Is it worth making an enhancement request for such?
Probably slicing of their inputs, or using std.range.take() of their inputs, is enough. Do you agree? Bye, bearophile
Ah but with slicing the inputs, you must check the length first so that you don't get an out of bounds error. I would prefer it if cmp did this for me, since it is going to check lengths anyway.
Except that cmp doesn't check the length. It just keeps popping elements until one of the two ranges is empty. Taking a length which it had to check would make it _less_ efficient, not more. The correct thing to do here is indeed to slice the range if it can be sliced or to use take if it can't be sliced. And if you want to slice the range and are worried about length, then just use min to make sure that you don't pass a length which is too large. - Jonathan M Davis
Jul 04 2013
parent "Damian" <damianday hotmail.co.uk> writes:
On Thursday, 4 July 2013 at 23:10:12 UTC, Jonathan M Davis wrote:
 On Friday, July 05, 2013 00:36:02 Damian wrote:
 On Thursday, 4 July 2013 at 22:02:06 UTC, bearophile wrote:
 Damian:
 Why don't these functions take a length parameter, like 
 strcmp
 and memcmp?
 Is it worth making an enhancement request for such?
Probably slicing of their inputs, or using std.range.take() of their inputs, is enough. Do you agree? Bye, bearophile
Ah but with slicing the inputs, you must check the length first so that you don't get an out of bounds error. I would prefer it if cmp did this for me, since it is going to check lengths anyway.
Except that cmp doesn't check the length. It just keeps popping elements until one of the two ranges is empty. Taking a length which it had to check would make it _less_ efficient, not more. The correct thing to do here is indeed to slice the range if it can be sliced or to use take if it can't be sliced. And if you want to slice the range and are worried about length, then just use min to make sure that you don't pass a length which is too large. - Jonathan M Davis
In the example I gave startsWith is fine, but I also need case insensitive comparison, take and icmp, however, does seem to fit the bill perfectly.
Jul 04 2013