feat(atoi_base): O(n) yay
This commit is contained in:
parent
cd40b66c0e
commit
8331a437c0
1
Makefile
1
Makefile
|
@ -11,6 +11,7 @@ SRC = \
|
||||||
ft_list_push_front.s \
|
ft_list_push_front.s \
|
||||||
ft_list_sort.s \
|
ft_list_sort.s \
|
||||||
ft_list_remove_if.s \
|
ft_list_remove_if.s \
|
||||||
|
ft_atoi_base.s \
|
||||||
|
|
||||||
OBJ = $(SRC:.s=.o)
|
OBJ = $(SRC:.s=.o)
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,123 @@
|
||||||
|
global ft_atoi_base
|
||||||
|
extern malloc ; for LUT
|
||||||
|
extern free ; for LUT
|
||||||
|
|
||||||
|
ft_atoi_base:
|
||||||
|
push rsp
|
||||||
|
push rbx
|
||||||
|
push r12 ; sign
|
||||||
|
push r13 ; base
|
||||||
|
push r14 ; return value
|
||||||
|
push r15 ; base LUT
|
||||||
|
push rdi
|
||||||
|
push rsi
|
||||||
|
mov rdi, 128
|
||||||
|
call malloc WRT ..plt
|
||||||
|
mov r15, rax
|
||||||
|
pop rsi
|
||||||
|
pop rdi
|
||||||
|
push rdi
|
||||||
|
push rsi
|
||||||
|
|
||||||
|
xor rcx, rcx
|
||||||
|
.init_lut_loop: ; sets LUT to 0
|
||||||
|
cmp rcx, 128
|
||||||
|
je .set_base
|
||||||
|
mov byte [r15 + rcx], 0
|
||||||
|
inc rcx
|
||||||
|
jmp .init_lut_loop
|
||||||
|
|
||||||
|
.set_base:
|
||||||
|
xor rcx, rcx ; rcx is base length/count
|
||||||
|
.set_base_loop:
|
||||||
|
cmp byte [rsi + rcx], 0
|
||||||
|
je .check_base_len
|
||||||
|
cmp byte [rsi + rcx], 43
|
||||||
|
je .error
|
||||||
|
cmp byte [rsi + rcx], 45
|
||||||
|
je .error
|
||||||
|
cmp byte [rsi + rcx], 126
|
||||||
|
jg .error
|
||||||
|
cmp byte [rsi + rcx], 33
|
||||||
|
jl .error
|
||||||
|
xor rbx, rbx
|
||||||
|
mov bl, byte [rsi + rcx]
|
||||||
|
cmp byte [r15 + rbx], 0
|
||||||
|
jne .error
|
||||||
|
mov rdx, rcx
|
||||||
|
inc rdx
|
||||||
|
mov byte [r15 + rbx], dl
|
||||||
|
inc rcx
|
||||||
|
jmp .set_base_loop
|
||||||
|
|
||||||
|
.check_base_len:
|
||||||
|
cmp rcx, 2
|
||||||
|
jl .error
|
||||||
|
mov r13, rcx
|
||||||
|
|
||||||
|
xor rcx, rcx ; index = 0
|
||||||
|
xor r14, r14 ; number = 0
|
||||||
|
mov r12, 1 ; sign = 0
|
||||||
|
.skip_white_spaces:
|
||||||
|
cmp byte [rdi + rcx], 33
|
||||||
|
jle .skip_once
|
||||||
|
cmp byte [rdi + rcx], 126
|
||||||
|
jg .skip_once
|
||||||
|
jmp .get_sign_loop
|
||||||
|
|
||||||
|
.skip_once:
|
||||||
|
inc rcx
|
||||||
|
jmp .skip_white_spaces
|
||||||
|
|
||||||
|
.get_sign_loop:
|
||||||
|
cmp byte [rdi + rcx], 43
|
||||||
|
je .pos_once
|
||||||
|
cmp byte [rdi + rcx], 45
|
||||||
|
je .neg_once
|
||||||
|
jmp .get_number_loop
|
||||||
|
|
||||||
|
.pos_once:
|
||||||
|
inc rcx
|
||||||
|
jmp .get_sign_loop
|
||||||
|
|
||||||
|
.neg_once:
|
||||||
|
neg r12
|
||||||
|
inc rcx
|
||||||
|
jmp .get_sign_loop
|
||||||
|
|
||||||
|
.get_number_loop:
|
||||||
|
xor rbx, rbx
|
||||||
|
mov bl, byte [rdi + rcx]
|
||||||
|
cmp bl, 0
|
||||||
|
je .done
|
||||||
|
mov bl, byte [r15 + rbx] ; set bl to base index
|
||||||
|
cmp bl, 0
|
||||||
|
je .done
|
||||||
|
mov rax, r14
|
||||||
|
mul r13
|
||||||
|
mov r14, rax
|
||||||
|
dec rbx
|
||||||
|
add r14, rbx
|
||||||
|
inc rcx
|
||||||
|
jmp .get_number_loop
|
||||||
|
|
||||||
|
.error:
|
||||||
|
mov r14, 0
|
||||||
|
|
||||||
|
.done:
|
||||||
|
mov rdi, r15
|
||||||
|
call free WRT ..plt
|
||||||
|
mov rax, r14
|
||||||
|
cmp r12, 1
|
||||||
|
je .exit
|
||||||
|
neg rax
|
||||||
|
.exit:
|
||||||
|
pop rsi
|
||||||
|
pop rdi
|
||||||
|
pop r15
|
||||||
|
pop r14
|
||||||
|
pop r13
|
||||||
|
pop r12
|
||||||
|
pop rbx
|
||||||
|
pop rsp
|
||||||
|
ret
|
1
libasm.h
1
libasm.h
|
@ -21,6 +21,7 @@ void ft_list_push_front(t_list **, void *);
|
||||||
int ft_list_size(t_list *);
|
int ft_list_size(t_list *);
|
||||||
void ft_list_sort(t_list **, int (*)());
|
void ft_list_sort(t_list **, int (*)());
|
||||||
void ft_list_remove_if(t_list **, void *, int (*)(), void (*)(void *));
|
void ft_list_remove_if(t_list **, void *, int (*)(), void (*)(void *));
|
||||||
|
int ft_atoi_base(char *, char *);
|
||||||
|
|
||||||
void ft_list_print(t_list *begin_list);
|
void ft_list_print(t_list *begin_list);
|
||||||
|
|
||||||
|
|
97
main.c
97
main.c
|
@ -17,98 +17,11 @@ int ft_strcmp(const char *s1, const char *s2) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
/*
|
t_list *l = NULL;
|
||||||
t_list *list;
|
ft_list_push_front(&l, "Salut\n");
|
||||||
|
ft_list_print(l);
|
||||||
list = NULL;
|
int test = ft_atoi_base("700chmod", "01234567");
|
||||||
|
printf("%d\n", test);
|
||||||
ft_list_push_front(&list, "toto\n");
|
|
||||||
ft_list_push_front(&list, "tutu\n");
|
|
||||||
ft_list_push_front(&list, "zaza\n");
|
|
||||||
ft_list_push_front(&list, "bobo\n");
|
|
||||||
ft_list_push_front(&list, "tata\n");
|
|
||||||
ft_list_push_front(&list, "babar\n");
|
|
||||||
ft_list_push_front(&list, "zoro\n");
|
|
||||||
|
|
||||||
ft_list_print(list);
|
|
||||||
|
|
||||||
printf("%lu\n", sizeof(t_list));
|
|
||||||
printf("%d\n", ft_list_size(list));
|
|
||||||
|
|
||||||
ft_list_sort(&list, &ft_strcmp);
|
|
||||||
|
|
||||||
ft_list_print(list);
|
|
||||||
|
|
||||||
t_list *a;
|
|
||||||
|
|
||||||
a = NULL;
|
|
||||||
ft_list_push_front(&a, "d\n");
|
|
||||||
ft_list_push_front(&a, "c\n");
|
|
||||||
ft_list_push_front(&a, "b\n");
|
|
||||||
ft_list_push_front(&a, "a\n");
|
|
||||||
|
|
||||||
ft_list_print(a);
|
|
||||||
|
|
||||||
ft_list_sort(&a, &ft_strcmp);
|
|
||||||
|
|
||||||
printf("On est sorti.e.s de cet enfer\n");
|
|
||||||
ft_list_print(a);
|
|
||||||
|
|
||||||
t_list *b;
|
|
||||||
|
|
||||||
b = NULL;
|
|
||||||
ft_list_push_front(&b, "ziziron\n");
|
|
||||||
ft_list_push_front(&b, "abalon\n");
|
|
||||||
ft_list_push_front(&b, "zebulon\n");
|
|
||||||
ft_list_print(b);
|
|
||||||
ft_list_sort(&b, &ft_strcmp);
|
|
||||||
ft_list_print(b);
|
|
||||||
|
|
||||||
t_list *null = NULL;
|
|
||||||
|
|
||||||
ft_list_sort(&null, &ft_strcmp);
|
|
||||||
|
|
||||||
t_list *begin_ok = NULL;
|
|
||||||
|
|
||||||
ft_list_push_front(&begin_ok, "c");
|
|
||||||
ft_list_push_front(&begin_ok, "f");
|
|
||||||
ft_list_push_front(&begin_ok, "b");
|
|
||||||
ft_list_push_front(&begin_ok, "h");
|
|
||||||
ft_list_push_front(&begin_ok, "e");
|
|
||||||
ft_list_push_front(&begin_ok, "l");
|
|
||||||
ft_list_push_front(&begin_ok, "d");
|
|
||||||
ft_list_push_front(&begin_ok, "i");
|
|
||||||
ft_list_push_front(&begin_ok, "j");
|
|
||||||
ft_list_push_front(&begin_ok, "k");
|
|
||||||
ft_list_push_front(&begin_ok, "g");
|
|
||||||
ft_list_push_front(&begin_ok, "a");
|
|
||||||
|
|
||||||
ft_list_print(begin_ok);
|
|
||||||
printf("\n");
|
|
||||||
ft_list_sort(&begin_ok, &ft_strcmp);
|
|
||||||
|
|
||||||
printf("\n");
|
|
||||||
printf("\n");
|
|
||||||
ft_list_print(begin_ok);
|
|
||||||
printf("\n");
|
|
||||||
*/
|
|
||||||
t_list *c = NULL;
|
|
||||||
ft_list_push_front(&c, "b\n");
|
|
||||||
ft_list_push_front(&c, "c\n");
|
|
||||||
ft_list_push_front(&c, "a\n");
|
|
||||||
ft_list_push_front(&c, "c\n");
|
|
||||||
ft_list_push_front(&c, "a\n");
|
|
||||||
ft_list_push_front(&c, "b\n");
|
|
||||||
ft_list_push_front(&c, "c\n");
|
|
||||||
ft_list_push_front(&c, "c\n");
|
|
||||||
ft_list_push_front(&c, "b\n");
|
|
||||||
ft_list_push_front(&c, "a\n");
|
|
||||||
ft_list_push_front(&c, "b\n");
|
|
||||||
ft_list_print(c);
|
|
||||||
// ft_list_sort(&c, *ft_strcmp);
|
|
||||||
ft_list_print(c);
|
|
||||||
ft_list_remove_if(&c, "b\n", &ft_strcmp, &do_nothing);
|
|
||||||
ft_list_print(c);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue