malachite_base/rational_sequences/to_string.rs
1// Copyright © 2025 Mikhail Hogrefe
2//
3// This file is part of Malachite.
4//
5// Malachite is free software: you can redistribute it and/or modify it under the terms of the GNU
6// Lesser General Public License (LGPL) as published by the Free Software Foundation; either version
7// 3 of the License, or (at your option) any later version. See <https://www.gnu.org/licenses/>.
8
9use crate::rational_sequences::RationalSequence;
10use core::fmt::{Debug, Display, Formatter, Result, Write};
11
12impl<T: Display + Eq> Display for RationalSequence<T> {
13 /// Converts a [`RationalSequence`] to a [`String`].
14 ///
15 /// # Worst-case complexity
16 /// $T(n) = O(n)$
17 ///
18 /// $M(n) = O(n)$
19 ///
20 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.component_len()`.
21 ///
22 /// # Examples
23 /// ```
24 /// use malachite_base::rational_sequences::RationalSequence;
25 ///
26 /// assert_eq!(
27 /// RationalSequence::<u8>::from_vecs(vec![], vec![]).to_string(),
28 /// "[]"
29 /// );
30 /// assert_eq!(
31 /// RationalSequence::<u8>::from_vecs(vec![], vec![1, 2]).to_string(),
32 /// "[[1, 2]]"
33 /// );
34 /// assert_eq!(
35 /// RationalSequence::<u8>::from_vecs(vec![1, 2], vec![]).to_string(),
36 /// "[1, 2]"
37 /// );
38 /// assert_eq!(
39 /// RationalSequence::<u8>::from_vecs(vec![1, 2], vec![3, 4]).to_string(),
40 /// "[1, 2, [3, 4]]"
41 /// );
42 /// ```
43 fn fmt(&self, f: &mut Formatter) -> Result {
44 f.write_char('[')?;
45 let mut first = true;
46 for x in &self.non_repeating {
47 if first {
48 first = false;
49 } else {
50 f.write_str(", ")?;
51 }
52 Display::fmt(x, f)?;
53 }
54 if !self.repeating.is_empty() {
55 if !self.non_repeating.is_empty() {
56 f.write_str(", ")?;
57 }
58 f.write_char('[')?;
59 let mut first = true;
60 for x in &self.repeating {
61 if first {
62 first = false;
63 } else {
64 f.write_str(", ")?;
65 }
66 Display::fmt(x, f)?;
67 }
68 f.write_char(']')?;
69 }
70 f.write_char(']')
71 }
72}
73
74impl<T: Display + Eq> Debug for RationalSequence<T> {
75 /// Converts a [`RationalSequence`] to a [`String`].
76 ///
77 /// This is the same implementation as for [`Display`].
78 ///
79 /// # Worst-case complexity
80 /// $T(n) = O(n)$
81 ///
82 /// $M(n) = O(n)$
83 ///
84 /// where $T$ is time, $M$ is additional memory, and $n$ is `self.component_len()`.
85 ///
86 /// # Examples
87 /// ```
88 /// use malachite_base::rational_sequences::RationalSequence;
89 /// use malachite_base::strings::ToDebugString;
90 ///
91 /// assert_eq!(
92 /// RationalSequence::<u8>::from_vecs(vec![], vec![]).to_debug_string(),
93 /// "[]"
94 /// );
95 /// assert_eq!(
96 /// RationalSequence::<u8>::from_vecs(vec![], vec![1, 2]).to_debug_string(),
97 /// "[[1, 2]]"
98 /// );
99 /// assert_eq!(
100 /// RationalSequence::<u8>::from_vecs(vec![1, 2], vec![]).to_debug_string(),
101 /// "[1, 2]"
102 /// );
103 /// assert_eq!(
104 /// RationalSequence::<u8>::from_vecs(vec![1, 2], vec![3, 4]).to_string(),
105 /// "[1, 2, [3, 4]]"
106 /// );
107 /// ```
108 fn fmt(&self, f: &mut Formatter) -> Result {
109 Display::fmt(self, f)
110 }
111}