Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
July 04, 2013 cmp and icmp. | ||||
---|---|---|---|---|
| ||||
Why don't these functions take a length parameter, like strcmp and memcmp? Is it worth making an enhancement request for such? |
July 04, 2013 Re: cmp and icmp. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Damian | 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
|
July 04, 2013 Re: cmp and icmp. | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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
}
}
|
July 04, 2013 Re: cmp and icmp. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Damian | 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
|
July 04, 2013 Re: cmp and icmp. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Damian | 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
|
July 04, 2013 Re: cmp and icmp. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan M Davis | 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.
|
Copyright © 1999-2021 by the D Language Foundation