Skip to content
This repository was archived by the owner on Feb 18, 2020. It is now read-only.

Commit 5d1ebf7

Browse files
CArray_Print with auto offset.
1 parent d9da742 commit 5d1ebf7

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

kernel/carray.c

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,8 +1126,10 @@ CArray_CheckFromAny(CArray *op, CArrayDescriptor *descr, int min_depth,
11261126
*/
11271127
static void
11281128
_print_recursive(CArray * array, CArrayIterator * iterator, int * index, int current_dim,
1129-
int summarized, int x_summarized, int y_summarized, int notated, int has_digits)
1129+
int summarized, int x_summarized, int y_summarized, int notated, int has_digits,
1130+
int max_digits)
11301131
{
1132+
int offset;
11311133
int i, index_jumps, j;
11321134
index_jumps = ((int *)CArray_STRIDES(array))[current_dim] / CArray_ITEMSIZE(array);
11331135

@@ -1146,11 +1148,12 @@ _print_recursive(CArray * array, CArrayIterator * iterator, int * index, int cur
11461148
}
11471149
php_printf("[");
11481150
_print_recursive(array, iterator, index, current_dim + 1, summarized,
1149-
x_summarized, y_summarized, notated, has_digits);
1151+
x_summarized, y_summarized, notated, has_digits, max_digits);
11501152
}
11511153
}
11521154
if(current_dim >= array->ndim-1) {
11531155
*index = *index + 1;
1156+
char tmp_str_num[11];
11541157
if(array->descriptor->type == TYPE_INTEGER) {
11551158
int * value;
11561159
if (notated) {
@@ -1162,12 +1165,18 @@ _print_recursive(CArray * array, CArrayIterator * iterator, int * index, int cur
11621165
} else {
11631166
for (i = 0; i < CArray_DIMS(array)[current_dim]; i++) {
11641167
value = (int *) CArrayIterator_DATA(iterator);
1168+
snprintf(tmp_str_num, 11, "%d", *value);
1169+
offset = max_digits - strlen(tmp_str_num);
1170+
for (j = 0; j < offset; j++) {
1171+
php_printf(" ");
1172+
}
11651173
php_printf(" %d ", *value);
11661174
CArrayIterator_NEXT(iterator);
11671175
}
11681176
}
11691177
}
11701178
if(array->descriptor->type == TYPE_DOUBLE) {
1179+
char tmp_str_num[320];
11711180
double * value;
11721181
if (notated) {
11731182
for (i = 0; i < CArray_DIMS(array)[current_dim]; i++) {
@@ -1179,12 +1188,22 @@ _print_recursive(CArray * array, CArrayIterator * iterator, int * index, int cur
11791188
if (has_digits) {
11801189
for (i = 0; i < CArray_DIMS(array)[current_dim]; i++) {
11811190
value = (double *) CArrayIterator_DATA(iterator);
1191+
snprintf(tmp_str_num, 320, "%.8f", *value);
1192+
offset = max_digits - strlen(tmp_str_num);
1193+
for (j = 0; j < offset; j++) {
1194+
php_printf(" ");
1195+
}
11821196
php_printf(" %.8f ", *value);
11831197
CArrayIterator_NEXT(iterator);
11841198
}
11851199
} else {
11861200
for (i = 0; i < CArray_DIMS(array)[current_dim]; i++) {
11871201
value = IT_DDATA(iterator);
1202+
snprintf(tmp_str_num, 320, "%.0f", *value);
1203+
offset = max_digits - strlen(tmp_str_num);
1204+
for (j = 0; j < offset; j++) {
1205+
php_printf(" ");
1206+
}
11881207
php_printf(" %.0f. ", *value);
11891208
CArrayIterator_NEXT(iterator);
11901209
}
@@ -1217,13 +1236,15 @@ _print_recursive(CArray * array, CArrayIterator * iterator, int * index, int cur
12171236
void
12181237
CArray_Print(CArray *array, int force_summary)
12191238
{
1239+
int biggest_number_len = 0;
12201240
int i;
12211241
int has_digits = 0;
12221242
int summarized = 0;
12231243
int x_summarized = 0;
12241244
int y_summarized = 0;
12251245
int notated = 0;
12261246
int start_index = 0;
1247+
12271248
if(array->ndim == 0) {
12281249
if(CArray_TYPE(array) == TYPE_DOUBLE_INT) {
12291250
php_printf("%f", DDATA(array)[0]);
@@ -1264,21 +1285,36 @@ CArray_Print(CArray *array, int force_summary)
12641285
CArrayIterator * it = CArray_NewIter(array);
12651286

12661287
if(CArray_TYPE(array) == TYPE_DOUBLE_INT) {
1288+
char tmp_str_num[320];
12671289
do {
12681290
if ((int)IT_DDATA(it)[0] != IT_DDATA(it)[0]) {
12691291
has_digits = 1;
12701292
}
12711293
if (IT_DDATA(it)[0] > 99999999) {
12721294
notated = 1;
12731295
}
1296+
if (has_digits) {
1297+
snprintf(tmp_str_num, 320, "%.8f", IT_DDATA(it)[0]);
1298+
} else {
1299+
snprintf(tmp_str_num, 320, "%.0f", IT_DDATA(it)[0]);
1300+
}
1301+
1302+
if (strlen(tmp_str_num) > biggest_number_len) {
1303+
biggest_number_len = strlen(tmp_str_num);
1304+
}
12741305
if (notated && has_digits) {
12751306
break;
12761307
}
12771308
CArrayIterator_NEXT(it);
12781309
} while (CArrayIterator_NOTDONE(it));
12791310
}
12801311
if(CArray_TYPE(array) == TYPE_INTEGER_INT) {
1312+
char tmp_str_num[11];
12811313
do {
1314+
snprintf(tmp_str_num, 11, "%d", IT_IDATA(it)[0]);
1315+
if (strlen(tmp_str_num) > biggest_number_len) {
1316+
biggest_number_len = strlen(tmp_str_num);
1317+
}
12821318
if (IT_IDATA(it)[0] > 99999999) {
12831319
notated = 1;
12841320
break;
@@ -1288,7 +1324,7 @@ CArray_Print(CArray *array, int force_summary)
12881324
}
12891325

12901326
CArrayIterator_RESET(it);
1291-
_print_recursive(array, it, &start_index, 0, summarized, x_summarized, y_summarized, notated, has_digits);
1327+
_print_recursive(array, it, &start_index, 0, summarized, x_summarized, y_summarized, notated, has_digits, biggest_number_len);
12921328
CArrayIterator_FREE(it);
12931329
php_printf("\n");
12941330
}

kernel/convert.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,6 @@ CArray_CanCastTo(CArrayDescriptor *from, CArrayDescriptor *to)
156156
ret = (from->elsize <= to->elsize);
157157
}
158158
}
159-
/*
160-
* TODO: If totype is STRING or unicode
161-
* see if the length is long enough to hold the
162-
* stringified value of the object.
163-
*/
164159
}
165160
return ret;
166161
}
@@ -225,10 +220,8 @@ CArray_FillWithScalar(CArray * arr, CArrayScalar * sc)
225220
CArrayDescriptor_FREE(dtype);
226221
return -1;
227222
}
228-
229-
/* Use the value pointer we got if possible */
223+
230224
if (value != NULL) {
231-
/* TODO: switch to SAME_KIND casting */
232225
retcode = CArray_AssignRawScalar(arr, dtype, value, NULL, CARRAY_UNSAFE_CASTING);
233226

234227
CArrayDescriptor_FREE(dtype);

0 commit comments

Comments
 (0)