Optimized version of leftpad in plain C
1
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; }
2
Name:
Anonymous
2016-03-23 21:43
strlen(first) + strlen(second) can overflow. i = -1, ++i, undefined behavior. junk!
3
Name:
Anonymous
2016-03-23 21:47
suigintou!
4
Name:
Anonymous
2016-03-23 21:47
leftpadgate
5
Name:
Anonymous
2016-03-23 21:47
>>2,3 Suigintou is not junk
!
6
Name:
Anonymous
2016-03-23 23:16
Quick, import it to nodejs using some ffi module.
7
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.
8
Name:
Anonymous
2016-03-24 5:37
>>2 i = -1, ++i, undefined behavior. What the fuck are you on?
9
Name:
Anonymous
2016-03-24 6:04
leftpad(const char* str, size_t len , char ch) int len = strlen(str); WHY.jpg
10
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; }
11
Name:
>>10
2016-03-24 10:16
Oops, forgot to free the pointers in testPad().
12
Name:
Anonymous
2016-03-24 11:04
13
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.
14
Name:
Anonymous
2016-03-24 14:37
>>13 You wouldn't have bugs if you been writing test driven clean code.
15
Name:
Anonymous
2016-03-24 16:17
IMO this problem can be solved more elegantly with recursion.
16
Name:
Anonymous
2016-03-24 16:37
left pad docker images
17
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
18
Name:
Anonymous
2016-03-24 21:41
19
Name:
Anonymous
2016-03-24 23:16
>>16 ...hosting a J2EE
left_pad WEB SERVICE .
20
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 = ' ' .
21
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
22
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) ) ) )
23
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.
24
Name:
Anonymous
2016-03-26 13:47
>>23 Not as useless as formal verification, though.
25
Name:
Anonymous
2016-03-29 17:18
>>22 ) ) )
is you serious nigga?
26
Name:
Anonymous
2016-03-29 17:23
>>25 yo nigger, we don't say "nigga" around here, nigger.
27
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)
28
Name:
Anonymous
2016-03-29 19:47
>>25 no
>>27 epic my nomad friend
29
Name:
Anonymous
2016-03-29 20:36
>>27 u mena
rot3 f = \x y z -> f z x y
??
30
Name:
Anonymous
2016-03-29 20:37
WTF WHO IS EATING BACKSLASHES
31
Name:
Anonymous
2016-03-29 20:38
rot3 f = \\x y z -> f z x y
32
Name:
Anonymous
2016-03-30 18:57
morad
33
Name:
Anonymous
2016-03-30 18:57
CHECK EM DUBS
34
Name:
Anonymous
2016-03-30 18:58
35
Name:
Anonymous
2016-03-31 16:42
36
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; }
37
Name:
Anonymous
2016-04-01 4:23
38
Name:
Anonymous
2016-04-01 9:41
39
Name:
Cudder
!cXCudderUE
2016-04-01 10:41
40
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