Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Optimized version of leftpad in plain C

Name: Anonymous 2016-03-23 21:32

#include <string.h>

static const char*
string_add(const char* first, const char* second)
{
const char* result = malloc(strlen(first) + strlen(second) + 1);
strcat(result, first);
strcat(result, second);
return result;
}

const char*
leftpad(const char* str, size_t len, char ch)
{
int i = -1;

if (ch == 0) ch = ' ';

int len = strlen(str);

while (++i < len) {
char buf[2] = { 0 };
buf[0] = ch;
str = string_add(buf, str);
}

return str;
}

Name: Anonymous 2016-04-01 23:27

>>36
Please avoid strlen or things like http://g.e-hentai.org/s/235d1491e1/430920-23 may happen to you.
#include <stdlib.h>
#include <string.h>

char *leftpad(char *s, size_t s_len, size_t n, int c)
{
char *to;

to = malloc(n + s_len + 1); /* no overflow checking? */
if (to) {
memset(to, c, n);
memcpy(to + n, s, s_len + 1);
}
return to;
}

Name: L. A. Calculus !jYCj6s4P.g 2016-04-02 1:22

IF DA SIZE OF DA STRING PLUS n IS GREATER THAN INT_MAX, UR NOT USING DA FUNCTION PROPERLY. PERIOD. DAT'S DA CALLING CONVENTION. IF U CALL DA FUNCTION PROPERLY, IT WILL WORK PROPERLY. DAT'S ALL DER IS TO IT. END OF FUCKING STORY.

Please avoid strlen
NO. GO FIND OUT WAT A STRING IS.

N WHAT IF n+s_len+1 OVERFLOWS IN UR EXAMPLE? U'VE WRITTEN PAST THE END OF DA STORAGE ALLOCATED BY malloc N UR PROGRAM'S BLOWN TO BITS.

SO UR CODE AIN'T PERFECT EITHER. U'VE FAILED AT BEING PEDANTIC. NOW GET DA FUCK OUTTA MY THRED, SAFETY MAN.

AND IF UR GONNA ARGUE DAT RELYING ON A SENSIBLE CALL IS BAD PRACTICE, CHEW ON DIS, BITCH:

leftpad("GO FUCK YASELF", 123421, 4932, 's'); /* FIX YOUR FUNCTION YA FUCKIN MORON. MY CALL DIDN'T WORK. */

CHECK FUCKIN MATE. NEXT VICTIM.

Name: Anonymous 2016-04-02 11:30

>>42
Cudder will laugh at you, not with you if you keep using strlen.

Name: Anonymous 2016-04-02 13:00

>>43
what you talking 'bout, faggot?
did you miss cudder's lesson on strcat/strlen?
let me refresh ur shit memory
http://bbs.progrider.org/prog/read/1406427616/133

Name: Anonymous 2016-04-02 14:39

>>44
She agrees with me. Avoid strlen.

Name: Anonymous 2016-04-02 15:24

>>45
>She

Name: Cudder !cXCudderUE 2016-04-02 16:31

>>42
+1

>>43,44,45
This is how those cargo cult "best practices" start. I wasn't saying "never use strlen", I was just saying use it when it makes sense and not when it doesn't. It didn't make sense to use strlen in that particular situation but that doesn't mean it doesn't make sense ever.

Now I feel like Dijkstra trying to explain how goto is not inherently bad...

Name: Anonymous 2016-04-02 18:29

>>47
Look at those bloated quotes!

Name: Anonymous 2016-04-02 18:48

>>47
It doesn't make sense that situation.
And optimise your quotes.

Name: Anonymous 2016-04-02 19:32

>>49
Yes, it does. Compare: >>43,44,45 vs. >>43-45

Also, Happy World Autism Day!

Name: Anonymous 2016-04-02 20:03

Cudder and LAC
Sitting under a tree
K I S S I N G

Name: Anonymous 2016-04-02 21:15

>>50
What?

Name: Anonymous 2016-04-03 7:36

World Altruism Day

Name: Anonymous 2016-04-03 12:17

>>51
I don't like this, I am the only one who may kiss LAC.

Name: Anonymous 2016-04-03 12:43

>>54
Check 'em

Name: Anonymous 2016-04-03 14:26

