trim whitespace v3

K

Keith Thompson

John Kelly said:
When I asked a specific question you provided no answer.

Am I obligated to do so? Was your question answered by others?
How many specific questions of mine have you chosen not to answer?

[...]
I don't know who has more experience. But when you don't answer
specific questions, it's clear you have an agenda with more focus on
asserting yourself than helping individuals.

I was trying to help you in more general ways. I'm not interested
in helping you solve a problem that you're unwilling to define.
 
K

Keith Thompson

John Kelly said:
I understand the principle you advocate. It's used in corporate
environments where the lawyers insist on clean room development with a
Chinese wall between specification and code. The reason is to guard
against possible copyright infringement.
[...]

No, you don't understand it. Seebs explained it in a followup;
read it if you like.
 
F

Francesco S. Carta

Have you tried v3?
# if __STDC_VERSION__>= 199901L
# include<stdint.h>
# endif

<stdint.h> won't be inlcluded, unless the macro above is true. On the
gcc I am using, that means calling gcc with the option:

-std=c99

Sorry, I overlooked the conditional inclusion above. I've just noticed
that the fallback method was getting used, which in turn meant the macro
wasn't defined (I wasn't using the C99 flag).

Definitely missing from both 4.4.0 and 4.5, I confirm - at least from
the packages I have on my box, but I doubt I have deleted any header
from them.

Besides, I miss to see the connection between gcc the page you linked.
 
I

Ian Collins

Am I obligated to do so? Was your question answered by others?
How many specific questions of mine have you chosen not to answer?

He doesn't care, only his questions are important. Don't forget this is
one of "his" threads...
 
K

Keith Thompson

Why, does gcc work only on POSIX systems?
Definitely missing from both 4.4.0 and 4.5, I confirm - at least from
the packages I have on my box, but I doubt I have deleted any header
from them.

Besides, I miss to see the connection between gcc the page you linked.

How did you confirm it?

If this source file:

#include <errno.h>
#ifndef EOVERFLOW
#error "EOVERFLOW is not defined"
#endif

triggers the #error during compilation, you can be pretty sure
that your system doesn't define EOVERFLOW -- though there *might*
be a way to change the compiler's mode so it does some magic to
make it visible.
 
J

John Kelly

Definitely missing from both 4.4.0 and 4.5, I confirm - at least from
the packages I have on my box, but I doubt I have deleted any header
from them.

Besides, I miss to see the connection between gcc the page you linked.

The Open Group Base Specifications Issue 6
IEEE Std 1003.1, 2004 Edition

It lists common symbolic constants. Seems like any gcc environment
would have them.

If I find many environments lacking EOVERFLOW, I may use a different
symbolic constant. But if MinGW is the only platform lacking it, that
problem can wait.
 
J

John Kelly

John Kelly said:
On Mon, 23 Aug 2010 12:06:04 -0700, Keith Thompson <[email protected]>
I understand the principle you advocate. It's used in corporate
environments where the lawyers insist on clean room development with a
Chinese wall between specification and code. The reason is to guard
against possible copyright infringement. [...]

No, you don't understand it. Seebs explained it in a followup;
read it if you like.

I definitely don't care what Seebs thinks. That's another reason I
don't read his spew.
 
K

Keith Thompson

John Kelly said:
John Kelly said:
On Mon, 23 Aug 2010 12:06:04 -0700, Keith Thompson <[email protected]>
I understand the principle you advocate. It's used in corporate
environments where the lawyers insist on clean room development with a
Chinese wall between specification and code. The reason is to guard
against possible copyright infringement. [...]

No, you don't understand it. Seebs explained it in a followup;
read it if you like.

I definitely don't care what Seebs thinks. That's another reason I
don't read his spew.

In this particular case, what he wrote happens to match what I would
have written if I had taken the time to respond.

You claim to understand the principle I advocate. You don't. If you'd
like to, I've told you where to find an explanation.
 
S

Seebs

When I asked a specific question you provided no answer.

That was because the question had no meaningful answer.
I don't know who has more experience. But when you don't answer
specific questions, it's clear you have an agenda with more focus on
asserting yourself than helping individuals.

