GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
str-vec.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-2025 The Octave Project Developers
4//
5// See the file COPYRIGHT.md in the top-level directory of this
6// distribution or <https://octave.org/copyright/>.
7//
8// This file is part of Octave.
9//
10// Octave is free software: you can redistribute it and/or modify it
11// under the terms of the GNU General Public License as published by
12// the Free Software Foundation, either version 3 of the License, or
13// (at your option) any later version.
14//
15// Octave is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18// GNU General Public License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with Octave; see the file COPYING. If not, see
22// <https://www.gnu.org/licenses/>.
23//
24////////////////////////////////////////////////////////////////////////
25
26/*
27
28The function string_vector::list_in_columns was adapted from a similar
29function distributed in the GNU file utilities, copyright (C) 85, 88,
3090, 91, 95, 1996 Free Software Foundation, Inc.
31
32*/
33
34#if defined (HAVE_CONFIG_H)
35# include "config.h"
36#endif
37
38#include <ostream>
39#include <string>
40
41#include "cmd-edit.h"
42#include "lo-utils.h"
43#include "str-vec.h"
44
45// Create a string vector from a NULL terminated list of C strings.
46
47string_vector::string_vector (const char * const *s)
48 : m_data ()
49{
50 octave_idx_type n = 0;
51
52 if (s)
53 {
54 const char * const *t = s;
55
56 while (*t++)
57 n++;
58 }
59
60 resize (n);
61
62 for (octave_idx_type i = 0; i < n; i++)
63 elem (i) = s[i];
64}
65
66// Create a string vector from up to N C strings. Assumes that N is
67// nonnegative.
68
70 : m_data (dim_vector (n, 1))
71{
72 for (octave_idx_type i = 0; i < n; i++)
73 elem (i) = s[i];
74}
75
77string_vector::sort (bool make_uniq)
78{
79 // Don't use Array<std::string>::sort () to allow sorting in place.
81 lsort.sort (m_data.rwdata (), numel ());
82
83 if (make_uniq)
84 uniq ();
85
86 return *this;
87}
90{
92
93 if (len > 0)
94 {
95 octave_idx_type k = 0;
96
97 for (octave_idx_type i = 1; i < len; i++)
98 if (elem (i) != elem (k))
99 if (++k != i)
100 elem (k) = elem (i);
101
102 if (len != ++k)
103 resize (k);
104 }
105
106 return *this;
107}
108
110string_vector::append (const std::string& s)
111{
113
114 resize (len + 1);
115
116 elem (len) = s;
117
118 return *this;
119}
120
123{
125 octave_idx_type sv_len = sv.numel ();
126 octave_idx_type new_len = len + sv_len;
127
128 resize (new_len);
129
130 for (octave_idx_type i = 0; i < sv_len; i++)
131 elem (len + i) = sv[i];
132
133 return *this;
134}
135
136std::string
137string_vector::join (const std::string& sep) const
138{
139 std::string retval;
140
142
143 if (len > 0)
144 {
146
147 for (i = 0; i < len - 1; i++)
148 retval += elem (i) + sep;
149
150 retval += elem (i);
151 }
152
153 return retval;
154}
155
156char **
158{
160
161 char **retval = new char * [len + 1];
162
163 retval[len] = nullptr;
164
165 for (octave_idx_type i = 0; i < len; i++)
166 retval[i] = octave::strsave (elem (i).c_str ());
167
168 return retval;
169}
170
171std::list<std::string>
173{
175
176 std::list<std::string> retval;
177
178 for (octave_idx_type i = 0; i < len; i++)
179 retval.push_back (elem (i));
180
181 return retval;
182}
183
184void
185string_vector::delete_c_str_vec (const char * const *v)
186{
187 if (! v)
188 return;
189
190 const char * const *p = v;
191
192 while (*p)
193 delete [] *p++;
194
195 delete [] v;
196}
197
198// Format a list in neat columns.
199
200std::ostream&
201string_vector::list_in_columns (std::ostream& os, int width,
202 const std::string& prefix) const
203{
204 // Compute the maximum name length.
205
206 octave_idx_type max_name_length = 0;
207 octave_idx_type total_names = numel ();
208
209 if (total_names == 0)
210 {
211 // List empty, remember to end output with a newline.
212
213 os << "\n";
214 return os;
215 }
216
217 for (octave_idx_type i = 0; i < total_names; i++)
218 {
219 octave_idx_type name_length = elem (i).length ();
220 if (name_length > max_name_length)
221 max_name_length = name_length;
222 }
223
224 // Allow at least two spaces between names.
225
226 max_name_length += 2;
227
228 // Calculate the maximum number of columns that will fit.
229
230 octave_idx_type line_length
231 = ((width <= 0 ? octave::command_editor::terminal_cols () : width)
232 - prefix.length ());
233
234 octave_idx_type nc = line_length / max_name_length;
235 if (nc == 0)
236 nc = 1;
237
238 // Calculate the number of rows that will be in each column except
239 // possibly for a short column on the right.
240
241 octave_idx_type nr = total_names / nc + (total_names % nc != 0);
242
243 octave_idx_type count;
244 for (octave_idx_type row = 0; row < nr; row++)
245 {
246 count = row;
247
248 // Print the next row.
249
250 os << prefix;
251
252 while (1)
253 {
254 std::string nm = elem (count);
255
256 os << nm;
257 octave_idx_type name_length = nm.length ();
258
259 count += nr;
260 if (count >= total_names)
261 break;
262
263 octave_idx_type spaces_to_pad = max_name_length - name_length;
264 for (octave_idx_type i = 0; i < spaces_to_pad; i++)
265 os << ' ';
266 }
267 os << "\n";
268 }
269
270 return os;
271}
T * rwdata()
Size of the specified dimension.
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
void sort(T *data, octave_idx_type nel)
Definition oct-sort.cc:1521
string_vector & append(const std::string &s)
Definition str-vec.cc:110
string_vector & sort(bool make_uniq=false)
Definition str-vec.cc:77
void resize(octave_idx_type n, const std::string &rfv="")
Definition str-vec.h:93
static void delete_c_str_vec(const char *const *)
Definition str-vec.cc:185
std::ostream & list_in_columns(std::ostream &, int width=0, const std::string &prefix="") const
Definition str-vec.cc:201
string_vector & uniq()
Definition str-vec.cc:89
octave_idx_type numel() const
Definition str-vec.h:98
std::string join(const std::string &sep="") const
Definition str-vec.cc:137
char ** c_str_vec() const
Definition str-vec.cc:157
std::list< std::string > std_list() const
Definition str-vec.cc:172
string_vector()=default
std::string & elem(octave_idx_type i)
Definition str-vec.h:102
F77_RET_T len
Definition xerbla.cc:61