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

Pages: 1-4041-8081-

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-03-23 21:43

strlen(first) + strlen(second) can overflow. i = -1, ++i, undefined behavior.
junk!

Name: Anonymous 2016-03-23 21:47

suigintou!

Name: Anonymous 2016-03-23 21:47

leftpadgate

Name: Anonymous 2016-03-23 21:47

>>2,3
Suigintou is not junk!

Name: Anonymous 2016-03-23 23:16

Quick, import it to nodejs using some ffi module.

Name: Anonymous 2016-03-24 1:13

This doesn't look very web-scale. Please add a callback interface so you don't block the main thread.

Name: Anonymous 2016-03-24 5:37

>>2
i = -1, ++i, undefined behavior.
What the fuck are you on?

Name: Anonymous 2016-03-24 6:04

leftpad(const char* str, size_t len, char ch)

int len = strlen(str);

WHY.jpg

Name: Anonymous 2016-03-24 10:10

OPTIMIZED

enum Dirn {
Left = 0,
Right = 1,
};

template<typename T, Dirn W> // W = Left or Right
T* pad(T* src, int n, T ch = (T)' ') {
const size_t c = strlen(src);
const size_t p = W * (c*sizeof(T));
const size_t sz = (c+n) * sizeof(T);
T* ret = new T[sz];
for(size_t i = p; i < p+n; i++) { // fill `ch`
ret[i] = ch;
}
for(size_t i = (W^Dirn::Right)*n, j = 0;
j < (c*sizeof(T));
i++, j++) {
ret[i] = src[j];
}
ret[sz] = 0;
return ret;
}


~~~~~~~~~~~~~~

Full:

// $ clang++ -std=c++11 -O3 leftpad.cc -o leftpad
// $ ./leftpad

#include <string.h>
#include <stdio.h>

namespace leftpad {
enum Dirn {
Left = 0,
Right = 1,
};

// generic pad
// either left or right padding
// `src` and `ret` are NULL-terminated
// callee must delete[] `ret`
template<typename T, Dirn W> // W = Left or Right
T* pad(T* src, int n, T ch = (T)' ') {
const size_t c = strlen(src);
const size_t p = W * (c*sizeof(T));
const size_t sz = (c+n) * sizeof(T);
T* ret = new T[sz];
for(size_t i = p; i < p+n; i++) { // fill `ch`
ret[i] = ch;
}
for(size_t i = (W^Dirn::Right)*n, j = 0;
j < (c*sizeof(T));
i++, j++) {
ret[i] = src[j];
}
ret[sz] = 0;
return ret;
}

void testPad() {
const char* t = "Donald Trump 2016";

char* g1 = pad<char, Dirn::Left>((char*)t, 3, ' ');
const char* e1 = (char*)" Donald Trump 2016";
if(strcmp(e1, g1)) {
printf("Fail!\nExpected \"%s\"; Got \"%s\"\n\n",
e1, g1);
} else {
printf("Pass!\n");
}

const char* e2 = (char*)"Donald Trump 2016 ";
char* g2 = pad<char, Dirn::Right>((char*)t, 3, ' ');
if(strcmp(e2, g2)) {
printf("Fail!\nExpected \"%s\"; Got \"%s\"\n\n",
e2, g2);
} else {
printf("Pass!\n");
}
}
}

int main(int, char**) {
leftpad::testPad();
return 0;
}

Name: >>10 2016-03-24 10:16

Oops, forgot to free the pointers in testPad().

Name: Anonymous 2016-03-24 11:04

>>11
sweet dubs

Name: Anonymous 2016-03-24 14:26

>>9
That was just version 1.0. You really can't blame the first version for having bugs. I'm planning to eventually release version 2.0 that actually builds.

Name: Anonymous 2016-03-24 14:37

>>13
You wouldn't have bugs if you been writing test driven clean code.

Name: Anonymous 2016-03-24 16:17

IMO this problem can be solved more elegantly with recursion.

Name: Anonymous 2016-03-24 16:37

left pad docker images

Name: Anonymous 2016-03-24 19:06

def left_pad(s, c, n):
return (n - len(s)) * c + s


proc len = (string s) int: upb s - lwb s + 1,
left pad = (string s, char c, int n) string: (n - len(s)) × c + s

Name: Anonymous 2016-03-24 21:41

Name: Anonymous 2016-03-24 23:16

>>16
...hosting a J2EE left_pad WEB SERVICE.

Name: Anonymous 2016-03-25 4:00

contains_only(_, []).
contains_only(X, [X|T]) :- contains_only(X, T).

