@@ -11,7 +11,7 @@ unsafe void ParseCommandLine() {
1111 CleanUpParms ( ) ;
1212 if ( cmdLine == null ) return ;
1313
14- fixed( char * pCharFx = cmdLine ) {
14+ fixed ( char * pCharFx = cmdLine ) {
1515 char * pChar = pCharFx ;
1616
1717 while ( * pChar > 0 && char . IsWhiteSpace ( * pChar ) )
@@ -30,8 +30,8 @@ unsafe void ParseCommandLine() {
3030 continue ;
3131 }
3232
33- if ( firstLetter == null ) {
34- if ( * pChar == '\" ' ) {
33+ if ( firstLetter == null ) {
34+ if ( * pChar == '\" ' ) {
3535 inQuotes = true ;
3636 firstLetter = pChar + 1 ;
3737 continue ;
@@ -107,20 +107,20 @@ public unsafe void CreateCmdLine(ReadOnlySpan<char> commandLine) {
107107 full [ 0 ] = '\0 ' ;
108108
109109 char * dst = full ;
110- fixed( char * pCommandLine = commandLine ) {
110+ fixed ( char * pCommandLine = commandLine ) {
111111 char * src = pCommandLine ;
112112 bool inQuotes = false ;
113113 char * inQuotesStart = null ;
114- while ( * src > 0 ) {
115- if ( * src == '"' ) {
116- if ( src == pCommandLine || ( src [ - 1 ] != '/' && src [ - 1 ] != '\\ ' ) ) {
114+ while ( * src > 0 ) {
115+ if ( * src == '"' ) {
116+ if ( src == pCommandLine || ( src [ - 1 ] != '/' && src [ - 1 ] != '\\ ' ) ) {
117117 inQuotes = ! inQuotes ;
118118 inQuotesStart = src + 1 ;
119119 }
120120 }
121121
122- if ( * src == '*' ) {
123- if ( src == pCommandLine || ( inQuotes && char . IsWhiteSpace ( src [ - 1 ] ) ) || ( inQuotes && src == inQuotesStart ) ) {
122+ if ( * src == '*' ) {
123+ if ( src == pCommandLine || ( inQuotes && char . IsWhiteSpace ( src [ - 1 ] ) ) || ( inQuotes && src == inQuotesStart ) ) {
124124 LoadParametersFromFile ( src , dst , MAX_BUFFER_LEN - ( ( nint ) dst - ( nint ) full ) , inQuotes ) ;
125125 continue ;
126126 }
@@ -224,11 +224,93 @@ public string GetParm(int index) {
224224 return parms [ index + 1 ] ;
225225 }
226226
227- public unsafe void RemoveParm ( string name ) {
228- throw new NotImplementedException ( ) ;
227+
228+ static int StrLen ( char [ ] buffer , int start ) {
229+ int len = 0 ;
230+ while ( buffer [ start + len ] != '\0 ' )
231+ len ++ ;
232+ return len ;
233+ }
234+
235+ static int StrIStr ( char [ ] buffer , int start , string needle ) {
236+ int haystackLen = StrLen ( buffer , start ) ;
237+ if ( needle . Length > haystackLen )
238+ return - 1 ;
239+
240+ for ( int i = 0 ; i <= haystackLen - needle . Length ; i ++ ) {
241+ bool match = true ;
242+ for ( int j = 0 ; j < needle . Length ; j ++ ) {
243+ if ( char . ToLowerInvariant ( buffer [ start + i + j ] ) != char . ToLowerInvariant ( needle [ j ] ) ) {
244+ match = false ;
245+ break ;
246+ }
247+ }
248+ if ( match )
249+ return start + i ;
250+ }
251+
252+ return - 1 ;
253+ }
254+
255+ public void RemoveParm ( string name ) {
256+ if ( cmdLine == null )
257+ return ;
258+
259+ int nParmLen = name . Length ;
260+
261+ char [ ] buffer = new char [ cmdLine . Length + 1 ] ;
262+ cmdLine . AsSpan ( ) . CopyTo ( buffer ) ;
263+ buffer [ cmdLine . Length ] = '\0 ' ;
264+
265+ int p = 0 ;
266+ while ( buffer [ p ] != '\0 ' ) {
267+ int curlen = StrLen ( buffer , p ) ;
268+
269+ int found = StrIStr ( buffer , p , name ) ;
270+ if ( found == - 1 )
271+ break ;
272+
273+ int nextparam = found + 1 ;
274+ bool hadQuote = false ;
275+ if ( found > 0 && buffer [ found - 1 ] == '"' )
276+ hadQuote = true ;
277+
278+ while ( buffer [ nextparam ] != '\0 ' && buffer [ nextparam ] != ' ' && buffer [ nextparam ] != '"' )
279+ nextparam ++ ;
280+
281+ if ( ( nextparam - found ) > nParmLen ) {
282+ p = nextparam ;
283+ continue ;
284+ }
285+
286+ while ( buffer [ nextparam ] != '\0 ' && buffer [ nextparam ] != '-' && buffer [ nextparam ] != '+' )
287+ nextparam ++ ;
288+
289+ if ( hadQuote )
290+ found -- ;
291+
292+ if ( buffer [ nextparam ] != '\0 ' ) {
293+ int n = curlen - ( nextparam - p ) ;
294+ new Span < char > ( buffer , nextparam , n ) . CopyTo ( new Span < char > ( buffer , found , n ) ) ;
295+ buffer [ found + n ] = '\0 ' ;
296+ }
297+ else {
298+ int n = nextparam - found ;
299+ new Span < char > ( buffer , found , n ) . Clear ( ) ;
300+ }
301+ }
302+
303+ int len = StrLen ( buffer , 0 ) ;
304+ while ( len > 0 && buffer [ len - 1 ] == ' ' )
305+ len -- ;
306+
307+ cmdLine = new string ( buffer , 0 , len ) ;
308+ ParseCommandLine ( ) ;
229309 }
230310
231311 public void SetParm ( int index , string newParm ) {
232- throw new NotImplementedException ( ) ;
312+ Dbg . Assert ( index >= 0 && index < parms . Count ) ;
313+ if ( index >= 0 && index < parms . Count )
314+ parms [ index ] = newParm ;
233315 }
234316}
0 commit comments