@@ -145,7 +145,16 @@ void Dotenv::ParseContent(const std::string_view input) {
145
145
// If there is no equal character, then ignore everything
146
146
auto equal = content.find (' =' );
147
147
if (equal == std::string_view::npos) {
148
- break ;
148
+ auto newline = content.find (' \n ' );
149
+ if (newline != std::string_view::npos) {
150
+ // If we used `newline` only,
151
+ // the '\n' might remain and cause an empty-line parse
152
+ content.remove_prefix (newline + 1 );
153
+ } else {
154
+ content = {};
155
+ }
156
+ // No valid data here, skip to next line
157
+ continue ;
149
158
}
150
159
151
160
key = content.substr (0 , equal);
@@ -195,7 +204,9 @@ void Dotenv::ParseContent(const std::string_view input) {
195
204
store_.insert_or_assign (std::string (key), multi_line_value);
196
205
auto newline = content.find (' \n ' , closing_quote + 1 );
197
206
if (newline != std::string_view::npos) {
198
- content.remove_prefix (newline);
207
+ content.remove_prefix (newline + 1 );
208
+ } else {
209
+ content = {};
199
210
}
200
211
continue ;
201
212
}
@@ -216,7 +227,7 @@ void Dotenv::ParseContent(const std::string_view input) {
216
227
if (newline != std::string_view::npos) {
217
228
value = content.substr (0 , newline);
218
229
store_.insert_or_assign (std::string (key), value);
219
- content.remove_prefix (newline);
230
+ content.remove_prefix (newline + 1 );
220
231
}
221
232
} else {
222
233
// Example: KEY="value"
@@ -226,8 +237,13 @@ void Dotenv::ParseContent(const std::string_view input) {
226
237
// since there could be newline characters inside the value.
227
238
auto newline = content.find (' \n ' , closing_quote + 1 );
228
239
if (newline != std::string_view::npos) {
229
- content.remove_prefix (newline);
240
+ // Use +1 to discard the '\n' itself => next line
241
+ content.remove_prefix (newline + 1 );
242
+ } else {
243
+ content = {};
230
244
}
245
+ // No valid data here, skip to next line
246
+ continue ;
231
247
}
232
248
} else {
233
249
// Regular key value pair.
@@ -243,15 +259,21 @@ void Dotenv::ParseContent(const std::string_view input) {
243
259
if (hash_character != std::string_view::npos) {
244
260
value = content.substr (0 , hash_character);
245
261
}
246
- content.remove_prefix (newline);
262
+ store_.insert_or_assign (std::string (key), trim_spaces (value));
263
+ content.remove_prefix (newline + 1 );
247
264
} else {
248
265
// In case the last line is a single key/value pair
249
266
// Example: KEY=VALUE (without a newline at the EOF)
250
- value = content.substr (0 );
267
+ value = content;
268
+ auto hash_char = value.find (' #' );
269
+ if (hash_char != std::string_view::npos) {
270
+ value = content.substr (0 , hash_char);
271
+ }
272
+ store_.insert_or_assign (std::string (key), trim_spaces (value));
273
+ content = {};
251
274
}
252
275
253
- value = trim_spaces (value);
254
- store_.insert_or_assign (std::string (key), value);
276
+ store_.insert_or_assign (std::string (key), trim_spaces (value));
255
277
}
256
278
}
257
279
}
0 commit comments