authorgravatar for carter.sande@duodecima.technologyCarter Sande <carter.sande@duodecima.technology> 2020-06-22 02:53:14-07:00
committergravatar for carter.sande@duodecima.technologyCarter Sande <carter.sande@duodecima.technology> 2020-06-22 03:03:30-07:00
log7cb41a415a6eca7d13980a62ee5727b4adcad8b0
treecbe3affdbc5b7c1a664e3601d4f2abcf22adff6e
parent8faa85ac19e1bbbe962d69b283c94526e35f5338

ArgIteratorWindows: simplify quote state tracking


1 files changed, 6 insertions(+), 8 deletions(-)

lib/std/process.zig+6-8
...@@ -281,8 +281,6 @@ pub const ArgIteratorWasi = struct {...@@ -281,8 +281,6 @@ pub const ArgIteratorWasi = struct {
281pub const ArgIteratorWindows = struct {281pub const ArgIteratorWindows = struct {
282 index: usize,282 index: usize,
283 cmd_line: [*]const u8,283 cmd_line: [*]const u8,
284 in_quote: bool,
285 seen_quote_count: usize,
286284
287 pub const NextError = error{OutOfMemory};285 pub const NextError = error{OutOfMemory};
288286
...@@ -294,8 +292,6 @@ pub const ArgIteratorWindows = struct {...@@ -294,8 +292,6 @@ pub const ArgIteratorWindows = struct {
294 return ArgIteratorWindows{292 return ArgIteratorWindows{
295 .index = 0,293 .index = 0,
296 .cmd_line = cmd_line,294 .cmd_line = cmd_line,
297 .in_quote = false,
298 .seen_quote_count = 0,
299 };295 };
300 }296 }
301297
...@@ -326,6 +322,7 @@ pub const ArgIteratorWindows = struct {...@@ -326,6 +322,7 @@ pub const ArgIteratorWindows = struct {
326 }322 }
327323
328 var backslash_count: usize = 0;324 var backslash_count: usize = 0;
325 var in_quote = false;
329 while (true) : (self.index += 1) {326 while (true) : (self.index += 1) {
330 const byte = self.cmd_line[self.index];327 const byte = self.cmd_line[self.index];
331 switch (byte) {328 switch (byte) {
...@@ -333,14 +330,14 @@ pub const ArgIteratorWindows = struct {...@@ -333,14 +330,14 @@ pub const ArgIteratorWindows = struct {
333 '"' => {330 '"' => {
334 const quote_is_real = backslash_count % 2 == 0;331 const quote_is_real = backslash_count % 2 == 0;
335 if (quote_is_real) {332 if (quote_is_real) {
336 self.seen_quote_count += 1;333 in_quote = !in_quote;
337 }334 }
338 },335 },
339 '\\' => {336 '\\' => {
340 backslash_count += 1;337 backslash_count += 1;
341 },338 },
342 ' ', '\t' => {339 ' ', '\t' => {
343 if (self.seen_quote_count % 2 == 0) {340 if (!in_quote) {
344 return true;341 return true;
345 }342 }
346 backslash_count = 0;343 backslash_count = 0;
...@@ -358,6 +355,7 @@ pub const ArgIteratorWindows = struct {...@@ -358,6 +355,7 @@ pub const ArgIteratorWindows = struct {
358 defer buf.deinit();355 defer buf.deinit();
359356
360 var backslash_count: usize = 0;357 var backslash_count: usize = 0;
358 var in_quote = false;
361 while (true) : (self.index += 1) {359 while (true) : (self.index += 1) {
362 const byte = self.cmd_line[self.index];360 const byte = self.cmd_line[self.index];
363 switch (byte) {361 switch (byte) {
...@@ -368,7 +366,7 @@ pub const ArgIteratorWindows = struct {...@@ -368,7 +366,7 @@ pub const ArgIteratorWindows = struct {
368 backslash_count = 0;366 backslash_count = 0;
369367
370 if (quote_is_real) {368 if (quote_is_real) {
371 self.seen_quote_count += 1;369 in_quote = !in_quote;
372 } else {370 } else {
373 try buf.append('"');371 try buf.append('"');
374 }372 }
...@@ -379,7 +377,7 @@ pub const ArgIteratorWindows = struct {...@@ -379,7 +377,7 @@ pub const ArgIteratorWindows = struct {
379 ' ', '\t' => {377 ' ', '\t' => {
380 try self.emitBackslashes(&buf, backslash_count);378 try self.emitBackslashes(&buf, backslash_count);
381 backslash_count = 0;379 backslash_count = 0;
382 if (self.seen_quote_count % 2 == 1) {380 if (in_quote) {
383 try buf.append(byte);381 try buf.append(byte);
384 } else {382 } else {
385 return buf.toOwnedSlice();383 return buf.toOwnedSlice();