File tree Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Original file line number Diff line number Diff line change @@ -43,14 +43,36 @@ bool List::contains(const Value &value) const
43
43
return (indexOf (value) != -1 );
44
44
}
45
45
46
- /* ! Joins the list items with spaces. */
46
+ /* ! Joins the list items with spaces or without any separator if there are only digits . */
47
47
std::string List::toString () const
48
48
{
49
49
std::string ret;
50
- for (int i = 0 ; i < size (); i++) {
51
- ret.append (at (i).toString ());
52
- if (i + 1 < size ())
53
- ret.push_back (' ' );
50
+ bool digits = true ;
51
+
52
+ for (const auto &item : *this ) {
53
+ if (item.type () == Value::Type::Integer) {
54
+ long num = item.toLong ();
55
+
56
+ if (num < 0 || num >= 10 ) {
57
+ digits = false ;
58
+ break ;
59
+ }
60
+ } else {
61
+ digits = false ;
62
+ break ;
63
+ }
54
64
}
65
+
66
+ if (digits) {
67
+ for (const auto &item : *this )
68
+ ret.append (item.toString ());
69
+ } else {
70
+ for (int i = 0 ; i < size (); i++) {
71
+ ret.append (at (i).toString ());
72
+ if (i + 1 < size ())
73
+ ret.push_back (' ' );
74
+ }
75
+ }
76
+
55
77
return ret;
56
78
}
Original file line number Diff line number Diff line change @@ -151,4 +151,22 @@ TEST(ListTest, ToString)
151
151
list.push_back (" áä" );
152
152
list.push_back (" ľ š" );
153
153
ASSERT_EQ (list.toString (), " áä ľ š" );
154
+
155
+ list.clear ();
156
+ list.push_back (-2 );
157
+ list.push_back (5 );
158
+ list.push_back (8 );
159
+ ASSERT_EQ (list.toString (), " -2 5 8" );
160
+
161
+ list.clear ();
162
+ list.push_back (2 );
163
+ list.push_back (10 );
164
+ list.push_back (8 );
165
+ ASSERT_EQ (list.toString (), " 2 10 8" );
166
+
167
+ list.clear ();
168
+ list.push_back (0 );
169
+ list.push_back (9 );
170
+ list.push_back (8 );
171
+ ASSERT_EQ (list.toString (), " 098" );
154
172
}
You can’t perform that action at this time.
0 commit comments