@@ -19,15 +19,9 @@ public static EnvFile Load(string path)
1919 foreach ( var line in File . ReadAllLines ( path ) )
2020 {
2121 envFile . _lines . Add ( line ) ;
22- var trimmed = line . TrimStart ( ) ;
23- if ( ! trimmed . StartsWith ( '#' ) && trimmed . Contains ( '=' ) )
22+ if ( TryParseKey ( line , out var key ) )
2423 {
25- var eqIndex = trimmed . IndexOf ( '=' ) ;
26- if ( eqIndex > 0 )
27- {
28- var key = trimmed [ ..eqIndex ] . Trim ( ) ;
29- envFile . _keys . Add ( key ) ;
30- }
24+ envFile . _keys . Add ( key ) ;
3125 }
3226 }
3327 return envFile ;
@@ -45,19 +39,10 @@ public void Add(string key, string? value, string? comment, bool onlyIfMissing =
4539 // Update the existing key's value
4640 for ( int i = 0 ; i < _lines . Count ; i ++ )
4741 {
48- var trimmed = _lines [ i ] . TrimStart ( ) ;
49- if ( ! trimmed . StartsWith ( '#' ) && trimmed . Contains ( '=' ) )
42+ if ( TryParseKey ( _lines [ i ] , out var lineKey ) && lineKey == key )
5043 {
51- var eqIndex = trimmed . IndexOf ( '=' ) ;
52- if ( eqIndex > 0 )
53- {
54- var lineKey = trimmed [ ..eqIndex ] . Trim ( ) ;
55- if ( lineKey == key )
56- {
57- _lines [ i ] = value is not null ? $ "{ key } ={ value } " : $ "{ key } =";
58- return ;
59- }
60- }
44+ _lines [ i ] = value is not null ? $ "{ key } ={ value } " : $ "{ key } =";
45+ return ;
6146 }
6247 }
6348 }
@@ -71,6 +56,22 @@ public void Add(string key, string? value, string? comment, bool onlyIfMissing =
7156 _keys . Add ( key ) ;
7257 }
7358
59+ private static bool TryParseKey ( string line , out string key )
60+ {
61+ key = string . Empty ;
62+ var trimmed = line . TrimStart ( ) ;
63+ if ( ! trimmed . StartsWith ( '#' ) && trimmed . Contains ( '=' ) )
64+ {
65+ var eqIndex = trimmed . IndexOf ( '=' ) ;
66+ if ( eqIndex > 0 )
67+ {
68+ key = trimmed [ ..eqIndex ] . Trim ( ) ;
69+ return true ;
70+ }
71+ }
72+ return false ;
73+ }
74+
7475 public void Save ( string path )
7576 {
7677 File . WriteAllLines ( path , _lines ) ;
0 commit comments