@@ -18,8 +18,8 @@ using v8::String;
1818 */
1919const std::regex LINE (
2020 " \\ s*(?:export\\ s+)?([\\ w.-]+)(?:\\ s*=\\ s*?|:\\ s+?)(\\ s*'(?:\\\\ '|[^']"
21- " )*'|\\ s*\" (?:\\\\\" |[^\" ])*\" |\\ s*`(?:\\\\ `|[^`])*`|[^#\r\n ]+)?\\ s*(? "
22- " :#.*)? " ); // NOLINT(whitespace/line_length)
21+ " )*'|\\ s*\" (?:\\\\\" |[^\" ])*\" |\\ s*`(?:\\\\ `|[^`])*`|[^#\r\n ]+)?\\ s*" );
22+ // NOLINT(whitespace/line_length)
2323
2424std::vector<std::string> Dotenv::GetPathFromArgs (
2525 const std::vector<std::string>& args) {
@@ -104,6 +104,7 @@ Local<Object> Dotenv::ToObject(Environment* env) {
104104void Dotenv::ParseContent (const std::string_view content) {
105105 std::string lines = std::string (content);
106106 lines = std::regex_replace (lines, std::regex (" \r\n ?" ), " \n " );
107+ lines = RemoveComments (lines);
107108
108109 std::smatch match;
109110 while (std::regex_search (lines, match, LINE)) {
@@ -126,7 +127,7 @@ void Dotenv::ParseContent(const std::string_view content) {
126127 }
127128
128129 // Remove surrounding quotes
129- value = trim_quotes (value);
130+ value = TrimQuotes (value);
130131
131132 store_.insert_or_assign (std::string (key), value);
132133 lines = match.suffix ();
@@ -179,7 +180,7 @@ void Dotenv::AssignNodeOptionsIfAvailable(std::string* node_options) {
179180 }
180181}
181182
182- std::string_view Dotenv::trim_quotes (std::string_view str) {
183+ std::string_view Dotenv::TrimQuotes (std::string_view str) {
183184 static const std::unordered_set<char > quotes = {' "' , ' \' ' , ' `' };
184185 if (str.size () >= 2 && quotes.count (str.front ()) &&
185186 quotes.count (str.back ())) {
@@ -188,4 +189,22 @@ std::string_view Dotenv::trim_quotes(std::string_view str) {
188189 return str;
189190}
190191
192+ std::string Dotenv::RemoveComments (const std::string& content) {
193+ std::istringstream iss (content);
194+ std::string result;
195+ std::string line;
196+
197+ while (std::getline (iss, line)) {
198+ if (line.empty ()) {
199+ continue ;
200+ }
201+ // Trim leading whitespace and check if the line starts with '#'
202+ std::string trimmed = line.substr (line.find_first_not_of (" \t " ));
203+ if (trimmed.front () != ' #' ) {
204+ result.append (line + " \n " );
205+ }
206+ }
207+ return result;
208+ }
209+
191210} // namespace node
0 commit comments