From 86dec80726604ea01304d0bd2053adb71c07d8a0 Mon Sep 17 00:00:00 2001 From: Sebastian Stenzel Date: Wed, 18 Nov 2020 10:33:04 +0100 Subject: [PATCH] avoid weird iteration counter manipulation --- .../cryptomator/common/vaults/FuseVolume.java | 48 ++++++------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java b/main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java index 82095d9be..f8defc62e 100644 --- a/main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java +++ b/main/commons/src/main/java/org/cryptomator/common/vaults/FuseVolume.java @@ -1,5 +1,6 @@ package org.cryptomator.common.vaults; +import com.google.common.collect.Iterators; import org.apache.commons.lang3.SystemUtils; import org.cryptomator.common.mountpoint.InvalidMountPointException; import org.cryptomator.common.mountpoint.MountPointChooser; @@ -20,15 +21,12 @@ import java.nio.file.Path; import java.util.ArrayList; import java.util.List; import java.util.SortedSet; -import java.util.regex.MatchResult; import java.util.regex.Pattern; -import java.util.stream.Collectors; public class FuseVolume extends AbstractVolume { private static final Logger LOG = LoggerFactory.getLogger(FuseVolume.class); - - private static final Pattern pattern = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'"); //Thanks to https://stackoverflow.com/a/366532 + private static final Pattern NON_WHITESPACE_OR_QUOTED = Pattern.compile("[^\\s\"']+|\"([^\"]*)\"|'([^']*)'"); // Thanks to https://stackoverflow.com/a/366532 private Mount mount; @@ -57,37 +55,21 @@ public class FuseVolume extends AbstractVolume { } private String[] splitFlags(String str) { - List strings = new ArrayList<>(); - List results = pattern.matcher(str).results().collect(Collectors.toList()); - for (int i = 0; i < results.size(); i++) { - String current = group(results.get(i), false); - MatchResult next = i + 1 < results.size() ? results.get(i + 1) : null; - - if (group(next, true) != null && current.endsWith("=")) { - //"next" is a quoted elements and "current" is missing it's argument - //--> "next" must be joined with "current" and is skipped in the regular iteration - strings.add(current + group(next, true)); - i++; - } else { - //"next" is a normal unquoted string/is not missing from "current" - //--> Add "current" and advance - strings.add(current); + List flags = new ArrayList<>(); + var matches = Iterators.peekingIterator(NON_WHITESPACE_OR_QUOTED.matcher(str).results().iterator()); + while (matches.hasNext()) { + String flag = matches.next().group(); + // check if flag is missing its argument: + if (flag.endsWith("=") && matches.hasNext() && matches.peek().group(1) != null) { // next is "double quoted" + // next is "double quoted" and flag is missing its argument + flag += matches.next().group(1); + } else if (flag.endsWith("=") && matches.hasNext() && matches.peek().group(2) != null) { + // next is 'single quoted' and flag is missing its argument + flag += matches.next().group(2); } + flags.add(flag); } - return strings.toArray(new String[0]); - } - - private String group(MatchResult result, boolean onlyMatchQuoted) { - if (result == null) { - return null; - } - if (result.group(1) != null) { - return result.group(1); - } - if (result.group(2) != null || onlyMatchQuoted) { - return result.group(2); - } - return result.group(); + return flags.toArray(String[]::new); } @Override