shorter_or_equal(List, Length) :- length(List, ListLength), ListLength =< Length.

left_padded_list(List, PadToLength, Padding, PaddedList) :-
length(PaddedList, PadToLength), contains_only(Padding, Pad), (shorter_or_equal(Pad, PadToLength) -> append(Pad, List, PaddedList); !, false).

left_padded_string(String, PadToLength, Padding, PaddedString) :-
string_chars(String, List), left_padded_list(List, PadToLength, Padding, PaddedList), string_chars(PaddedString, PaddedList).


Used like this:

?- left_padded_string("hello", 10, ' ', X).
X = " hello" .
?- left_padded_string("hello", 10, X, " hello").
X = ' ' .

Name: Cudder !cXCudderUE 2016-03-25 16:06

OP is clearly a noob.

>>14
You wouldn't have bugs if you actually used your brain and thought before writing.

I'm not going to write the C version because it's so bloody simple in Asm. There's more lines of comments than code here. Allocate your own buffers and do the length-checking yourself.
; al pad char
; ecx pad length
; edi dest string
; esi source string
; edx source length
leftpad:
rep stosb
mov ecx, edx
rep movsb
ret

Name: Anonymous 2016-03-25 16:43

(define (left-pad str chr num)
(if (< num 1)
str
(left-pad
(string-append chr str)
chr
(- num 1)
)
)
)

Name: Anonymous 2016-03-25 22:20

>>14
Test suites only reveal bugs that you can think of testing. They're useless for revealing edge cases.

Name: Anonymous 2016-03-26 13:47

>>23
Not as useless as formal verification, though.

Name: Anonymous 2016-03-29 17:18

>>22
)
)
)

is you serious nigga?

Name: Anonymous 2016-03-29 17:23

>>25
yo nigger, we don't say "nigga" around here, nigger.

Name: Anonymous 2016-03-29 19:21

>>22
(.:) = (.) . (.)
rot3 f = \x y z -> f z x y

leftpad :: [a] -> a -> Int -> [a]
leftpad = (flip . rot3) ((++) .: replicate)

Name: Anonymous 2016-03-29 19:47

>>25
no

>>27
epic my nomad friend

Name: Anonymous 2016-03-29 20:36

>>27
u mena rot3 f = \x y z -> f z x y??

Name: Anonymous 2016-03-29 20:37

WTF WHO IS EATING BACKSLASHES

Name: Anonymous 2016-03-29 20:38

rot3 f = \\x y z -> f z x y

Name: Anonymous 2016-03-30 18:57

morad

Name: Anonymous 2016-03-30 18:57

CHECK EM DUBS

Name: Anonymous 2016-03-30 18:58

>>33
sweet dubs bro

Name: Anonymous 2016-03-31 16:42

>>34
Yeah, pretty cool.

Name: L. A. Calculus !jYCj6s4P.g 2016-04-01 1:51

#include <stdlib.h>
#include <string.h>

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

to = malloc(n + strlen(s) + 1);
if (to != NULL) {
memset(to, c, n);
strcpy(to + n, s);
}
return to;
}

Name: Anonymous 2016-04-01 4:23

Name: Anonymous 2016-04-01 9:41

>>36
size_t

Name: Cudder !cXCudderUE 2016-04-01 10:41

!= NULL
:facepalm:

Name: L. A. Calculus !jYCj6s4P.g 2016-04-01 23:16

>>38
OUTTA MY THRED SAFETY MAN

>>39
OUTTA MY THRED LORD OF DA STAK BOI BIT PUSHERS

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

Name: Anonymous 2016-04-17 15:01

>>79
Nobody gives a shit about utf8. your programs should know how to work with any kind of encoding.

Name: Anonymous 2016-04-17 18:13

>>79
and use an ICU library
An inherit its overhead and vulnerabilities for something as simple as this? No thanks.

>>80
Take a step away from enterprise and a step towards purity.

>>81
UTF-8 is the only worthy encoding. I suppose you think all programs should be equipped with image recognition so they can read ancient egyptian inscriptions on walls as well.

Name: Anonymous 2016-04-17 18:47

>>81
your programs should know how to work with any kind of encoding.

God, no. There's no advantage to perpetuating a bunch of shitty ancient code pages and other defective text encodings.

Most programs should accept only UTF-8 and emit only UTF-8. In the odd case where you need to accept non-UTF8 input, force the client to specify the encoding - don't autodetect.

Name: Anonymous 2016-04-17 19:07

Shift_JIS is the only standard we really need.

