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-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

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