Skip to content

StringUtil#splitOnEverySecondWhiteSpace returns empty collection when string with no whitespace is passed as argument. #1

@Trup10ka

Description

@Trup10ka

This bug causes functions which use it to act how they should not.

String parsing function:

    @NotNull
    public static List<@NotNull String> splitOnEverySecondWhiteSpace(@NotNull String args)
    {
        String regex = "(\\S+\\s+\\S+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(args);

        List<String> result = new ArrayList<>();

        while (matcher.find())
        {
            result.add(matcher.group());
        }

        return result;
    }

For example, when parsing arguments in:

    @Override
    @NotNull
    public Map<String, @Nullable String> parse(@NotNull String args, @NotNull String keyValueDelimiter)
    {
        Map<String, @Nullable String> result = new HashMap<>();
        List<String> pairs = StringUtil.splitOnEverySecondWhiteSpace(args);
        List<String> validKeys = Arrays.asList(keys);

        for (String pair : pairs)
        {
            String[] keyValue = pair.split(keyValueDelimiter);
            if (keyValue.length != 2 || keyValue[1].isEmpty())
            {
                throw new IllegalArgumentException("Missing value for key: " + keyValue[0]);
            }
            if (!validKeys.contains(keyValue[0]))
            {
                System.out.println("Unrecognized key: " + keyValue[0]);
                return new HashMap<>();
            }
            result.put(keyValue[0], keyValue[1]);
        }
        return result;
    }

If we enter in console:

kappa> cc -fn

expected output should be:

kappa> cc -fn
Missing value for '-fn' key

but actual is

Failed to parse arguments

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions