Apple writes bad co-

Apple writes bad co-

Other urls found in this thread:

tsunanet.net/~tsuna/strlen.c.html
git.musl-libc.org/cgit/musl/tree/src/string/strlen.c
cdecl.org/
man.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man9/style.9?query=style
twitter.com/NSFWRedditImage

-de


fixed that for you

That's pretty clever

wheres that from

what does this do?

Elegant

>const pointer
What the fug

Im retarded can someone explain this to me like im 7 years old

That's why it's bad.
Any programmer worth their salt knows that clever code is bad code.

t. salty code monkey with 30k salary who does it 'close to the metal' aka programs 10 stack underflows a day into his precious C code and hates on 'liberal fags' who work for apple and develop clever code for 200k salaries and drive teslas around

>faggots impressed by this
Jesus, where are all the C hackers on Sup Forums?

not sure if 7, but I'll try 17.
it creates a pointer and sets it to the address of str. it continues to increment the address until it can no longer be successfully dereferenced (the middle clause of the for-loop). That way you know that the pointer s is at the end of the string str. The length of the string then is simply the pointer difference between s and str.

>all that butthurt
You don't need to be upset if you don't know the basics of programming and it's principles, user. It's not for everyone.

t. Pajeet

It's a mix of ancient C code (register, K&R arguments) with modern characteristics (const pointers).

Basically 's' is a pointer that starts at the beginning of the string and continues advancing until it points at a 0 (because in C everything different from 0 is "true", who the fucks needs actual booleans). Then returns the number of chars advanced, and that's the string length.

See

>it continues to increment the address until it can no longer be successfully dereferenced (the middle clause of the for-loop).
No, the loop condition is simply the value that s points to, and it will only evaluate to false when the 0-terminator is reached.

>register
address_for_the_end_of the_string - address_for_the_start_of the_string

>declaring str again before the function body
How old is this code? I haven't seen code that does that since before C89

This is really basic stuff if you have experience with C and maybe know a bit of assembly to understand what "register" tells the C compiler.

Also, who the hell would use such strlen now that only supports ASCII when there are unicode characters everywhere?

Americans.

OSX, *nix and Linux uses UTF-8 and in that case there is no concern about the way strlen does because it still does what it intends to do (return number of bytes in string).

What the hell is that function syntax?
size_t strlen(str) const char *str;
{
}

Pointer minus pointer returns a diffptr_t, not a size_t.

Fucking apple n00bs.

Read K&R you pleb.

It's old-style parameter declaration.

Said like a real Pajeet.

Before the modern declaration syntax was created, functions with parameters were declared as follows:

return_type function_name(argname1, argname2, argname3)
type1 argname1;
type2 argname2, argname3;


Essentially, parameters were declared the same way variables would be.

This. Writing 'clever' or obfuscated code has literally no advantage. In fact it hurts you because other devs can't read it easily, and I'm sure the compiler has a harder time optimizing that shit. If you wanna write clever code, save it for your pet projects that no one else will touch.

>other devs can't read it
And neither can you a week later.

>standard implementation of strlen lifted straight out of K&R is considered "a clever hack" by Sup Forums

God damn it, just go back to Java you fucking pajeets.

>A function called millions of times a second should trade speed for readability
Any programmer with half a brain cell can figure out what that function is doing
Stop being retarded.

>calling strlen a million times a second in the first place

One piece of software isn't doing it. ALL of your software added up does it.

>but other devs..
i don't want anybody incapable on conceiving the genius of my code to be able to read it

>Optimizing is bad! Let's make inefficient code for those hardware makers to fulfill that programmed obsolescence quotas!
You meant this, right?

>you can only optimize shit if you write clever code
Please, refer to And if your programming language of choice requires you to write clever code to optimize your program, you might as well use another language.

Sup Forums is dead.

isnt that the normal way of doing it anyways?

You can write the function a million other ways, it doesn't matter how good your compiler is at optimizing the code, it's still going to be slower than the "clever" code.
The function OP posted is good because it exploits Boolean logic, the only optimization in newer compilers would be the removal of the register keyword.

Slow ass pajeet code

tsunanet.net/~tsuna/strlen.c.html

Sup Forums was always dead. Go find a private community or somewhere where they actually narrow their topics and moderate heavily (until inevitably it crumbles due to inactivity or drama, then find the next community).

I'm here for banter and Chinese shit threads.

>it doesn't matter how good your compiler is at optimizing the code, it's still going to be slower than the "clever" code.
That doesn't make any sense and you know it. Worse yet you're endorsing bad programming attitudes.

If you're only gonna pull false information from your ass, then you better go learn the basics of programming again.

>mfw

>I program like THIS so he can program like THIS
(show images of that code and JS pajeet code)

>153 lines+comments for a strlen function

access violation if str is not null terminated

>inb4 this is by design in c
that doesn't make it good code

But it's faster and for a function as primitive as strlen, that really matters.

The only way to improve speed from here on is to drop C strings, like most other programming languages do.

GNUcucks will defend this

utf8 isnt a fixed bytes/char

user didn't claim it was either

it makes him wrong though. i think unicode chars can have null bytes in them. havent checked but it should be possible

Meanwhile, musl combines both approaches into a small yet tricky implementation.
git.musl-libc.org/cgit/musl/tree/src/string/strlen.c

>implying that shit is fast
>implying a less obfuscated version wouldn't compile to the same exact asm anyways

this

