Re: Brian Kernighan, maybe I'm not worthy, maybe I'm scum
- From: laraine <lariadc@xxxxxxxxx>
- Date: Sat, 12 Jan 2008 10:19:20 -0800 (PST)
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[snip]
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.
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;}
.
- Prev by Date: Re: Status of the Kernighan Scum project
- Next by Date: Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- Previous by thread: Re: Brian Kernighan, maybe I'm not worthy, maybe I'm scum
- Next by thread: Re: Brian Kernighan, maybe I'm not worthy, maybe I'm scum
- Index(es):
Relevant Pages
|