Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #58580 > unrolled thread
| Started by | jacob navia <jacob@jacob.remcomp.fr> |
|---|---|
| First post | 2015-03-21 16:38 +0100 |
| Last post | 2015-03-21 22:46 +0000 |
| Articles | 10 — 7 participants |
Back to article view | Back to comp.lang.c
Code for strlen jacob navia <jacob@jacob.remcomp.fr> - 2015-03-21 16:38 +0100
Re: Code for strlen Kaz Kylheku <kaz@kylheku.com> - 2015-03-21 16:09 +0000
Re: Code for strlen Greg Martin <greg.at.softsprocket@dot.com> - 2015-03-21 16:35 +0000
Re: Code for strlen Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-21 09:41 -0700
Re: Code for strlen Lőrinczy Zsigmond <nospam@for.me> - 2015-03-21 20:27 +0100
Re: Code for strlen Kaz Kylheku <kaz@kylheku.com> - 2015-03-21 17:13 +0000
Re: Code for strlen Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-03-21 09:35 -0700
Re: Code for strlen Keith Thompson <kst-u@mib.org> - 2015-03-21 15:01 -0700
Re: Code for strlen jacob navia <jacob@jacob.remcomp.fr> - 2015-03-21 18:25 +0100
Re: Code for strlen Aleksandar Kuktin <akuktin@gmail.com> - 2015-03-21 22:46 +0000
| From | jacob navia <jacob@jacob.remcomp.fr> |
|---|---|
| Date | 2015-03-21 16:38 +0100 |
| Subject | Code for strlen |
| Message-ID | <mek3a7$rvp$1@speranza.aioe.org> |
I have spent a week trying to get the strlen function right. It is
very complicated and I am still not sure if it produces the correct
results in all cases. Maybe we can together make a strlen function
that is better than what gcc has to offer ( :-) )
Interesting issues were what happens when NULL is passed to strlen? I
could not find any reference to the standard and all page references
that I got so far were showing completely different content. I am very
confused and do not grasp the concept that there are many standards. I
thought there was only one C standard. How can then the pages be
different from my version to other versions. It does not make sense
and I think it is a deliberate attempt to shut me up by gcc lovers who
infiltrated the C standard committee.
Thanks in advance for your comments.
jacob
P.S. COPYRIGHT:
This code is my work. You may only read it if you agree that my coding
style and my approach to the strlen() problem is the best (especially
better than gcc).
Enjoy!
/*----------------------------------------------------------------
* strlen() - Return the string length of a string by returning a
* integer value that is directly proportional to the length of the
* passed string back to the caller who can then do meaningful
* calculations with it
*/
#include <stdio.h>
#include <math.h>
/* The efficiency constant */
#define LCC_JACOB_EFFICIENCY_CONSTANT 42
/* For good performance, use lookup table */
static unsigned char strlen_lookup[16] = {
0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1
};
/* Do the bits so they go into the lookup table */
static size_t chrlen(char c) {
int index;
index = (c | (c >> 4)) & 0x0f;
return strlen_lookup[index];
}
static int offsetpointerstrlen(const char *s) {
// Convert string pointer to double
double *spos = (double*)s;
double initial_value = *spos;
if (initial_value < 0) {
initial_value = -initial_value;
}
double value = initial_value;
int i;
for (i = 0; i < (int)(M_PI * LCC_JACOB_EFFICIENCY_CONSTANT); i++) {
value = sqrt(value);
if (value < 0) {
value = -value;
}
}
return (int)rint(value);
}
/* Perform the algorithm to get as a result the length of the string
also known
* as string length sometimes. */
size_t strlen(const char *s) {
double partlen;
partlen = chrlen(*s);
// Do a partial string length
size_t total_len = partlen;
if (total_len) {
/* Calclate the point where to determine the string next */
const char *next_string = s + offsetpointerstrlen(s);
total_len = total_len + jacobs_strlen(next_string);
}
return total_len;
}
[toc] | [next] | [standalone]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Date | 2015-03-21 16:09 +0000 |
| Message-ID | <20150321090457.184@kylheku.com> |
| In reply to | #58580 |
On 2015-03-21, jacob navia <jacob@jacob.remcomp.fr> wrote:
> I have spent a week trying to get the strlen function right. It is
> very complicated and I am still not sure if it produces the correct
> results in all cases. Maybe we can together make a strlen function
> that is better than what gcc has to offer ( :-) )
>
> Interesting issues were what happens when NULL is passed to strlen? I
> could not find any reference to the standard and all page references
> that I got so far were showing completely different content.
Have you looked in C99 7.1.4 Use of Library Functions.
"Each of the following statements applies unless explicitly stated otherwise in
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the detailed descriptions that follow: If an argument to a function has an
invalid value (such as a value outside the domain of the function, or a pointer
outside the address space of the program, or a null pointer, or a pointer to
^^^^^^^^^^^^^^^^^
non-modifiable storage when the corresponding parameter is not const-qualified)
or a type (after promotion) not expected by a function with variable number of
arguments, the behavior is undefined."
^^^^^^^^^^^^^^^^^^^^^^^^^^
So, for any function that has a pointer argument, unless the description of
the function allows a null pointer, it is undefined behavior to pass one.
[toc] | [prev] | [next] | [standalone]
| From | Greg Martin <greg.at.softsprocket@dot.com> |
|---|---|
| Date | 2015-03-21 16:35 +0000 |
| Message-ID | <Z4hPw.66468$Lw2.55565@fx30.iad> |
| In reply to | #58583 |
On 2015-03-21, Kaz Kylheku <kaz@kylheku.com> wrote: > On 2015-03-21, jacob navia <jacob@jacob.remcomp.fr> wrote: >> I have spent a week trying to get the strlen function right. It is >> very complicated and I am still not sure if it produces the correct >> results in all cases. Maybe we can together make a strlen function >> that is better than what gcc has to offer ( :-) ) >> >> Interesting issues were what happens when NULL is passed to strlen? I >> could not find any reference to the standard and all page references >> that I got so far were showing completely different content. > > Have you looked in C99 7.1.4 Use of Library Functions. > > "Each of the following statements applies unless explicitly stated otherwise in > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ > the detailed descriptions that follow: If an argument to a function has an > invalid value (such as a value outside the domain of the function, or a pointer > outside the address space of the program, or a null pointer, or a pointer to > ^^^^^^^^^^^^^^^^^ > non-modifiable storage when the corresponding parameter is not const-qualified) > or a type (after promotion) not expected by a function with variable number of > arguments, the behavior is undefined." > ^^^^^^^^^^^^^^^^^^^^^^^^^^ > > So, for any function that has a pointer argument, unless the description of > the function allows a null pointer, it is undefined behavior to pass one. So, that means he can handle the situation anyway he chooses? -- http://www.softsprocket.com
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-03-21 09:41 -0700 |
| Message-ID | <33002fd7-b493-44e5-bc54-010fb89786b8@googlegroups.com> |
| In reply to | #58586 |
On Saturday, March 21, 2015 at 4:35:17 PM UTC, Greg Martin wrote: > > > So, for any function that has a pointer argument, unless the description > > the function allows a null pointer, it is undefined behavior to pass one. > > So, that means he can handle the situation anyway he chooses? > The OP is a troll post by someone pretending to be Jacob. Better make that clear. But the whole point of undefined behaviour is that the compiler writer can make the program do anything. So you can return zero, return -1 (cast to a size_t these days), crash out with a nice error message, or just read from memory address zero and see what happens. On a tiny system, the memory needed to hold the error message might be prohibitive. On a typical system, it's usually the best option, unless the system is mission critical in a no-backup environment, when you've just got to try to suppress errors and hope.
[toc] | [prev] | [next] | [standalone]
| From | Lőrinczy Zsigmond <nospam@for.me> |
|---|---|
| Date | 2015-03-21 20:27 +0100 |
| Message-ID | <mekgmr$4fo$1@speranza.aioe.org> |
| In reply to | #58589 |
On 2015.03.21. 17:41, Malcolm McLean wrote:
> The OP is a troll post by someone pretending to be Jacob.
> Better make that clear.
I guessed that, too;
but I still would like to point out than strlen
might be trivial, but strnlen isn't;
at least I've seen two different
platform where it didn't work correctly.
1. BS2000 calculated strnlen (ptr, maxlen) as
min (strlen (ptr), maxlen)
2. AIX 5.old gave very strange results:
strnlen("JV02B ",1)=3
strnlen("JV02B ",2)=2
strnlen("JV02B ",3)=1
strnlen("JV02B ",4)=0
[toc] | [prev] | [next] | [standalone]
| From | Kaz Kylheku <kaz@kylheku.com> |
|---|---|
| Date | 2015-03-21 17:13 +0000 |
| Message-ID | <20150321101037.201@kylheku.com> |
| In reply to | #58586 |
On 2015-03-21, Greg Martin <greg.at.softsprocket@dot.com> wrote: > On 2015-03-21, Kaz Kylheku <kaz@kylheku.com> wrote: >> On 2015-03-21, jacob navia <jacob@jacob.remcomp.fr> wrote: >>> I have spent a week trying to get the strlen function right. It is >>> very complicated and I am still not sure if it produces the correct >>> results in all cases. Maybe we can together make a strlen function >>> that is better than what gcc has to offer ( :-) ) >>> >>> Interesting issues were what happens when NULL is passed to strlen? I >>> could not find any reference to the standard and all page references >>> that I got so far were showing completely different content. >> >> Have you looked in C99 7.1.4 Use of Library Functions. >> >> "Each of the following statements applies unless explicitly stated otherwise in >> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >> the detailed descriptions that follow: If an argument to a function has an >> invalid value (such as a value outside the domain of the function, or a pointer >> outside the address space of the program, or a null pointer, or a pointer to >> ^^^^^^^^^^^^^^^^^ >> non-modifiable storage when the corresponding parameter is not const-qualified) >> or a type (after promotion) not expected by a function with variable number of >> arguments, the behavior is undefined." >> ^^^^^^^^^^^^^^^^^^^^^^^^^^ >> >> So, for any function that has a pointer argument, unless the description of >> the function allows a null pointer, it is undefined behavior to pass one. > > So, that means he can handle the situation anyway he chooses? It means that it is not even necessary to make a choice. "Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, ..." "Ignoring the situation" includes literal ignorance: implementing the feature as if the undefined behavior situation doesn't exist.
[toc] | [prev] | [next] | [standalone]
| From | Malcolm McLean <malcolm.mclean5@btinternet.com> |
|---|---|
| Date | 2015-03-21 09:35 -0700 |
| Message-ID | <a1e3aba9-4133-4012-8723-dd2cd84ed1c3@googlegroups.com> |
| In reply to | #58580 |
On Saturday, March 21, 2015 at 3:38:58 PM UTC, jacob navia wrote: > > /* The efficiency constant */ > #define LCC_JACOB_EFFICIENCY_CONSTANT 42 > Heh heh heh. That one was at least funny. There are some people who really do code like that, though not quite to that extreme. But really posting under another man's name is not on.
[toc] | [prev] | [next] | [standalone]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Date | 2015-03-21 15:01 -0700 |
| Message-ID | <874mpevwmb.fsf@kst-u.example.com> |
| In reply to | #58587 |
Malcolm McLean <malcolm.mclean5@btinternet.com> writes:
> On Saturday, March 21, 2015 at 3:38:58 PM UTC, jacob navia wrote:
>> /* The efficiency constant */
>> #define LCC_JACOB_EFFICIENCY_CONSTANT 42
>>
> Heh heh heh.
> That one was at least funny. There are some people who really do
> code like that, though not quite to that extreme.
Malcolm, I suggest that that's just the kind of reaction the forger is
looking for. Please don't encourage him/her/it/them.
> But really posting under another man's name is not on.
To the forger, whoever you are: You are not funny. You are not clever.
Those of us who have had differences with jacob are not impressed by
your attacks on him. Go away.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
[toc] | [prev] | [next] | [standalone]
| From | jacob navia <jacob@jacob.remcomp.fr> |
|---|---|
| Date | 2015-03-21 18:25 +0100 |
| Message-ID | <mek9he$ec5$1@speranza.aioe.org> |
| In reply to | #58580 |
THIS IS A FORGED MESSAGE Anyway it is more obvious this time...
[toc] | [prev] | [next] | [standalone]
| From | Aleksandar Kuktin <akuktin@gmail.com> |
|---|---|
| Date | 2015-03-21 22:46 +0000 |
| Message-ID | <meksck$3u7$1@speranza.aioe.org> |
| In reply to | #58594 |
On Sat, 21 Mar 2015 18:25:03 +0100, jacob navia wrote: > THIS IS A FORGED MESSAGE > > Anyway it is more obvious this time... PGP signatures? Or am I being paranoid?
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c
csiph-web