you could at least write it less cryptically, making it more obvious you're just address-walking until null

>i think unicode chars can have null bytes in them. havent checked but it should be possible
No, you are so very wrong.
Unicode doesn't even have a definite concept of a character. It's not a very useful concept.


>it makes him wrong though
No, the number of bytes in a string is the length of it by definition. in c at least.

Looks like some of you little guys havent been reading your Computer Systems a programmers perspective books.

byte length sounds pretty useless when talking about strings, which should be treated as arrays of (multibyte) chars

fine, i meant utf8

>useless
Try writing some code to take a string from a user, store it in memory, write a copy, with each character incremented by one, to another location in memory and write it out.
All without knowing the length of any of the strings in bytes.
I'll wait.

>which should be treated as arrays of (multibyte) chars
Like UTF-8, or something else?


how does that make you right?

yea if anything this piece of code's greatest sin is just looking old.

I suppose I can ask it here instead of sqt

The register keyword just "suggests" the compiler to store a variable in a cpu register (which the compiler may or may not comply) right? What if I absolutely 100% want my variable to be allocated in a cpu register?

Oh my god where are your parents?

The pointer isnt const retard. How would it advance if it was? Be less retarded please

>his libc doesn't have an implementation of strlen_s
lmao enjoy your buffer overflows fgt

this isn't that impressive

Pointer to const char. Const pointer would be
char * const str;


cdecl.org/

>return(result);
>not return result;
Shit taste/10.

Won't this break on machines where a long long int is 128 bits or more?

Are there any libs/compilers that do memoization of strlen?
If the compiler knows there's been no dereferences of that pointer, can't it use the same return value if it's called twice or more?

Now you know why GNU is shit.

>strlen_s
It's there, and it's called strnlen in the POSIX.1-2008 standard.

>30k

Where I live you are lucky if you get a 10k one

Not even sysadmins get 30

strnlen_s and strnlen are two different things.

Read the C11 standard. Inform yourself before you Shrek yourself.

>POSIX
lmao who gives a fuck about that shit?

>strnlen_s and strnlen
lmao they don't come much dumber than that

A collection of companies such as IBM, Fujitsu, HPE, friends

The fuck is this

>legacy shit
Oh ok. Carry on.

except it doesn't work properly with UTF-8 strings.

so K&R is only way to become patrician?

man.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man9/style.9?query=style

The fuck are you on about?

size_t mystrlen(const char *str){
size_t i;
for(i = 0; str[i] != '\0'; ++i);
return i;
}

How much speed would be lost with this?

>have to extract each element for each element in the array
>have to check for equality
>probably have to convert between types thanks to that '\0'
A lot

Lol wut

>>have to extract each element for each element in the array
It's the exact same as accessing a pointer. compiler > you. This part I'm sure of.

The standard strlen function also checks if *s is true. Isn't that the same as checking equality?

And '\0' is a constant which will probably get optimized somehow, or am wrong here?

I figured that shit out quite quickly.

If anybody can't figure that out they have no business looking at the code.

It will produce the same binary as OP, but you version is more readable.

Both the original and your code compile to essentially the same assembly with gcc on Linux x86_64.

K&R strlen:
strlen:
.begin:
/* %rdi is the first parameter to the function */
cmpb $0, (%rdi)
je .return_zero
movq %rdi, %rax
.loop:
addq $1, %rax
cmpb $0, (%rax)
jne .loop
subq %rdi, %rax
ret
.return_zero:
xorl %eax, %eax
ret

Your code:
strlen:
.begin:
xorl %eax, %eax
cmpb $0, (%rdi)
je .return_zero
.loop:
addq $1, %rax
cmpb $0, (%rdi,%rax)
jne .loop
ret
.return_zero:
ret

Your code just accesses memory with a constant offset of %rdi, while the OP's code does subtraction in the end.

>The standard strlen function also checks if *s is true. Isn't that the same as checking equality?
not really, it just executes the loop when *s is not zero.


>And '\0' is a constant which will probably get optimized somehow, or am wrong here?
In ascii, the '\0' is the null character. so, it's replaced with 0.

So, str[i] != 0; is just checking if str[i] is not 0, which is the same as str[i], so the compiler can replace that.


c is a _very_ simple language. The only way to really make code run faster is to make the computer do less stuff.

What do you use to get that ouput? gcc -S or objdump?

gcc -S. I just removed the function prolog/epilog macros for brevity, which just manipulate the stack frame and aren't actually needed in this case, and gave sensible names to the jump labels.

If you compile with -Os, it's mostly the same, but with "incq %rax" instead of "addq $1, %rax".

strlen:
movq %rdi, %rax
.loop:
cmpb $0, (%rax)
je .exit
incq %rax
jmp .loop
.exit:
subq %rdi, %rax
ret

strlen:
xorl %eax, %eax
.loop:
cmpb $0, (%rdi,%rax)
je .return
incq %rax
jmp .loop
.return:
ret

...

why #if 0?

>no CSS

Easier to manage than one huge /* … */, although you only should do that with actual code and not just a cheat sheet for gdb command line usage.

>other devs can't read it easily
the bigger problem is that not even I understand my own "clever" code after a couple weeks have passed.

about as easy to manage as one of these though.
/*/
...

//*/

That explains it though.

Why not use an identifier?

Isn't that function declaration old K&R C? Why are they using that?

How the fuck is that considered "clever"?

It's only clever to retarded pajeets who don't know anything beyond what stackoverflow says.