Not when the specific questions are wrong.

Look, imagine that you were out proselytizing, and someone asked you
what kinds of ritual sex were most essential to Christianity. You
might not actually give them a list of kinds of ritual sex, even though
it was a very specific question. That's because the specific question
shows that the person asking it is way out in left field, and there
is no such thing as an answer to that question that will bring them
closer. (Keep in mind, "none" isn't an answer; they asked for specific
kinds.)

-s
 
S

Seebs

I listened to feedback and made corresponding improvements. The c.l.c
truth is, some of you hate anyone who won't kneel to you.

Amusingly, I actually hate anyone who WILL. I think status games are
seriously dysfunctional, and I won't put up with them.
I bow to a higher authority.

Lip service doesn't count.

-s
 
F

Francesco S. Carta

Why, does gcc work only on POSIX systems?


How did you confirm it?

If this source file:

#include<errno.h>
#ifndef EOVERFLOW
#error "EOVERFLOW is not defined"
#endif

triggers the #error during compilation, you can be pretty sure
that your system doesn't define EOVERFLOW -- though there *might*
be a way to change the compiler's mode so it does some magic to
make it visible.

Already tested that by running the code in the OP of this thread, the
compiler halted at the first instance of EOVERFLOW.

I've searched for that token into all files of the packages, and I found
no (meaningful) occurrence of it. Just once in a comment of an internal
header, but no definition whatsoever (that commented instruction used it
instead of defining it).

If there is any way to force MinGW to magically pull it out from the
compiler binary, that goes beyond my knowledge.
 
S

Seebs

I definitely don't care what Seebs thinks.

Why so personal? I can be a real grade-A jerk, but I happen to know a
fair bit about the subject matter, and I'm usually pretty willing to
answer interesting questions.

Now, I recognize that you don't read my posts, but I suppose this is
something that I think is of use to general readers, whether they program
in C or not:

If you want to learn, it is nearly always devastatingly bad form to
write people off based on personality conflicts. If someone knows the
material, pay attention to what they have to say, and if they annoy
you, deal with it on your own time. As you say, life is short; you
don't have time to be a prima donna. If the advice you need is available
from an asshole, it's time to learn to filter content from the writing
of an asshole.

Robustness in the face of poor behavior is a core skill for ANYONE who
wants to learn to program, or really, who wants to do anything which
may at some point involve other people. It's not an optional thing that
comes long after developing skill at the field.

-s
 
K

Kenny McCormack

You ask for my help, then you refuse to follow my advice.

That's your right, of course, but I don't think there's anything more
I can do for you (unless you change your mind and start listening
to people who have more experience than you do).

You two need to get a room somewhere.

Admit it, Kiki, you're just dyin' to spread your legs for hunky John!

--
(This discussion group is about C, ...)

Wrong. It is only OCCASIONALLY a discussion group
about C; mostly, like most "discussion" groups, it is
off-topic Rorsharch [sic] revelations of the childhood
traumas of the participants...
 
K

Kenny McCormack

When I asked a specific question you provided no answer.



I don't know who has more experience. But when you don't answer
specific questions, it's clear you have an agenda with more focus on
asserting yourself than helping individuals.

Gee, ya think???

She wants you. Bad.
 
K

Kenny McCormack

Tom St Denis said:
Ah, so you *are* a troll like the rest and your gimmick is you write
overly complicated completely amateur code. I guess your ruse is to
show how incapable and clumsy the C language is by incompetently
developing shit software?

You're feeling unloved, aren't you?
 
S

Seebs

In this particular case, what he wrote happens to match what I would
have written if I had taken the time to respond.

Although probably it wasn't as polite.

Partially because, well. Basically, so far as I can tell, Kelly's not
sane, and is not-sane in a way which precludes effectively communicating
with him, so I don't actually put measurable effort into communicating
with him. When he asks questions I think would be interesting to read
an answer to, I try to answer them. When people seem to be getting stuck
on his pathological behaviors, I point out the pattern to save them the
trouble of trying to figure out how to make a narcissist comprehend
any instance of the general set of "other people are not you, but are
just as real as you".

