kl0u commented on a change in pull request #9976: [FLINK-14493][core] Introduce data types to ConfigOptions. URL: https://github.com/apache/flink/pull/9976#discussion_r339461108
########## File path: flink-core/src/main/java/org/apache/flink/configuration/StructuredOptionsSplitter.java ########## @@ -0,0 +1,176 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.configuration; + +import org.apache.flink.annotation.Internal; + +import java.util.ArrayList; +import java.util.List; + +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * Helper class for splitting a string on a given delimiter with quoting logic. + */ +@Internal +class StructuredOptionsSplitter { + + /** + * Splits the given string on the given delimiter. It supports quoting parts of the string with + * either single (') or double quotes ("). Quotes can be escaped by doubling the quotes. + * + * <p>Examples: + * <ul> + * <li>'A;B';C => [A;B], [C]</li> + * <li>"AB'D";B;C => [AB'D], [B], [C]</li> + * <li>"AB'""D;B";C => [AB'\"D;B], [C]</li> + * </ul> + * + * <p>For more examples check the tests. + * @param string a string to split + * @param delimiter delimiter to split on + * @return a list of splits + */ + static List<String> splitEscaped(String string, char delimiter) { + List<Token> tokens = tokenize(checkNotNull(string), delimiter); + return processTokens(tokens); + } + + private static List<String> processTokens(List<Token> tokens) { + final List<String> splits = new ArrayList<>(); + for (int i = 0; i < tokens.size(); i++) { + Token token = tokens.get(i); + switch (token.getTokenType()) { + case DOUBLE_QUOTED: + case SINGLE_QUOTED: + if (i + 1 < tokens.size() && tokens.get(i + 1).getTokenType() != TokenType.DELIMITER) { + int illegalPosition = tokens.get(i + 1).getPosition() - 1; + throw new IllegalArgumentException( + "Could not split string. Illegal quoting at position: " + illegalPosition); + } + splits.add(token.getString()); + break; + case UNQUOTED: + splits.add(token.getString()); + break; + case DELIMITER: + if (i + 1 < tokens.size() && tokens.get(i + 1).getTokenType() == TokenType.DELIMITER) { + splits.add(""); + } + break; + } + } + + return splits; + } + + private static List<Token> tokenize(String string, char delimiter) { + final List<Token> tokens = new ArrayList<>(); + final StringBuilder builder = new StringBuilder(); + for (int cursor = 0; cursor < string.length(); ) { + final char c = string.charAt(cursor); + + int nextChar = cursor + 1; + if (c == '\'') { + nextChar = consumeInQuotes(string, '\'', cursor, builder); + tokens.add(new Token(TokenType.SINGLE_QUOTED, builder.toString(), cursor)); + } else if (c == '"') { + nextChar = consumeInQuotes(string, '"', cursor, builder); + tokens.add(new Token(TokenType.DOUBLE_QUOTED, builder.toString(), cursor)); + } else if (c == delimiter) { + tokens.add(new Token(TokenType.DELIMITER, String.valueOf(c), cursor)); + } else if (!Character.isWhitespace(c)) { + nextChar = consumeUnquoted(string, delimiter, cursor, builder); + tokens.add(new Token(TokenType.UNQUOTED, builder.toString().trim(), cursor)); + } + builder.setLength(0); + cursor = nextChar; + } + + return tokens; + } + + private static int consumeInQuotes(String string, char quote, int cursor, StringBuilder builder) { + for (int i = cursor + 1; i < string.length(); i++) { + char c = string.charAt(i); + if (c == quote) { + if (i + 1 < string.length() && string.charAt(i + 1) == quote) { + builder.append(c); + i += 1; + } else { + return i + 1; + } + } else { + builder.append(c); + } + } + + throw new IllegalArgumentException("Could not split string. Quoting was not closed properly."); + } + + private static int consumeUnquoted(String string, char delimiter, int cursor, StringBuilder builder) { + int i; + for (i = cursor; i < string.length(); i++) { + char c = string.charAt(i); + if (c == delimiter) { + return i; + } else if (c == '\'' || c == '"') { + throw new IllegalArgumentException("Could not split string. Illegal quoting at position: " + i); + } else { Review comment: I think you can remove the `else`. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
