Skip to content

Commit fe54105

Browse files
committed
Disable overflow with ellipsis in alternate format
1 parent e74834f commit fe54105

File tree

1 file changed

+86
-11
lines changed

1 file changed

+86
-11
lines changed

src/arrayformat.rs

+86-11
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,22 @@ use super::{ArrayBase, Axis, Data, Dimension, Ix, NdProducer};
99
use crate::aliases::Ix1;
1010
use std::fmt;
1111

12-
/// Maximum axis length before overflowing with an ellipsis.
12+
/// Default maximum axis length before overflowing with an ellipsis.
1313
const AXIS_LEN_LIMIT: Ix = 6;
1414

1515
/// The string used as an ellipsis.
1616
const ELLIPSIS: &str = "...";
1717

18+
/// Returns the axis length limit based on whether or not the alternate (`#`)
19+
/// flag was specified on the formatter.
20+
fn axis_len_limit(f: &mut fmt::Formatter<'_>) -> usize {
21+
if f.alternate() {
22+
std::usize::MAX
23+
} else {
24+
AXIS_LEN_LIMIT
25+
}
26+
}
27+
1828
/// Formats the contents of a list of items, using an ellipsis to indicate when
1929
/// the `length` of the list is greater than `limit`.
2030
///
@@ -129,7 +139,8 @@ where
129139
S: Data<Elem = A>,
130140
{
131141
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
132-
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
142+
let limit = axis_len_limit(f);
143+
format_array(self, f, <_>::fmt, limit, 0)
133144
}
134145
}
135146

@@ -142,8 +153,10 @@ where
142153
S: Data<Elem = A>,
143154
{
144155
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
156+
let limit = axis_len_limit(f);
157+
format_array(self, f, <_>::fmt, limit, 0)?;
158+
145159
// Add extra information for Debug
146-
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)?;
147160
write!(
148161
f,
149162
", shape={:?}, strides={:?}, layout={:?}",
@@ -168,7 +181,8 @@ where
168181
S: Data<Elem = A>,
169182
{
170183
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171-
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
184+
let limit = axis_len_limit(f);
185+
format_array(self, f, <_>::fmt, limit, 0)
172186
}
173187
}
174188

@@ -181,7 +195,8 @@ where
181195
S: Data<Elem = A>,
182196
{
183197
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184-
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
198+
let limit = axis_len_limit(f);
199+
format_array(self, f, <_>::fmt, limit, 0)
185200
}
186201
}
187202
/// Format the array using `LowerHex` and apply the formatting parameters used
@@ -193,7 +208,8 @@ where
193208
S: Data<Elem = A>,
194209
{
195210
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
196-
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
211+
let limit = axis_len_limit(f);
212+
format_array(self, f, <_>::fmt, limit, 0)
197213
}
198214
}
199215

@@ -206,7 +222,8 @@ where
206222
S: Data<Elem = A>,
207223
{
208224
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209-
format_array(self, f, <_>::fmt, AXIS_LEN_LIMIT, 0)
225+
let limit = axis_len_limit(f);
226+
format_array(self, f, <_>::fmt, limit, 0)
210227
}
211228
}
212229