>>55
Working hard to train the next generation of trans allies!

Name: Anonymous 2016-04-10 4:38

None of these C solutions work with unicode right?
Once again the UNIX-loving toilet scrubbers somehow manage to feel superior while taking a whole thread to create code that's worse than what a freshman pythonista would write in 5 minutes.

Name: Anonymous 2016-04-12 0:27

//prog.go
package prog

func LeftPad(s string, l int, pad byte) string {
diff := l - len(s)
if diff <= 0 {
return s
}

b := make([]byte, l)
for i := 0; i < diff; i++ {
b[i] = pad
}
copy(b[diff:], s)
return string(b)
}

//prog_test.go
package prog

import "testing"

func BenchmarkLeftPadHMA(b *testing.B) {
for n := 0; n < b.N; n++ {
LeftPad("hax my anus", 20, ' ')
}
}


$ go test -bench=.
testing: warning: no tests to run
PASS
BenchmarkLeftPadHMA-8 10000000 124 ns/op
ok _/C_/Users/progrider/code/go/prog 1.419s


Unbeatable

Name: Anonymous 2016-04-12 1:49

>>58
Ew it's No.

Name: Anonymous 2016-04-12 4:49

>>59
Yeah, if it only has one memory allocation, it's not leftpad.

Name: Anonymous 2016-04-12 21:52

>>57
Unicode is cancer

Name: Anonymous 2016-04-13 22:42

>>61
Unicode is the fucking future!

Name: Anonymous 2016-04-14 0:04

>>62
Unicode unleashed the fucking fury!

Name: Anonymous 2016-04-14 3:14

>>61
If you don't want to spend your time supporting awful garbage because you want to interoperate with the rest of the world maybe you should consider a change of profession.
Or you could become a lisper and spend all your time writing blog posts about how smart you are without ever writing any code at all.

Name: Anonymous 2016-04-14 5:19

>>64
Or you could become a lisper and spend all your time writing blog posts about how smart you are without ever writing any code at all.

True dat.

Name: Cudder !cXCudderUE 2016-04-14 10:50

>>57
One word: UTF-8.

Name: Anonymous 2016-04-15 6:12

>>66
Exactly, none of these support UTF-8, either. Stop trying to sound smart, you fucking idiot.

Name: Cudder !cXCudderUE 2016-04-15 10:36

>>67
There's no need to "support" UTF-8, it Just Works™. Retard.

Name: Anonymous 2016-04-15 14:31

>>68
It actually doesn't work if you do retarded shit like:
some_retarded_str[42] = 'a'; // lol here we change a byte

Name: Anonymous 2016-04-15 14:41

>>68
Please, learn what UTF-8 is, woman.

Name: Anonymous 2016-04-15 15:05

>>70
>woman

Name: Anonymous 2016-04-15 15:25

>>71
greater than women
I agree.

Name: Anonymous 2016-04-15 20:17

>>72
Who are you quoting?

Name: Anonymous 2016-04-15 20:33

>>70
implying she uses Linux

Name: Anonymous 2016-04-16 3:22

>>74
>she

Name: Anonymous 2016-04-16 5:10

Assuming the original text was valid UTF-8, you can left pad with zero bytes and end up with valid UTF-8. Unless I'm an idiot.

Name: Anonymous 2016-04-16 7:28

optimize these dubs!

Name: Cudder !cXCudderUE 2016-04-16 14:50

>>69
Look at what the code is doing before posting anything, fucktard. That doesn't work in the general case but >>76 has it right.

Name: Anonymous 2016-04-16 17:12

Fine guys, but for real internationalization (not merely "UTF-8 support"):

- Make leftPad accept an arbitrary UTF-8 sequence to pad by
- Truncate the pad sequence only at UTF-8 codepoint boundaries (don't split multibyte characters)
- Truncate the pad sequence at a locale-appropriate character boundary as determined by http://www.unicode.org/reports/tr29/

Enough of your broken toy programs, just admit defeat and use an ICU library

Name: Anonymous 2016-04-16 21:11

There are some fundamentally difficult and/or complex problems to completely solve in computing, and all of your little "optimized" shortcut shit is nothing more than that: Shit!

- Date & time processing
- Text handling
- Caching
- Calculating the weight of Cudder's mom

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List