-s
 
J

James Waldby

See "Define ideas"

The more you say, the more likely your words will not match the reality
of the code. I try to keep the code clean and the words few. That way,
you get an overview in "Define ideas," but for the details you must read
the code.

Keeping "the code clean and the words few" is a fine principle but
a higher and more useful principle is to clearly document what the
code is supposed to do. That you have not done; see below.

You shunted aside Keith Thompson's excellent advice (above) with
a terse response, 'See "Define ideas"'. Looking at that section,
it said:

Trim leading and trailing whitespace from any null terminated string
found in memory, no matter whether data or garbage. Reports failure
and quits if null terminator not found within system defined limit.

Encountering such a specification for code to be written, a programmer
might think that the program should look through memory to find any
null terminated strings, and remove leading and trailing whitespace
from each such string. Some programmers might squeeze out such
whitespace by moving the strings together in memory, others might
zero the bytes where such whitespace is found, etc. Some programmers
might use system calls to find out how much memory has been allocated
to your program so they know how much memory to scan, others might try
to enforce a string registration regime so they know which strings to
work on. All in all, it seems likely that n programmers implementing
it will produce n different, incompatible, and useless functions, vs
the desired case of all producing interchangeable functions.

Minor note, keep verb tenses parallel -- avoid Trim/Reports/quits,
use Trim/Report/quit or Trims/Reports/quits.
 
J

James

John Kelly said:
With a 1,000,000 byte string having one space at the front, your fancy
"state machine" performs 999,999 individual reads and stores. My code
moves the whole block all at once. Memmove() may be library optimized
to a single machine instruction.

Why do you need to call memmove() at all? IMVHO, a low-level trim function
should only give you an offset and a length of the trimmed string. You can
then use that information to operate on the trimmed string directly without
resorting to shifting any memory around.

Humm, well, I could understand calling memmove() and realloc() to free up
some space if the trimmed string is significantly smaller than the original
string.
 
M

Moi

I dunno.  I think it's probably a combination of approach and
experience. He's clearly a fairly new programmer, and I think he
deserves credit for at least WANTING to deal with invalid inputs
gracefully.  I think it turns out that, due to the nature of C, his
attempts to do that are causing more harm than good, but it's a good
impulse to have.

The flaw in his approach is that he's not outlining his solution before
writing the code. I'm a professional developer [been working for nearly
a decade in crypto/swdevel] and I still mock out complex functions [like
say PKCS #8 parsers] in comment blocks before writing any C statements.

Exactly.
I would not call myself a professional developer, but I've read and written
enough code to ask the question: "what is this thing supposed to do".

In the case of the trim() function it is obvious:
* the string is supposed to be a valid object, decently null terminated, etc.
* It is writeable, and should be modified in-place.
* trimming it can never make it larger. At most smaller.
--> if it can be done in-place, there can be no memory overwrite problems
--> this function can be written as a helper/support function: no need to do
any error-handling. Errors are impossibble (unless the caller, etc, etc)

Now it is time for strategic thinking: do I need a state machine,
or will strspn+strlen+backstepping+memmove be good enough for the task?

This is the hard part. In some cases I might take a walk or a smoke.
Not in this case: anything goes.
And I do know: I cannot beat a memmove(), unless there is a lot of
state involved.
And I realize that *speed is not important*. Most data I deal with
comes from disk, and disk is 10-100 times slower than CPU. Compared to
page faults (or cache misses), the impact of string/memory operations
is peanuts; just get them right.
(IMHO, Dann Corbit posted the right solution a few days ago
; of course ignored by "John Kelly" )


You know who likes Shakespeare, too ?

HTH,
AvK
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

trim whitespace, bullet proof version 63
trim whitespace 194
trim 6
Trim string 42
Request for source code review of simple Ising model 88
Strange bug 65
malloc and maximum size 56
Dead Code? 4

Members online

No members online now.

Forum statistics

Threads
474,085
Messages
2,570,597
Members
47,219
Latest member
Geraldine7

Latest Threads

Top