Skip to content

Commit 2721e2a

Browse files
committed
Small changes for kwBIN and STATMEDIAN.
1 parent 5f4cbda commit 2721e2a

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

samples/distro-examples/tests/strings.bas

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,11 @@ REM Only TRIM Strings
202202
if (trim(10) != 10) then throw "err"
203203
if (trim(10.1) != 10.1) then throw "err"
204204
if (trim([1,2,3]) != [1,2,3]) then throw "err"
205+
206+
REM test for integer-to-string conversion
207+
number_test=[[, "0", "0", "0"], [0, "0", "0", "0"], [7, "111", "7", "7"], [14, "1110", "16", "E"], [2090, "100000101010", "4052", "82A"]]
208+
for t in number_test
209+
if (t[1] != bin(t[0])) then throw "err: bin(" + str(t[0]) + ") <> " + t[1]
210+
if (t[2] != oct(t[0])) then throw "err: oct(" + str(t[0]) + ") <> " + t[2]
211+
if (t[3] != hex(t[0])) then throw "err: hex(" + str(t[0]) + ") <> " + t[3]
212+
next

src/common/blib_func.c

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static char *date_m3_table[] = TABLE_MONTH_3C;
3131
static char *date_mN_table[] = TABLE_MONTH_FULL;
3232

3333
#define BUF_LEN 64
34+
#define BIN_LEN 32 // Number of max bits (digits) kwBIN creates
3435

3536
/*
3637
*/
@@ -960,32 +961,29 @@ void cmd_str1(long funcCode, var_t *arg, var_t *r) {
960961
IF_ERR_RETURN;
961962

962963
if (l == 0) {
963-
r->v.p.ptr = (char *)malloc(2);
964-
strcpy(r->v.p.ptr, "0");
965-
r->v.p.length = strlen(r->v.p.ptr) + 1;
964+
v_createstr(r, "0");
966965
break;
967966
}
968967

969-
tb = malloc(33);
970-
memset(tb, 0, 33);
971-
972-
for (int i = 0; i < 32; i++) {
968+
tb = malloc(BIN_LEN + 1);
969+
memset(tb, 0, BIN_LEN + 1);
970+
971+
for (int i = 0; i < BIN_LEN; i++) {
973972
if (l & (1 << i)) {
974-
tb[31 - i] = '1';
973+
tb[BIN_LEN - 1 - i] = '1';
975974
} else {
976-
tb[31 - i] = '0';
975+
tb[BIN_LEN - 1 - i] = '0';
977976
}
978977
}
979-
978+
980979
// remove preceding zeros
981980
p = tb;
982981
while (*p == '0') {
983982
p++;
984983
}
985984

986-
r->v.p.ptr = (char *)malloc(strlen(p) + 1);
987-
strcpy(r->v.p.ptr, p);
988-
r->v.p.length = strlen(r->v.p.ptr) + 1;
985+
v_createstr(r, p);
986+
989987
break;
990988
case kwHEX:
991989
//

src/common/blib_math.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ var_num_t statmeandev(var_num_t *e, int count) {
235235
//
236236
// compare function for median calculation
237237
//
238-
int median_compare (const void * a, const void * b) {
238+
int median_compare(const void * a, const void * b) {
239239
var_num_t fa = *(var_num_t*) a;
240240
var_num_t fb = *(var_num_t*) b;
241241
return (fa > fb) - (fa < fb);
@@ -255,10 +255,12 @@ var_num_t statmedian(var_num_t *e, int count) {
255255

256256
qsort(e, count, sizeof(var_num_t), median_compare);
257257

258-
if (count % 2 == 0)
259-
return (e[count/2] + e[count/2 - 1]) / 2;
260-
else
261-
return e[count/2];
258+
if (count % 2 == 0) {
259+
return (e[count/2] + e[count/2 - 1]) / 2;
260+
}
261+
else {
262+
return e[count/2];
263+
}
262264
}
263265

264266
//

0 commit comments

Comments
 (0)