digitalmars.D - cmp and icmp.
- Damian (3/3) Jul 04 2013 Why don't these functions take a length parameter, like strcmp
- bearophile (5/8) Jul 04 2013 Probably slicing of their inputs, or using std.range.take() of
- Damian (19/27) Jul 04 2013 Ah but with slicing the inputs, you must check the length first
- bearophile (23/38) Jul 04 2013 To compare if two strings (or char arrays) are equal use ==, or
- Jonathan M Davis (8/24) Jul 04 2013 Except that cmp doesn't check the length. It just keeps popping elements...
- Damian (4/35) Jul 04 2013 In the example I gave startsWith is fine, but I also need case
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
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
On Thursday, 4 July 2013 at 22:02:06 UTC, bearophile wrote:Damian: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 } }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
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
On Friday, July 05, 2013 00:36:02 Damian wrote:On Thursday, 4 July 2013 at 22:02:06 UTC, bearophile wrote: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 DavisDamian: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.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
On Thursday, 4 July 2013 at 23:10:12 UTC, Jonathan M Davis wrote:On Friday, July 05, 2013 00:36:02 Damian wrote: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.On Thursday, 4 July 2013 at 22:02:06 UTC, bearophile wrote: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 DavisDamian: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.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