1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// This file is part of yash, an extended POSIX shell.
// Copyright (C) 2022 WATANABE Yuki
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Field splitting
//!
//! This module provides components for field splitting used in word expansion.
//! [`Ifs`] represents the field separator characters, and [`Class`] classifies
//! them into whitespace and non-whitespace characters. [`Ranges`] is an
//! iterator that yields the ranges of characters in a field separated by the
//! field separators. The [`split_into`] and [`split`] functions perform field
//! splitting on a given field using a given IFS.
//!
//! # Field splitting semantics
//!
//! Field splitting divides a field into smaller parts delimited by a field
//! separator character, which is usually obtained from the `$IFS` variable.
//! Every occurrence of a non-whitespace separator delimits a new field (which
//! may be an empty field). One or more adjacent whitespace separators in the
//! middle of a field further split the field. The separators are not included
//! in the final results.
//!
//! Only [unquoted characters](super::attr::AttrChar) having a
//! `SoftExpansion` [origin](super::attr::Origin) are considered for delimiting.
//! Other characters are not subject to field splitting.
//!
//! # Example
//!
//! ```
//! use yash_env::semantics::expansion::attr::{AttrChar, AttrField, Origin};
//! use yash_env::semantics::expansion::split::{Ifs, split};
//! use yash_env::source::Location;
//!
//! // We use this utility to prepare fields used in the examples below:
//! fn field(s: &str) -> AttrField {
//! let chars = s.chars()
//! .map(|c| AttrChar {
//! value: c,
//! origin: Origin::SoftExpansion,
//! is_quoted: false,
//! is_quoting: false,
//! })
//! .collect();
//! let origin = Location::dummy("");
//! AttrField { chars, origin }
//! }
//!
//! let ifs = Ifs::new(" -");
//!
//! // When there are no separators in the input, the result is the input itself:
//! let fields: Vec<AttrField> = split(field("abc"), &ifs);
//! assert_eq!(fields, [field("abc")]);
//!
//! // Whitespace separators are removed:
//! let fields: Vec<AttrField> = split(field(" abc "), &ifs);
//! assert_eq!(fields, [field("abc")]);
//!
//! // An empty input yields no fields rather than an empty field:
//! let fields: Vec<AttrField> = split(field(""), &ifs);
//! assert_eq!(fields, []);
//!
//! // Whitespace separators split fields:
//! let fields: Vec<AttrField> = split(field("foo bar baz"), &ifs);
//! assert_eq!(fields, [field("foo"), field("bar"), field("baz")]);
//!
//! // Non-whitespace separators each split fields, which may produce empty fields:
//! let fields: Vec<AttrField> = split(field("foo-bar--baz"), &ifs);
//! assert_eq!(fields, [field("foo"), field("bar"), field(""), field("baz")]);
//!
//! // Whitespace separators around non-whitespace separators are ignored:
//! let fields: Vec<AttrField> = split(field("foo - bar - - baz"), &ifs);
//! assert_eq!(fields, [field("foo"), field("bar"), field(""), field("baz")]);
//!
//! // Trailing non-whitespace separators may seem special:
//! let fields: Vec<AttrField> = split(field("foo-bar"), &ifs);
//! assert_eq!(fields, [field("foo"), field("bar")]);
//! let fields: Vec<AttrField> = split(field("foo-bar-"), &ifs);
//! assert_eq!(fields, [field("foo"), field("bar")]);
//! let fields: Vec<AttrField> = split(field("foo-bar--"), &ifs);
//! assert_eq!(fields, [field("foo"), field("bar"), field("")]);
//! ```
//!
//! # The empty-last-field option
//!
//! TODO: Not yet supported
pub use ;
pub use Ranges;
use AttrField;
/// Performs field splitting and appends the result to a collection.
///
/// This function applies field splitting to the given field using the given IFS
/// and extends the given collection with the results. The resultant fields
/// share the same origin as the input field.
///
/// See also [`split`], which returns the results in a new collection rather
/// than extending an existing one.
/// Performs field splitting and returns the result in a new collection.
///
/// This function works similarly to [`split_into`], but returns the results in
/// a new collection.