Re: Brian Kernighan, maybe I'm not worthy, maybe I'm scum



On Dec 26 2007, 12:38 am, spinoza1111 <spinoza1...@xxxxxxxxx> wrote:
This is a blog post atwww.developerDotStar.com, reposted here for
useful comments. It's a critique of the "Beautiful" code authored by
Rob Pike and discussed by Brian Kernighan in a new O'Reilly book.
Because I don't use C (it's a pernicious language) I may have missed
something.


[snip]
The most serious flaw is that it uses the test parameter of match as
the index to the test "string" (which is only painfully a modern
string, as we'll see, whence the scare quotes).

Yes, Brian, I understand that in C the programmer can use a name as a
Von Neumann address to a byte now and forever, amen. I understand that
test is passed as are all variables in C by value, therefore on a
stack implementation (yes, the only possible implementation) the
programmer is "free", o happy day, to use the stack copy, test itself,
oh frabjous day, as the index itself.

"Freedom, hoy-day, freedom!" - Shakespeare, The Tempest

But I question the right of the C programmer to use something so
idiomatic as confusing a value parameter, something which is not
normally modifiable, as modifiable.

[snip]

Sorry to bother you on this newsgroup, spinoza, but I once
enjoyed parts of K&R's C book and liked the way they
seemed to build up a toolset, so thought I'd give this one at
least a look. I might be quite ignorant here, but I should
learn anyway what's going on.

Where exactly is 'text' used as an index?
And if something is just used as an index,
isn't that just read-only, not modifying it?

When one uses *text as a parameter in C, it
is the pointer that is being passed as the value
parameter. What exactly is wrong with dereferencing
that pointer? That is not the same thing as looking at
or modifying the value of the pointer. Perhaps that was not
what you were objecting to, though. I'm getting the impression
that you perhaps just don't like pointers and are just criticizing
C rather than Pike's particular program.

I do agree that '1' might not be the best return value, and
that is a refinement that one could make, I suppose, but
I imagine Pike was just trying a simple case.

I'm not sure that you're going to gain too many supporters
by attacking one writer in order to defend another writer.
Why not just defend the other writer with specific examples...

C.



Here's the code wrapped in a value class for Microsoft C++ Visual
Studio with a simple main().

public value class kernighanRegexC
{
public:
/* match: search for regexp anywhere in text */
static int match(char *regexp, char *text)
{
if (regexp[0] == '^')
return matchhere(regexp+1, text);
do { /* must look even if string is empty */
if (matchhere(regexp, text))
return 1;} while (*text++ != '\0');
return 0;
}

private:
/* matchhere: search for regexp at beginning of text */
static int matchhere(char *regexp, char *text)
{
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0], regexp+2, text);
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text))
return matchhere(regexp+1, text+1);
return 0;

}

/* matchstar: search for c*regexp at beginning of text */
static int matchstar(int c, char *regexp, char *text)
{
do { /* a * matches zero or more instance */
if (matchhere(regexp, text))
return 1;

} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}
};

int main(array ^args)
{
Console::WriteLine((kernighanRegexC::match("C", "DDC")).ToString());
return 0;}

.



Relevant Pages

  • Re: what different between 1 and 2, why?
    ... both have implicit assumptions about the sizes of data types ... The first does indeed make the assumption that the size of a pointer ... to char is exactly one byte. ... programmer does not need to know or care what it is. ...
    (comp.lang.c)
  • Re: pointers
    ... > good C programmer, ... I am new to strcpy and I've never used malloc. ... What's happening to this char *p? ... What is happening to this pointer as you go down through this code? ...
    (comp.lang.c)
  • pointers
    ... good C programmer, ... What's happening to this char *p? ... It's being assigned malloc() ... What is happening to this pointer as you go down through this code? ...
    (comp.lang.c)
  • Re: Problem with va_ macros and arrays of arrays
    ... > the arrays passed to a ... > specific char, somewhat similar to what the standard function ... that with an array of struct, or possibly a pointer to a dynamic array ... > As I'm still a beginner in C without a copy of the standard I ...
    (comp.lang.c)
  • [PATCH] 2.6.8 synclink_cs.c replace syncppp with genhdlc
    ... static int ioctl_common; ... -static void mgslpc_setup(struct net_device *dev) ... * dev pointer to network device structure ...
    (Linux-Kernel)