@@ -29,11 +29,17 @@ fn format_1d_array<A, S, F>(
29
29
{
30
30
let n = view. len ( ) ;
31
31
let indexes_to_be_printed = indexes_to_be_printed ( n, limit) ;
32
+ let last_index = indexes_to_be_printed. len ( ) ;
32
33
write ! ( f, "[" ) ?;
33
- for index in indexes_to_be_printed {
34
+ for ( j , index) in indexes_to_be_printed. into_iter ( ) . enumerate ( ) {
34
35
match index {
35
- Some ( i) => format ( & view[ i] , f) ?,
36
- None => write ! ( f, ", ..., " ) ?,
36
+ Some ( i) => {
37
+ format ( & view[ i] , f) ?;
38
+ if j != ( last_index-1 ) {
39
+ write ! ( f, ", " ) ?;
40
+ }
41
+ } ,
42
+ None => write ! ( f, "..., " ) ?,
37
43
}
38
44
}
39
45
write ! ( f, "]" ) ?;
@@ -61,29 +67,35 @@ where
61
67
D : Dimension ,
62
68
S : Data < Elem =A > ,
63
69
{
64
- let view = view. view ( ) . into_dyn ( ) ;
70
+ if view. shape ( ) . iter ( ) . any ( |& x| x == 0 ) {
71
+ write ! ( f, "{}{}" , "[" . repeat( view. ndim( ) ) , "]" . repeat( view. ndim( ) ) ) ?;
72
+ return Ok ( ( ) )
73
+ }
65
74
match view. shape ( ) {
66
75
[ ] => format ( view. iter ( ) . next ( ) . unwrap ( ) , f) ?,
67
- [ _] => format_1d_array ( & view. into_dimensionality :: < Ix1 > ( ) . unwrap ( ) , f, format, limit) ?,
76
+ [ _] => format_1d_array ( & view. view ( ) . into_dimensionality :: < Ix1 > ( ) . unwrap ( ) , f, format, limit) ?,
68
77
shape => {
78
+ let view = view. view ( ) . into_dyn ( ) ;
69
79
let first_axis_length = shape[ 0 ] ;
70
80
let indexes_to_be_printed = indexes_to_be_printed ( first_axis_length, limit) ;
71
- writeln ! ( f, "[" ) ?;
72
- for index in indexes_to_be_printed {
81
+ let n_to_be_printed = indexes_to_be_printed. len ( ) ;
82
+ write ! ( f, "[" ) ?;
83
+ for ( j, index) in indexes_to_be_printed. into_iter ( ) . enumerate ( ) {
73
84
match index {
74
85
Some ( i) => {
75
- write ! ( f, " " ) ?;
76
86
format_array (
77
87
& view. index_axis ( Axis ( 0 ) , i) , f, format. clone ( ) , limit
78
88
) ?;
79
- writeln ! ( f, "," ) ?
89
+ if j != ( n_to_be_printed -1 ) {
90
+ write ! ( f, ",\n " ) ?
91
+ }
80
92
} ,
81
93
None => {
82
- writeln ! ( f, " ...," ) ?
94
+ write ! ( f, "...,\n " ) ?
83
95
}
84
96
}
85
97
}
86
- writeln ! ( f, "]" ) ?;
98
+ write ! ( f, "]" ) ?;
87
99
}
88
100
}
89
101
Ok ( ( ) )
@@ -183,15 +195,17 @@ mod formatting_with_omit {
183
195
let a: Array2 < u32 > = arr2 ( & [ [ ] , [ ] ] ) ;
184
196
let actual_output = format ! ( "{}" , a) ;
185
197
let expected_output = String :: from ( "[[]]" ) ;
186
- assert_eq ! ( actual_output, expected_output) ;
198
+ print_output_diff ( & expected_output, & actual_output) ;
199
+ assert_eq ! ( expected_output, actual_output) ;
187
200
}
188
201
189
202
#[ test]
190
203
fn zero_length_axes ( ) {
191
204
let a = Array3 :: < f32 > :: zeros ( ( 3 , 0 , 4 ) ) ;
192
205
let actual_output = format ! ( "{}" , a) ;
193
206
let expected_output = String :: from ( "[[[]]]" ) ;
194
- assert_eq ! ( actual_output, expected_output) ;
207
+ print_output_diff ( & expected_output, & actual_output) ;
208
+ assert_eq ! ( expected_output, actual_output) ;
195
209
}
196
210
197
211
#[ test]
@@ -200,7 +214,8 @@ mod formatting_with_omit {
200
214
let a = arr0 ( element) ;
201
215
let actual_output = format ! ( "{}" , a) ;
202
216
let expected_output = format ! ( "{}" , element) ;
203
- assert_eq ! ( actual_output, expected_output) ;
217
+ print_output_diff ( & expected_output, & actual_output) ;
218
+ assert_eq ! ( expected_output, actual_output) ;
204
219
}
205
220
206
221
#[ test]
0 commit comments