@@ -250,16 +267,25 @@ mod formatting_with_omit {
250267

251268
#[test]
252269
fn dim_1() {
253-
let overflow: usize = 5;
270+
let overflow: usize = 2;
254271
let a = Array1::from_elem(AXIS_LEN_LIMIT + overflow, 1);
255272
let actual = format!("{}", a);
256273
let expected = "[1, 1, 1, ..., 1, 1, 1]";
257274
assert_str_eq(expected, &actual);
258275
}
259276

277+
#[test]
278+
fn dim_1_alternate() {
279+
let overflow: usize = 2;
280+
let a = Array1::from_elem(AXIS_LEN_LIMIT + overflow, 1);
281+
let actual = format!("{:#}", a);
282+
let expected = "[1, 1, 1, 1, 1, 1, 1, 1]";
283+
assert_str_eq(expected, &actual);
284+
}
285+
260286
#[test]
261287
fn dim_2_last_axis_overflow() {
262-
let overflow: usize = 3;
288+
let overflow: usize = 2;
263289
let a = Array2::from_elem((AXIS_LEN_LIMIT, AXIS_LEN_LIMIT + overflow), 1);
264290
let actual = format!("{}", a);
265291
let expected = "\
@@ -272,9 +298,24 @@ mod formatting_with_omit {
272298
assert_str_eq(expected, &actual);
273299
}
274300

301+
#[test]
302+
fn dim_2_last_axis_overflow_alternate() {
303+
let overflow: usize = 2;
304+
let a = Array2::from_elem((AXIS_LEN_LIMIT, AXIS_LEN_LIMIT + overflow), 1);
305+
let actual = format!("{:#}", a);
306+
let expected = "\
307+
[[1, 1, 1, 1, 1, 1, 1, 1],
308+
[1, 1, 1, 1, 1, 1, 1, 1],
309+
[1, 1, 1, 1, 1, 1, 1, 1],
310+
[1, 1, 1, 1, 1, 1, 1, 1],
311+
[1, 1, 1, 1, 1, 1, 1, 1],
312+
[1, 1, 1, 1, 1, 1, 1, 1]]";
313+
assert_str_eq(expected, &actual);
314+
}
315+
275316
#[test]
276317
fn dim_2_non_last_axis_overflow() {
277-
let overflow: usize = 5;
318+
let overflow: usize = 2;
278319
let a = Array2::from_elem((AXIS_LEN_LIMIT + overflow, AXIS_LEN_LIMIT), 1);
279320
let actual = format!("{}", a);
280321
let expected = "\
@@ -288,9 +329,26 @@ mod formatting_with_omit {
288329
assert_str_eq(expected, &actual);
289330
}
290331

332+
#[test]
333+
fn dim_2_non_last_axis_overflow_alternate() {
334+
let overflow: usize = 2;
335+
let a = Array2::from_elem((AXIS_LEN_LIMIT + overflow, AXIS_LEN_LIMIT), 1);
336+
let actual = format!("{:#}", a);
337+
let expected = "\
338+
[[1, 1, 1, 1, 1, 1],
339+
[1, 1, 1, 1, 1, 1],
340+
[1, 1, 1, 1, 1, 1],
341+
[1, 1, 1, 1, 1, 1],
342+
[1, 1, 1, 1, 1, 1],
343+
[1, 1, 1, 1, 1, 1],
344+
[1, 1, 1, 1, 1, 1],
345+
[1, 1, 1, 1, 1, 1]]";
346+
assert_str_eq(expected, &actual);
347+
}
348+
291349
#[test]
292350
fn dim_2_multi_directional_overflow() {
293-
let overflow: usize = 5;
351+
let overflow: usize = 2;
294352
let a = Array2::from_elem((AXIS_LEN_LIMIT + overflow, AXIS_LEN_LIMIT + overflow), 1);
295353
let actual = format!("{}", a);
296354
let expected = "\
@@ -304,6 +362,23 @@ mod formatting_with_omit {
304362
assert_str_eq(expected, &actual);
305363
}
306364

365+
#[test]
366+
fn dim_2_multi_directional_overflow_alternate() {
367+
let overflow: usize = 2;
368+
let a = Array2::from_elem((AXIS_LEN_LIMIT + overflow, AXIS_LEN_LIMIT + overflow), 1);
369+
let actual = format!("{:#}", a);
370+
let expected = "\
371+
[[1, 1, 1, 1, 1, 1, 1, 1],
372+
[1, 1, 1, 1, 1, 1, 1, 1],
373+
[1, 1, 1, 1, 1, 1, 1, 1],
374+
[1, 1, 1, 1, 1, 1, 1, 1],
375+
[1, 1, 1, 1, 1, 1, 1, 1],
376+
[1, 1, 1, 1, 1, 1, 1, 1],
377+
[1, 1, 1, 1, 1, 1, 1, 1],
378+
[1, 1, 1, 1, 1, 1, 1, 1]]";
379+
assert_str_eq(expected, &actual);
380+
}
381+
307382
#[test]
308383
fn dim_3_overflow_all() {
309384
let a = Array3::from_shape_fn((20, 10, 7), |(i, j, k)| {

0 commit comments

Comments
 (0)