Name: Anonymous 2016-04-17 19:49

>>83
Who said anything about autodetect? For the case >>79 was talking about you do not need to even know the encoding.

Name: Anonymous 2016-04-18 8:00

>>83
Fuck you. I do a lot of imaging old software & docs, and systems should have knowledge of all the shitty old encodings for when you need them. It's not like they take any considerable space or slow anything down.

All you backwards fucknuts need to have the machine fucking working for you, instead of trying to let the machine off the hook for complex cases. Are you dickheads are afraid of computing? This is what computers are for!

Name: Anonymous 2016-04-19 0:21

>>86
Legacy encodings considered harmful

Name: Anonymous 2016-04-19 2:36

>>87
Damn straight, I'll shove my glock in your face for that shit.

Name: Anonymous 2016-04-19 3:21

>>87
Harmful to whom or to what? Nothing!

Name: Anonymous 2016-04-23 23:32

>>89
To everyone who is bothered by them, so everyone.

Name: Anonymous 2016-04-24 5:08

>>90
The rest of the world is bothered by you, so now what?

Kill yourself!

Name: Anonymous 2016-04-26 2:36

>>83
Elitist pricks. The whole world doesn't revolve around your UTF-8 narcissism, if anything forcing it leaves for less compatibility which is precisely what I'm paying you for, programmer boy

Name: Anonymous 2016-04-26 23:29

>>92
Compatibility with inferior solutions is not an advantage. The herd must be culled.

Name: Anonymous 2016-04-27 2:07

(slime-restart-inferior-lisp)

Name: Anonymous 2016-04-30 23:49

>>92
Do we really have a rockstar manager on our /prog/?

Name: Anonymous 2016-05-01 6:08

>>93
Are you going to go through all the files of humanity and convert the non-UTF-8 ones? Are you going to force people to use external converter applications to bring them in? Just support the fucking encodings. They're tiny to include.

Name: Anonymous 2016-05-01 14:08

Are you going to force people to use external converter applications to bring them in?
Yes, because they shouldn't have used those encodings to begin with.

Name: Anonymous 2016-05-02 7:04

>>97
And in 30 years, whatever has readily supplanted UTF-8 will say you shouldn't have used UTF-8 to begin with. Go fuck yourself.

Name: Anonymous 2016-05-03 0:04

>>98
Won't happen. UTF-8 is good enough for everyone, even if every emoji codepoint comes in 10 flavors.

Name: Anonymous 2016-05-03 1:02

>>99
Nice dubs bro!

Name: Anonymous 2016-05-03 5:57

>>99
The problem is not Unicode's ability to hold this many codepoints. It's a great advantage indeed.

The problem is complexity of the standard itself. There's more to it than mapping values to characters. And even that got its flaws in the implementation.

For example combining characters, "ö" vs. "ö", U+006F U+0308 vs. U+00F6. On top of that, all the UAX'es: Unicode Bidirectional Algorithm, East Asian Width, Unicode Line Breaking Algorithm, Unicode Normalization Forms and more. And these are all part of the standard, I'm not including "Unicode Technical Standards" with which software does not have to be conformant, to implement Unicode. Good luck parsing and implementing all quirks of Unicode.

Given that, emojis are just a minor problem.

Name: Anonymous 2016-05-04 1:27

>>101
Granted, the Unicode spec is complex. But you can use UTF-8 as an encoding without having to worry about most of the additional technical standards. I mean, you should probably at least normalize input when saving it to a persistent store, but hell - you can just ignore it and fail hard on all kinds of foreign language edge cases like all American software. UTF-8 don't care.

Name: Anonymous 2016-05-04 15:31

>>102
Until you try to get the length of a string, or try to equate strings.

Name: Anonymous 2016-05-04 23:04

>>103
The byte length should be sufficient

Name: Anonymous 2016-05-04 23:45

>>104
...says the person who never had to truly work with Unicode strings.

Name: Anonymous 2016-05-07 8:04

>>105
No. Byte length is sufficient.

Name: Anonymous 2016-05-07 15:22

>>106
Your comprehension is insufficient.

Name: Anonymous 2016-05-07 18:49

>>107
No. The problem specification is insufficient.

Name: Anonymous 2016-05-07 21:55

>>108
No. The flow of cum into my anus is insufficient.

Name: Anonymous 2016-05-09 7:48

>>109
HAX MY ANUS

Name: Anonymous 2016-05-09 9:59

Please read: an urgent appeal to check these trips

Name: Anonymous 2016-05-09 19:28

111
That's pretty neat.

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