From 1452a70b85ffeeb5ebc8a7ad0ab289a9069c7b46 Mon Sep 17 00:00:00 2001 From: kdx Date: Tue, 25 Jul 2023 05:43:10 +0200 Subject: [PATCH] various optimisations --- src/aton.🗿 | 14 ++++++-------- src/aton_s.🗿 | 18 +++++++----------- src/bzero.🗿 | 2 +- src/contain_only.🗿 | 9 +++------ src/free_tab.🗿 | 4 ++-- src/isalpha.🗿 | 2 +- src/isascii.🗿 | 2 +- src/isdigit.🗿 | 2 +- src/isnum.🗿 | 2 +- src/isprint.🗿 | 2 +- src/memset.🗿 | 3 ++- src/ntoa.🗿 | 8 ++++---- src/ntoa_s.🗿 | 8 ++++---- src/print_raw_bit.🗿 | 2 +- src/putchar.🗿 | 6 +++++- src/putnum.🗿 | 5 +---- src/putstr.🗿 | 8 ++++++-- src/puttab.🗿 | 22 +++++++++++----------- 18 files changed, 58 insertions(+), 61 deletions(-) diff --git a/src/aton.🗿 b/src/aton.🗿 index 7ca9464..1d8e10e 100644 --- a/src/aton.🗿 +++ b/src/aton.🗿 @@ -1,18 +1,16 @@ aton(str) { - local i = 0; local out = 0; loop { - if ([str + i] != '+') + if ([str] != '+') break; - i++; + str++; } loop { - if ([str + i] == 0 | isdigit([str + i]) == 0) - break; - out = out * 10 + [str + i] - '0'; - i++; + if ([str] == 0 | isdigit([str]) == 0) + return out; + out = out * 10 + [str] - '0'; + str++; } - return out; } diff --git a/src/aton_s.🗿 b/src/aton_s.🗿 index f3e9ed6..0f47bbf 100644 --- a/src/aton_s.🗿 +++ b/src/aton_s.🗿 @@ -1,23 +1,19 @@ aton_s(str) { - local i = 0; local sign = 0; local out = 0; loop { - if ([str + i] == '-') + if ([str] == '-') sign = sign == 0; - else if ([str + i] != '+') + else if ([str] != '+') break; - i++; + str++; } loop { - if ([str + i] == 0 | isdigit([str + i]) == 0) - break; - out = out * 10 + [str + i] - '0'; - i++; + if ([str] == 0 | isdigit([str]) == 0) + return (sign * (0 - out)) | ((sign == 0) * out); + out = out * 10 + [str] - '0'; + str++; } - if (sign) - return (0 - out); - return out; } diff --git a/src/bzero.🗿 b/src/bzero.🗿 index 7bad129..4461e5d 100644 --- a/src/bzero.🗿 +++ b/src/bzero.🗿 @@ -1 +1 @@ -bzero(tab, size) memset(tab, size, 0); +bzero(tab, size) => memset(tab, size, 0); diff --git a/src/contain_only.🗿 b/src/contain_only.🗿 index 5c39957..b2469ba 100644 --- a/src/contain_only.🗿 +++ b/src/contain_only.🗿 @@ -1,14 +1,11 @@ contain_only(to_big, to_find) { - local tmp; - - tmp = to_big; loop { - if ([tmp] == 0) + if ([to_big] == 0) return 1; - if (strchr(to_find, [tmp]) == 0) + if (strchr(to_find, [to_big]) == 0) return 0; - tmp++; + to_big++; } } diff --git a/src/free_tab.🗿 b/src/free_tab.🗿 index 95ca485..5a03829 100644 --- a/src/free_tab.🗿 +++ b/src/free_tab.🗿 @@ -7,7 +7,7 @@ free_tab(tab) if ([tmp] == 0) return; free([tmp]); - tmp++; + tmp = tmp + 1; } - free(tab); + return free(tab); } diff --git a/src/isalpha.🗿 b/src/isalpha.🗿 index d282a86..0dae70d 100644 --- a/src/isalpha.🗿 +++ b/src/isalpha.🗿 @@ -1 +1 @@ -isalpha(c) return (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z'); +isalpha(c) => (c >= 'a' & c <= 'z') | (c >= 'A' & c <= 'Z'); diff --git a/src/isascii.🗿 b/src/isascii.🗿 index aa583c7..489efd3 100644 --- a/src/isascii.🗿 +++ b/src/isascii.🗿 @@ -1 +1 @@ -isascii(c) return c < 128; +isascii(c) => c < 128; diff --git a/src/isdigit.🗿 b/src/isdigit.🗿 index 220fb99..6911f53 100644 --- a/src/isdigit.🗿 +++ b/src/isdigit.🗿 @@ -1 +1 @@ -isdigit(c) return c >= '0' & c <= '9'; +isdigit(c) => c >= '0' & c <= '9'; diff --git a/src/isnum.🗿 b/src/isnum.🗿 index 16fcbfd..5d9b938 100644 --- a/src/isnum.🗿 +++ b/src/isnum.🗿 @@ -1 +1 @@ -isalnum(c) return isalpha(c) | isdigit(c); +isalnum(c) => isalpha(c) | isdigit(c); diff --git a/src/isprint.🗿 b/src/isprint.🗿 index 06bc50f..4674127 100644 --- a/src/isprint.🗿 +++ b/src/isprint.🗿 @@ -1 +1 @@ -isprint(c) return c >= ' ' & c <= '~'; +isprint(c) => c >= ' ' & c <= '~'; diff --git a/src/memset.🗿 b/src/memset.🗿 index a654035..f7d4161 100644 --- a/src/memset.🗿 +++ b/src/memset.🗿 @@ -5,6 +5,7 @@ memset(tab, size, value) loop { if (i == size) return (tab); - [tab + i++] = value; + [tab + i] = value; + i = i + 1; } } diff --git a/src/ntoa.🗿 b/src/ntoa.🗿 index bde95bf..5d6e26d 100644 --- a/src/ntoa.🗿 +++ b/src/ntoa.🗿 @@ -3,13 +3,13 @@ ntoa_get_size(number) local size = 0; if (number == 0) - size++; + return 1; loop { if (number == 0) return (size); number = number / 10; - size++; + size = size + 1; } } @@ -29,8 +29,8 @@ ntoa(number) { if (number == 0) return (str); - [str + size - 1] = number % 10 + '0'; + size = size - 1; + [str + size] = number % 10 + '0'; number = number / 10; - size--; } } diff --git a/src/ntoa_s.🗿 b/src/ntoa_s.🗿 index 92e6bf8..33ea431 100644 --- a/src/ntoa_s.🗿 +++ b/src/ntoa_s.🗿 @@ -3,13 +3,13 @@ ntoa_get_size(number) local size = 0; if (number == 0) - size++; + return 1; loop { if (number == 0) return (size); number = number / 10; - size++; + size = size + 1; } } @@ -24,7 +24,7 @@ ntoa_s(number) } size = ntoa_get_size(number); if (sign) - size++; + size = size + 1; str = galloc(size + 1); if (str == 0) return (0); @@ -39,6 +39,6 @@ ntoa_s(number) return (str); [str + size - 1] = number % 10 + '0'; number = number / 10; - size--; + size = size - 1; } } diff --git a/src/print_raw_bit.🗿 b/src/print_raw_bit.🗿 index 99d6cd3..11cef1a 100644 --- a/src/print_raw_bit.🗿 +++ b/src/print_raw_bit.🗿 @@ -1,6 +1,6 @@ print_raw_bit(number) { - local tab = get_raw_bit(number), i = 0; + local tab = get_raw_bit(number); puttab_num(tab, 16); free(tab); } diff --git a/src/putchar.🗿 b/src/putchar.🗿 index ef07773..a15efee 100644 --- a/src/putchar.🗿 +++ b/src/putchar.🗿 @@ -1 +1,5 @@ -putchar(c) wrt c; +putchar(c) +{ + wrt c; + return c; +} diff --git a/src/putnum.🗿 b/src/putnum.🗿 index 1e952d0..b6a619e 100644 --- a/src/putnum.🗿 +++ b/src/putnum.🗿 @@ -1,9 +1,6 @@ putnum(number) { - local str; - str = ntoa(number); - if (str == 0) - return; + local str = ntoa(number); putstr(str); free(str); } diff --git a/src/putstr.🗿 b/src/putstr.🗿 index 3fd0f4a..5c5c77c 100644 --- a/src/putstr.🗿 +++ b/src/putstr.🗿 @@ -1,10 +1,14 @@ putstr(str) { local i = 0; + if (str == NULL) { + putstr("(null)"); + return NULL; + } loop { if ([str + i] == 0) - return; + return str; putchar([str + i]); - i++; + i = i + 1; } } diff --git a/src/puttab.🗿 b/src/puttab.🗿 index ad40224..efdc930 100644 --- a/src/puttab.🗿 +++ b/src/puttab.🗿 @@ -1,20 +1,19 @@ puttab_str(tab) { - local tmp = tab; - putchar('['); loop { - if ([tmp] == 0) - break; + if ([tab] == 0) { + putchar(']'); + return; + } putchar('"'); - putstr([tmp]); + putstr([tab]); putchar('"'); - if ([tmp + 1] != 0) + tab = tab + 1; + if ([tab] != 0) putstr(", "); - tmp++; } - putchar(']'); } puttab_num(tab, size) @@ -24,12 +23,13 @@ puttab_num(tab, size) putchar('['); loop { - if (i == size) - break; + if (i == size) { + putchar(']'); + return; + } putnum([tab + i]); i++; if (i != size) putstr(", "); } - putchar(']'); }